本文介绍了通过API向Google Spreadsheet添加多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用以下代码向Google电子表格添加新行:
I can add a new row to a Google spreadsheet with this code:
$valueRange = new Google_Service_Sheets_ValueRange();
$valueRange->setValues(["values" => ['data1', 'data2']]);
$range = 'Sheet1!A1:A';
$conf = ["valueInputOption" => "USER_ENTERED"];
$service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf);
如何更改setValues参数以添加多行?
How should I change the setValues params to add multiple rows?
谢谢.
更新
与此同时,我使用这种方法:
In the meantime I use this approach:
$range_number = 1; // Keep spreadsheet header
$data = array();
for($i=0;$i<count($data);$i++){
$range_number++;
$range = 'Sheet1!A'.$range_number.':XX';
$values = array( array($data1[$i], $data2[$i], $data3[$i]) );
$data[] = new Google_Service_Sheets_ValueRange(
array( 'range' => $range, 'values' => $values )
);
}
$body = new Google_Service_Sheets_BatchUpdateValuesRequest(array(
'valueInputOption' => "USER_ENTERED",
'data' => $data
));
$result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $body);
// Clear rest of spreadsheet
$range_number++;
$range = 'Sheet1!A'.$range_number.':XX';
$clearRange = new Google_Service_Sheets_ClearValuesRequest();
$service->spreadsheets_values->clear($spreadsheetId, $range, $clearRange);
但是这种方式我也必须发送以前的数据.我的目标只是添加新数据.
But this way I have to send the previous data as well. My goal would be just to append the new data.
推荐答案
尝试遵循插入空的行或列指南.将"ROWS"指示为"dimension",并使用startIndex和endIndex添加所需的行数.
Try to follow the Insert an empty row or column guide. Indicate "ROWS" as your "dimension" and add how many rows you want by using startIndex and endIndex.
POST https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate
{
"requests": [
{
"insertDimension": {
"range": {
"sheetId": sheetId,
"dimension": "ROWS",
"startIndex": 0,
"endIndex": 100
},
"inheritBefore": false
}
},
],
}
这篇关于通过API向Google Spreadsheet添加多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!