I want to delete a record from a google spreadsheet using the gspread
library.
Also, how to can I get the number of rows/records in google spreadsheet? gspread
provides .row_count()
, which returns the total number of rows, including those that are blank, but I only want to count rows which have data.
Since gspread version 0.5. 0 (December 2016) you can remove a row with delete_row() .
On your Android phone or tablet, open a spreadsheet in the Google Sheets app. Touch and hold the row or column you want to delete. In the menu that appears, tap Delete.
gspread is a Python API for Google Sheets. Features: Google Sheets API v4. Open a spreadsheet by title, key or url. Read, write, and format cell ranges.
Since gspread version 0.5.0 (December 2016) you can remove a row with delete_row()
.
For example, to delete a row with index 42, you do:
worksheet.delete_row(42)
Can you specify exactly how you want to delete the rows/records? Are they in the middle of a sheet? Bottom? Top?
I had a situation where I wanted to wipe all data except the headers once I had processed it. To do this I just resized the worksheet twice.
#first row is data header to keep
worksheet.resize(rows=1)
worksheet.resize(rows=30)
This is a simple brute force solution for wiping a whole sheet without deleting the worksheet.
One way would be to download the data in a json object using get_all_records()
then check the length of that object. That method returns all rows above the last non blank row. It will return rows that are blank if a row after it is not blank, but not trailing blanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With