Reputation: 3302
I have this spreasheet with a single table:
Calling this: POST 'https://sheets.googleapis.com/v4/spreadsheets/{my_id}/values/Sheet1!E1:append?valueInputOption=USER_ENTERED'
with POST method and with these headers:
Authorization: Bearer {valid_auth_token}
Accept: application/json
Content-Type: application/json
and this body:
{
"majorDimension": "COLUMNS",
"values": [
["Header", "Hello"]
]
}
Will append Header, Hello
as a column in E
. My question is: how can I append new columns after the last column without specifiying the new range (E1 this case)? Is there any way to do so?
With this call: https://sheets.googleapis.com/v4/spreadsheets/{my_id}
I am able to get the Sheets name, but unable to get the ranges, how can I know the range?
Upvotes: 3
Views: 2164
Reputation: 201378
Header
and Hello
to the next column of the last column on the Spreadsheet without directly giving the range.If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Unfortunately, in the current stage, there are no methods in the Sheets API for directly retrieving the data range. Although there are the method of spreadsheets.values.append and the AppendCellsRequest of the method of spreadsheets.batchUpdate, these can be used for putting the values to the next row of the last row on the sheet. It's not the column. Also in this case, the data range cannot be retrieved. So in order to achieve above your goal, I would like to propose the following workaround.
In this workaround, the Web Apps is used as the wrapper. The client accesses to the Web Apps, and the Web Apps retrieves the data range and put the values to the next column of the last column on the Spreadsheet. In this case, the range is not required to be set when it accesses to the Web Apps.
The usage of this workaround is as follows.
Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.
If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.
Please copy and paste the following script.
function doPost(e) {
var obj = JSON.parse(e.postData.contents);
var sheet = SpreadsheetApp.openById(obj.spreadsheetId).getSheetByName(obj.sheetName);
var values = obj.values;
sheet.getRange(1, sheet.getLastColumn() + 1, values.length, values[0].length).setValues(values);
return ContentService.createTextOutput("ok");
}
https://script.google.com/macros/s/###/exec
.
In order to test to access to the Web Apps, please use the following curl command.
curl -L \
-H "Authorization: Bearer ###" \
-d '{"spreadsheetId": "###", "sheetName": "Sheet1", "values": [["Header"], ["Hello"]]}' \
"https://script.google.com/macros/s/###/exec"
"Header"
and "Hello"
are put to the next column of the last column on the sheet of "Sheet1".https://www.googleapis.com/auth/drive.readonly
and/or https://www.googleapis.com/auth/drive
to the access token.If I misunderstood your question and this was not the direction you want, I apologize.
Upvotes: 3