5

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

How should I change the setValues params to add multiple rows?

Thank you.

UPDATE

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.

3 Answers 3

5

For those of you who want to append multiple rows instead of update rows. You can use this solution.

$valueRange = new Google_Service_Sheets_ValueRange(); $valueRange->setValues([['John', 'Doe'],['Jane', 'Doe']]); $range = 'Sheet1!A1:A'; $conf = ["valueInputOption" => "USER_ENTERED"]; $service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf); 

It took me ages to work this out hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

2

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 } }, ], } 

5 Comments

Thanks. I can add multiple rows by batchUpdate, but with this method I have to set the rows I want to add data. Is there a way to append multiple rows, even if I don't know which row has the last data?
Have you tried spreadsheets.values.append? as of now this is the append-related methond in SheetsV4.
Yes. But I can't add more then one row with append
If you check the sample on this Append Values guide it demonstrates adding more than one row using that method.
Thanks, it's working :) I needed to add the majorDimension attribute.
0

You need an array like this in php, for example

[ ['value1','value2','value3'], ['value4','value5','value6'] ] 

To do so, if you have multiple array like

[ [0][0]=>'value1',[1]=>'value2',[2]=>'value3', [1][0]=>'value4',[1]=>'value5',[2]=>'value6' ] 

You need to iterate subArrays

$values = []; $i = 0; foreach ($yourArr as $subArray): foreach ($subArray as $key => $value): $values[$i][$key] = $value; endforeach; $i++; endforeach; 

And then

$body = new Google_Service_Sheets_ValueRange([ 'values' => $values ]); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.