首页 > 解决方案 > 如果使用 PHP 找到唯一 ID,则更新 Google 表格中的四列

问题描述

我有两个连续的 PHP 表格,表格一:应由团队成员填写,存储在带有自动生成的应用程序 ID 的谷歌表格中。表格二:应由团队负责人填写,与成员回复相关联地存储在上述相同的 google 表格中(如果负责人输入正确的申请 ID)。

Form One 运行良好!但是,使用Application ID关联Form 2响应并在同一行提交响应是我一直试图解决一个多星期但没有成功的技巧!

function submitApplication($responseId, $satisfaction, $attendance,
                                $communications, $commitment, $comments) 
{
    $indices = [
        'responseId' => 1
    ];
    $client = new \Google_Client();
    $client->setApplicationName('Google Sheets and php');
    $client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
    $client->setAccessType('offline');
    // $credentials_file = __DIR__ . "***";
    //connection to destination DB
    $credentials_file = __DIR__ . "***";
    $spreadsheetId = '***';
    $range = "Form Responses";
    $client->setAuthConfig($credentials_file);
    $service = new \Google_Service_Sheets($client);
    $response = $service->spreadsheets_values->get($spreadsheetId, $range);
    $values  = $response->getValues();

    foreach($values as $row){
        if (empty($row))
            continue;
        //for ($i=0; $i<$valuesLength; $i++){
        if($responseId === $row[$indices['responseId']]) {
            print_r($row);
            $valueRange = new Google_Service_Sheets_ValueRange();
            $options = array('valueInputOption' => 'RAW');
            $values = [[
                    $satisfaction
                    , $attendance
                    , $communications
                    , $commitment
                    , $comments
            ]];
          $body = new Google_Service_Sheets_ValueRange(['values' => $values]);
          $result = $service->spreadsheets_values->append($spreadsheetId, 'AB:AF', $body, $options);
          print($result->updatedRange. PHP_EOL);
        }
    }
}

标签: phpformsapigoogle-sheets

解决方案


推荐阅读