Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete/remove row from the google spreadsheet using gspread lib. in python?

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.

like image 854
Chintan Avatar asked Jan 31 '13 12:01

Chintan


People also ask

How do I delete a row in Gspread?

Since gspread version 0.5. 0 (December 2016) you can remove a row with delete_row() .

How do I remove a row in Google Sheets?

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.

What is Gspread in Python?

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.


2 Answers

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)
like image 116
Burnash Avatar answered Oct 16 '22 10:10

Burnash


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.

Count Rows with data

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.

like image 8
AsAP_Sherb Avatar answered Oct 16 '22 10:10

AsAP_Sherb