问题
I have the google sheet in which sheet has all data but how to reset the google sheet before adding the data in same google sheets any help would be appreciated
Google Sheet Image Link Given Here :https://i.stack.imgur.com/6sbvr.png
回答1:
- You want to reset the sheet using gspread.
- You have already been able to get and put values for Google Spreadsheet using gspread.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Pattern 1:
If reset
in your question is to clear all values of cells on the sheet in the Spreadsheet, how about the following sample script? In this pattern, all values in the cells are cleared.
Sample script:
spreadsheetId = "###" # Please set the Spreadsheet ID.
sheetName = "Sheet1" # Please set the sheet name.
spreadsheet = client.open_by_key(spreadsheetId)
worksheet = spreadsheet.worksheet(sheetName)
worksheet.clear()
Pattern 2:
If reset
in your question is to clear all values and format of cells on the sheet in the Spreadsheet, how about the following sample script? In this pattern, all values and cell formats in the cells are cleared.
Sample script:
spreadsheetId = "###" # Please set the Spreadsheet ID.
sheetName = "Sheet1" # Please set the sheet name.
spreadsheet = client.open_by_key(spreadsheetId)
worksheet = spreadsheet.worksheet(sheetName)
requests = {"requests": [{"updateCells": {"range": {"sheetId": worksheet._properties['sheetId']}, "fields": "*"}}]}
res = spreadsheet.batch_update(requests)
print(res)
References:
- clear()
- batch_update()
- UpdateCellsRequest
If I misunderstood your question and this was not the result you want, I apologize.
来源:https://stackoverflow.com/questions/60015321/how-to-reset-all-rows-and-column-data-uisng-python-gspread-sheets