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()