使用GoogleSheets Api更新单元格值

时间:2017-10-11 14:09:45

标签: php google-sheets

我可以使用Google Api更新单元格值吗?

我已经可以从谷歌表中读取所有值,但我无法更新任何值。

我试试这个:

   $body = new Google_Service_Sheets_ValueRange(array(
          'values' => $locations
        ));
        $params = array(
          'valueInputOption' => $locations
        );

        $result2 = $service->spreadsheets_values->update($spreadsheetId, "A1",  $body, $params);
      print($result2);

但是我收到了这个错误:

Fatal error: Uncaught Google_Service_Exception: {
 "error": {
   "code": 400,
   "message": "Invalid value at 'data.values[0]' (type.googleapis.com/google.pr
otobuf.ListValue), \"Stant 1\"\nInvalid value at 'data.values[1]' (type.googleap
is.com/google.protobuf.ListValue), \"Stant 10\"\nInvalid value at 'value_input_o
ption' (TYPE_ENUM), \"\"",
   "errors": [
     {
       "message": "Invalid value at 'data.values[0]' (type.googleapis.com/googl
e.protobuf.ListValue), \"Stant 1\"\nInvalid value at 'data.values[1]' (type.goog
leapis.com/google.protobuf.ListValue), \"Stant 10\"\nInvalid value at 'value_inp
ut_option' (TYPE_ENUM), \"\"",
       "domain": "global",
       "reason": "badRequest"
     }
   ],
   "status": "INVALID_ARGUMENT"
 }
}

1 个答案:

答案 0 :(得分:0)

好吧,谢谢,我找到了解决方案 - >

$options = array('valueInputOption' => 'RAW');

http://ajaxray.com/blog/store-data-to-google-sheets-using-php/

$options = array('valueInputOption' => 'RAW');
    $values = [
        ["Name", "Roll No.", "Contact"],
        ["Anis", "001", "+88017300112233"],
        ["Ashik", "002", "+88017300445566"]
    ];
    $body   = new Google_Service_Sheets_ValueRange(['values' => $values]);

    $result = $service->spreadsheets_values->update(SHEET_ID, 'A1:C3', $body, $options);
    print($result->updatedRange. PHP_EOL);