0
2
  • are you asking how to get the next available/unused row? Commented 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. Commented Dec 29, 2019 at 19:02

2 Answers 2

1
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.

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

2 Comments

I am using google sheet, so what do you mean by worksheet ?
if you aren't already I would recommend using gspread because it offers more functionality and more functions
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.