Google张API4:如何追加到行尾

Google sheets API 4: How to append to the end of the row

可以通过提供范围来更新行,这里是代码:

$range = "A1:B1";
$valueRange= new Google_Service_Sheets_ValueRange();
$valueRange->setValues(["values" => ["a", "b"]]); 
$conf = ["valueInputOption" => "RAW"];
$service->spreadsheets_values->update($spreadsheetId, $range, $valueRange, $conf);

考虑到我不知道范围,如何将行插入到 sheet 的末尾。

您在寻找 spreadsheets.values.append? The guide for example usage is here.

append 的参考文档将其描述为:Appends values to a spreadsheet. The input range is used to search for existing data and find a "table" within that range. Values will be appended to the next row of the table, starting with the first column of the table. See the guide and sample code for specific details of how tables are detected and data is appended.

如果这不是您要查找的内容,您能否更具体地说明您的问题?

由于我完成了一个与您正在做的概念非常相似的项目(尽管我使用的是 JS),我建议使用 GET request for Sheetsv4. Make it such that if the cell is empty, you'll write the appended data there using the Write on Sheets using PUT 指南检查行是否为空.当我不知道最后一行时,这是我的动态解决方法。

正如 Sam 指出的那样,文档指出 "range is used to search for existing data" 和 "Values will be appended to the next row"。

因此,如果将范围设置为整个电子表格,您将获得所需的结果。 (原 post 标题说追加到行尾,而您的描述说在电子表格末尾添加一行,所以我假设后者是您想要的。)

因此,将范围设置为工作表名称。

$range = "Sheet1";       //your worksheet name
$valueRange= new Google_Service_Sheets_ValueRange();
$valueRange->setValues(["values" => ["a", "b"]]); 
$conf = ["valueInputOption" => "RAW"];
$service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf);

行将添加到电子表格的底部。