用于PHP的Google工作表API会在追加时返回400错误

时间:2018-04-03 11:10:13

标签: php api google-sheets google-sheets-api google-apis-explorer

我正在尝试在电子表格的末尾附加数据,但我收到以下错误

  

Google_Service_Exception:{“error”:{“code”:400,
  “message”:“无效值[1716] [0]:struct_value {\ n fields {\ n
  key:\“0 \”\ n

这是我的代码:

$conf = ["valueInputOption" => "USER_ENTERED"];

$data[] = array($projects[0]);
$projects_data = new Google_Service_Sheets_ValueRange(array(
    'values' => $data
  ));             
$response = $service->spreadsheets_values->append($spreadsheetId, $range, $projects_data, $conf);

1 个答案:

答案 0 :(得分:0)

原来数据格式不正确。我将代码更改为以下内容并且有效。

for($i=0; $i<count($projects); $i++) {
  $projects_data = new Google_Service_Sheets_ValueRange(array(
    'values' => array(self::getFormattedData($projects[$i]))
  ));             
  $response = $service->spreadsheets_values->append($spreadsheetId, $range, $projects_data, $conf);
}    
public static function getFormattedData($data) {
    $ret = implode(',',$data);

    return explode(',',$ret);
}