Heim > Fragen und Antworten > Hauptteil
Ich bin neu bei der Google Sheets API, Derzeit entwickle ich ein System zum Senden von Daten an Google Sheets. Wenn ich versuche, Daten zu senden, wird ein Fehler angezeigt wie: Ungültiger Wert [0][0]
Unten sind meine Codezeilen.
`
$client = getClientAuth(); $spreadsheet_ID = $this->spreadsheet->spreadsheetId; $range = "Sheet1"; $values = $this->sheetData; $this->service = new Sheets($client); $body = new ValueRange([ 'majorDimension' => 'ROWS', 'values' => [array_map(function($nv) { return (is_null($nv)) ? "" : $nv; },$values)] ]); $params = ['valueInputOption' => 'RAW']; $insert = ['insertDataOption' => 'INSERT_ROWS']; //executing the request $data = $this->service->spreadsheets_values->append($spreadsheet_ID, $range, $body, $params, $insert); return $data;
`
P粉4468003292024-03-31 10:29:39
根据您的错误消息和显示脚本,我认为在您的情况下, $values
可能是一个二维数组。我认为如果 $values
是一维数组,那么您的脚本就可以工作。另外,请在 $params
中包含 'insertDataOption' => 'INSERT_ROWS'
。那么,既然如此,下面的修改如何呢?
$body = new ValueRange([ 'majorDimension' => 'ROWS', 'values' => [array_map(function($nv) { return (is_null($nv)) ? "" : $nv; },$values)] ]); $params = ['valueInputOption' => 'RAW']; $insert = ['insertDataOption' => 'INSERT_ROWS']; //executing the request $data = $this->service->spreadsheets_values->append($spreadsheet_ID, $range, $body, $params, $insert);
$body = new ValueRange([ 'majorDimension' => 'ROWS', 'values' => array_map(function($row) { return array_map(function($col) { return (is_null($col)) ? "" : $col; }, $row); }, $values) ]); $params = ['valueInputOption' => 'RAW', 'insertDataOption' => 'INSERT_ROWS']; $data = $service->spreadsheets_values->append($spreadsheet_ID, $range, $body, $params);
或
$body = new Google_Service_Sheets_ValueRange([ 'majorDimension' => 'ROWS', 'values' => array_map(function($row) { return array_map(function($col) { return (is_null($col)) ? "" : $col; }, $row); }, $values) ]); $params = ['valueInputOption' => 'RAW', 'insertDataOption' => 'INSERT_ROWS']; $data = $service->spreadsheets_values->append($spreadsheet_ID, $range, $body, $params);
当使用箭头函数时,我认为可以使用如下修改。
来自
'values' => array_map(function($row) {
return array_map(function($col) {
return (is_null($col)) ? "" : $col;
}, $row);
}, $values)
致
'values' => array_map(fn($row) => array_map(fn($col) => (is_null($col)) ? "" : $col, $row), $values)