how to reset all rows and column data uisng python gspread sheets

*爱你&永不变心* 提交于 2021-01-28 11:29:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!