Python - How to get the date of the last Google Spreasheets update?

Asked

Viewed 74 times

0

Personal talk!
Next, I needed to check if there were any updates to the Google Spreasheets shared document. I thought I’d bring this field: Last edit by Google Spreadsheets as a date or as a timestamp in python and check with the last saved or if the difference was less than a day, but found nothing referent in the documentation.

An example code of what I was thinking of doing:

from oauth2client.service_account import ServiceAccountCredentials
import gspread
import pandas as pd

def get_gsheet_table(url, sheet_name):

    # Autenticacao com conta Google a partir de um arquivo JSON na pasta 'credentials'
    scope = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets"]
    credentials = ServiceAccountCredentials.from_json_keyfile_name('./credentials/get_sheet_table.json', scope)
    gc = gspread.authorize(credentials)

    # Recebe os dados da planilha definida no paremetro
    wb = gc.open_by_url(url)
    sheet = wb.worksheet(sheet_name)
    
    #TO DO: verificar se teve atualização no último dia

    data = sheet.get_all_values()
    df = pd.DataFrame(data)

    df.columns = df.iloc[0]
    df = df.iloc[1:]
    return df

If the code returns that the spreadsheet has not been updated it saves processes here. I’m looking to do this way to get the document date because I wish I didn’t need to make a backup spreadsheet and compare the two.

Any other suggestion will be welcome too

  • 1

    After a few days with no answer on this topic, I opened a question in the American community and got a perfect user response Tanaike. He showed me that he had yes in the Google Drive Api documentation how to look at the date of file modifications through the Api Revisions: list

1 answer

0


Thanks to the documentation link passed by the user Tanaike, I was able to solve the problem

How did the code part turn out:

import requests

revisions_uri = f'https://www.googleapis.com/drive/v3/files/{wb.id}/revisions'
    headers = {'Authorization': f'Bearer {credentials.get_access_token().access_token}'}
    response = requests.get(revisions_uri, headers=headers).json()
    print(response['revisions'][-1]['modifiedTime'])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.