Google工作表API 4:如何追加到行尾

时间:2016-09-07 03:35:35

标签: php google-sheets-api

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

$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);

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

3 个答案:

答案 0 :(得分:6)

您在寻找spreadsheets.values.append吗?示例用法指南为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.

如果那不是你想要的,你能否更具体地回答你的问题?

答案 1 :(得分:0)

由于我做了一个与您正在做的事情非常相似的项目(尽管我使用过JS),我建议检查行是否为空或不使用Sheetsv4 GET request。如果单元格为空,则使用Write on Sheets using PUT指南在那里写入附加数据。当我不知道最后一行时,这是我的动态解决方法。

答案 2 :(得分:0)

Sam指出,文档状态"范围用于搜索现有数据"和"值将附加到下一行"。

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

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

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

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