- https://developers.google.com/sheets/api/reference/rest
- I was able to Create sheet and also clear sheet and some other operations
- But I am not able to get "number of row and the column" that are already filled, such that I can add some more data in the sheet.
-
are you asking how to get the next available/unused row?PiAreSquared– PiAreSquared2019-12-29 18:59:50 +00:00Commented Dec 29, 2019 at 18:59
-
Ya, For Eg: if 10 rows are filled, then I need to add more data from 11th row, old data should not get affected.Amrit Prasad– Amrit Prasad2019-12-29 19:02:22 +00:00Commented Dec 29, 2019 at 19:02
2 Answers
def nextAvailableRow(worksheet):
rowsUsed = len(filter(None, worksheet.col_values(1)))
return rowsUsed+1
This function should take in a worksheet as a parameter and output the next available row that is not used. It takes in all the values of the first column and then filters out all the empty rows to find how many rows have been used.
2 Comments
If you only want to insert new data after the last row with values, you can do it using the spreadsheets.values.append endpoint (Notice you can play with the API at "try this API" and there is also a Python example)
spreadsheet_id = 'your-spreadSheet-id'
ranges = "A1:A" # It will get the indo until the last row with data
value_render_option = "DIMENSION_UNSPECIFIED"
value_input_option = "USER_ENTERED"
# Body for the request
value_range_body = {
"values": [
[
"A11",
"B11"
],
[
"A12",
"B12"
]
],
"majorDimension": "DIMENSION_UNSPECIFIED"
}
request = service.spreadsheets().values()\
.append(spreadsheetId=spreadsheet_id, range=ranges, valueInputOption=value_input_option, body=value_range_body)
request.execute()
The outer array in values will represent the rows and the inner ones the columns as you can see in the example body in my code.
Notice: In another answer, someone is recommending you to use a third library, instead of Google Sheet's client library for Python. I would recommend you to use the Official Google library because it has the guarantee to be supported by Google.