0

I am using gspread in a python script and I have been trying to find a quick way of clearing a column in a given sheet.

Q: How can I clear the columns with gspread?

3kstc
  • 1,871
  • 3
  • 29
  • 53

4 Answers4

2

Take a look at the documentation.

You can only clear entire rows or multiple cells.

So, for a column you will need to do something like this :

worksheet = sheet.get_worksheet(0)

# Select a range (row by row)
cell_list = worksheet.range("N2:P2")
cell_list.extend(worksheet.range("N3:P3"))
cell_list.extend(worksheet.range("N4:P4"))
cell_list.extend(worksheet.range("N5:P5"))
cell_list.extend(worksheet.range("N6:P6"))

# Set the value
for cell in cell_list:
    cell.value = ''

# Update in batch
worksheet.update_cells(cell_list)
2

This is from gspread document and it works for me. To clear a column 'A' in a google sheet from all its contain

worksheet.batch_clear(['A:A'])

I hope this is useful for someone

Mmh
  • 21
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 21 '22 at 02:03
0

Clear method does not exist. You should loop through the data and

update_cell(row, col, "")
Davis Merrit
  • 123
  • 1
  • 1
  • 10
0

In latest version of gspread you have the following method values_cldar it takes a range as argument and will clear this range in a single HTTP request.

It should look like:

spsh = client.open("my sheet")
spsh.values_clear("A1:P4")
Lavigne958
  • 442
  • 5
  • 12