Trying to append a row to a Google Spreadsheet in PHP

徘徊边缘 提交于 2019-11-29 10:56:43

I had the same problem, there is a lack of documentation about this. But I found a solution. Here is a working example:

// ...

// Create the value range Object
$valueRange= new Google_Service_Sheets_ValueRange();

// You need to specify the values you insert
$valueRange->setValues(["values" => ["a", "b"]]); // Add two values

// Then you need to add some configuration
$conf = ["valueInputOption" => "RAW"];

// Update the spreadsheet
$service->spreadsheets_values->update($spreadsheetId, $range, $valueRange, $conf);

I think it's weird syntax, and I did not found clear documentation about it, I just tried some combination and now it works! Not sure it's the right way, hope it could help.

Adnan Javed
$client = $this->getClient();

$service = new Google_Service_Sheets($client);

$spreadsheetId = '1bFh8FYeez6c1snPCHZF_olsHOLRqEENJbChLKbE9xg0';

$range = 'A1:S1';

$values = [
    ['col 1', 'col 2', 'col 3'],
    ['col 1', 'col 2', 'col 3'],
    ['col 1', 'col 2', 'col 3']
];

$requestBody = new Google_Service_Sheets_ValueRange(array(
    'values' => $values
));

$params = [
    'valueInputOption' => 'RAW'
];

print_r($requestBody);

$response = $service->spreadsheets_values->update($spreadsheetId, $range, $requestBody, $params);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!