【问题标题】:Update values & create new row - Google Sheets API更新值并创建新行 - Google Sheets API
【发布时间】:2017-03-15 23:07:40
【问题描述】:
<?php 
$date_added = date('Y');
$api_key = 'abc';
$spreadsheet_id = '1-def';
$valueInputOption = 'USER_ENTERED';
$range="A9:F9";
$values = array("this","is","my","awesome","test","$date_added");
$access_token = "xyz";

$values_encoded = json_encode($values);

$AppendCellsRequest = "{
  'sheetId': 1,
  'rows': [
    {
      object(RowData)
    }
  ],
  'fields': $values_encoded,
}";

json_decode($AppendCellsRequest);

$url = 'https://sheets.googleapis.com/v4/spreadsheets/'.$spreadsheet_id.'/values/'.$range.'?valueInputOption='.$valueInputOption.'?key='.$api_key.
        '?access_token='.$access_token.'?response_type=code';

$url_json = file_get_contents($url);

if(!$url_encoded = json_decode($url_json, true)) {
    echo "Sorry";
}

?>

我的代码有点乱,但我现在是一片空白。

基本上,我希望我的脚本接受这些值并将它们添加到我的谷歌表格中。

在本例中,我将值设置为一个简单的数组,但我将包含一个非常基本的姓名、年龄和电话号码形式。

提前谢谢你。

【问题讨论】:

    标签: php json google-sheets-api


    【解决方案1】:

    Reading & Writing Cell Values 中所述,您可以使用以下方法:

    试试这个:

    $values = array(
        array(
            // Cell values ...
        ),
        // Additional rows ...
    );
    $body = new Google_Service_Sheets_ValueRange(array(
      'values' => $values
    ));
    $params = array(
      'valueInputOption' => $valueInputOption
    );
    $result = $service->spreadsheets_values->update($spreadsheetId, $range,
        $body, $params);
    

    您可以尝试以下示例:

    $values = array(
        array(
            // Cell values ...
        ),
        // Additional rows ...
    );
    $data = array();
    $data[] = new Google_Service_Sheets_ValueRange(array(
      'range' => $range,
      'values' => $values
    ));
    // Additional ranges to update ...
    
    $body = new Google_Service_Sheets_BatchUpdateValuesRequest(array(
      'valueInputOption' => $valueInputOption,
      'data' => $data
    ));
    $result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $body);
    

    此外,您可以选择是要覆盖表后的现有数据还是为新数据插入新行。默认情况下,输入会覆盖表格之后的数据。要将新数据写入新行,请指定 insertDataOption=INSERT_ROWS

    【讨论】:

      【解决方案2】:
      $result = $this->batchUpdateValues($id, 'A1:B2', 'USER_ENTERED', array(
              array('A', 'B'),
              array('C', 'D')
          ));
      

      public function batchUpdateValues($spreadsheetId, $range, $valueInputOption,
        $_values)
      {
          $service = $this->service;
          // [START sheets_batch_update_values]
          $values = [
              [
                  // Cell values ...
              ],
              // Additional rows ...
          ];
          // [START_EXCLUDE silent]
          $values = $_values;
          // [END_EXCLUDE]
          $data = [];
          $data[] = new Google_Service_Sheets_ValueRange([
              'range' => $range,
              'values' => $values
          ]);
          // Additional ranges to update ...
          $body = new Google_Service_Sheets_BatchUpdateValuesRequest([
              'valueInputOption' => $valueInputOption,
              'data' => $data
          ]);
          $result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $body);
          printf("%d cells updated.", $result->getTotalUpdatedCells());
          // [END sheets_batch_update_values]
          return $result;
      }
      

      来源:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多