gotqks
gotqks

Reputation: 29

How to use range in google sheet api?

enter image description here

I want to add a new value from B3.

B1:E1 is from the first line of B to the first line of E.

B:E is from the beginning of B to the end of E.

I wonder what the difference is when the range is B:E, B1:E1.

When there is a value in column A, the new value is appended to the end of A.

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append

service = discovery.build('sheets', 'v4', credentials=credentials)
spreadsheet_id = 'my-spreadsheet-id
range_ = 'sheet1!B:E' vs'sheet1!B1:E1'  # Questions
value_input_option = ''
insert_data_option = ''

value_range_body = {

}

request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, valueInputOption=value_input_option, insertDataOption=insert_data_option, body=value_range_body)
response = request.execute()

pprint(response)

Upvotes: 1

Views: 2128

Answers (1)

Nikko J.
Nikko J.

Reputation: 5533

API will check if the provided range is part of a data table by checking the content of the adjacent cells. If both range and its adjacent cells contain data, the range is considered part of the data table. (In your example, the data table is A1:E10)

Since B:E and B1:E1 have data in cell B1 and B1 is adjacent to the data of cell A1, the range is considered part of A1:E data table. This causes the API to append the data to column A.

You can verify this by creating another sheet, populate only cells B1:E2 and execute the API.

Example:

Here I used B:E as range

Before Executing Append API:

enter image description here

After Executing Append API

enter image description here

In your case, you need to explicitly define the row number in your range (B3:E3) and that range must be empty or else it will append its data to the first empty row below the provided range.

Reference:

Upvotes: 1

Related Questions