Upload values to google sheets

2019-08-20 06:07发布

I am trying to upload data to google sheets via sheets API, but I cannot figure out right format of data. This is how I understood It from docs,

public function insert($spreadSheetId)
{
    $body = array( 
                  array('ab', 'cd'),
                  array('ef', 'gh')
            );                     

    $values = new Google_Service_Sheets_ValueRange();
    $values->setValues(['values' => $body]);
    $this->client->spreadsheets_values->update($spreadSheetId, 'A1:B2', $values, ['valueInputOption' => 'RAW']);
}

But I am getting expcetion

HP Fatal error:  Uncaught exception 'Google_Service_Exception' with message '{
  "error": {
    "code": 400,
    "message": "Invalid values[0][0]: list_value {\n  values {\n    string_value: \"ab\"\n  }\n  values {\n    string_value: \"cd\"\n  }\n}\n",
    "errors": [
      {
        "message": "Invalid values[0][0]: list_value {\n  values {\n    string_value: \"ab\"\n  }\n  values {\n    string_value: \"cd\"\n  }\n}\n",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

1条回答
Anthone
2楼-- · 2019-08-20 06:38

Right format was

$body = array( 
                  array('ab', 'cd'),
                  array('ef', 'gh')
            );                     

    $values = new Google_Service_Sheets_ValueRange();
    $values->setValues($body);
查看更多
登录 后发表回答