Reputation: 479
Able to update the row by providing the range, here is the code:
$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);
Considering I don't know the range, how can I insert the row to the end of the sheet.
Upvotes: 2
Views: 13393
Reputation: 189
As Sam pointed out, the documentation states "range is used to search for existing data" and "Values will be appended to the next row".
Therefore, if you set the Range to the entire spreadsheet, you would get the results you desire. (Original post title says append to end of row, while your description says add a row to the end of the spreadsheet, so I assume the latter is what you want.)
Thus, set the range to the worksheet name.
$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);
Row will be added to the bottom of the spreadsheet.
Upvotes: 4
Reputation: 17613
Since I've done a project which is quite similar to the concept of what you're doing (I used JS though), I suggest checking the rows if it's empty or not using the 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 guide. This was my dynamic workaround when I didn't know the last row.
Upvotes: 0
Reputation: 3773
Are you looking for spreadsheets.values.append? The guide for example usage is here.
The reference docs for append
describe it as: 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.
If that's not what you're looking for, can you be more specific with your question?
Upvotes: 6