2

I'm trying to delete rows that column B date is less than 30 days.

I can correctly print the rows I want to eliminate with print(row) on the following code. but the sheet.delete_row() does not work with the following error:

AttributeError: 'Resource' object has no attribute 'delete_row'

from google.oauth2 import service_account
from googleapiclient.discovery import build
import datetime


service_account_file = 'keys.json'
scopes = ['https://www.googleapis.com/auth/spreadsheets']

credential = service_account.Credentials.from_service_account_file(service_account_file, scopes=scopes)

spreadsheet_id = ‘testtesttesttest’
service = build('sheets', 'v4', credentials=credential)
sheet = service.spreadsheets()

result = sheet.values().get(spreadsheetId=spreadsheet_id, range='database!A2:J').execute()
values = result.get('values', [])

date = datetime.datetime.now() - datetime.timedelta(30)

for row in values:
    if datetime.datetime.strptime(row[1], '%d/%m/%Y') < date:
       sheet.delete_row()
  • Does this answer your question? [How to delete rows in google spreadsheet using SHEET API v4 by Python?](https://stackoverflow.com/questions/56168301/how-to-delete-rows-in-google-spreadsheet-using-sheet-api-v4-by-python) – Iamblichus Jun 28 '21 at 09:22

1 Answers1

0

Try use spreadsheets().batchUpdate() from here, delete_row seems like used by gspread (another Python API for Google Sheets)

SCKU
  • 783
  • 9
  • 14