Reputation: 113
Hi I would like to clear a range of A3:J10000 in google sheet by using gspread.
Doing a loop like this takes too much time:
for cell in range_to_clear:
cell.value=''
sh.worksheet('WorksheetX').update_cells(range_to_clear,value_input_option='USER_ENTERED')
I found the values_clear() method but wasn't able to make it working:
range_2_delete = sh.worksheet("WorksheetX").range("A3:J10000")
sh.values_clear(range_2_delete)
The above code giving this error: AttributeError: 'list' object has no attribute 'encode'
Upvotes: 3
Views: 15177
Reputation: 7838
At the time of this posting, seems that what the Docs suggest to do is use the batch_clear
method on your Worksheet
object. Example:
# clear one or more ranges of values
worksheet.batch_clear(["A1:B1", "C2:E2", "my_named_range"])
Or the entire worksheet with worksheet.clear()
Upvotes: 2
Reputation: 201553
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
I think that the method of values_clear()
can be used for your situation.
Please modify your script as follows.
range_2_delete = sh.worksheet("WorksheetX").range("A3:J10000")
sh.values_clear(range_2_delete)
sh.values_clear("WorksheetX!A3:J10000")
or
sh.values_clear("'WorksheetX'!A3:J10000")
sh
is declared. If sh
is not declared, please use sh = gc.open('My poor gym results')
and sh = client.open_by_key(spreadsheetId)
. RefUpvotes: 8
Reputation: 124
You should try re-writing the code as follows:
range_2_delete = sh.worksheet("WorksheetX").range("A3:J10000")
values_clear(range_2_delete)
Upvotes: 0