0

I'm using a pretty simple python script to pull in a Google Sheet (that I don't have write access to) with the requests library, and write out a csv file, modifying the headers to what my CMS (Wordpress) needs for to update some data.

I don't want to re-export the file unless it's the Google Sheet modified since the last time I ran the script.

  • It looks like using the Python Google API library is one option, but maybe more than is necessary for this task.

  • The GSpread package looks promising, but the worksheet.updated attribute, which it looks like back in 2015 would have held a datetime string seems to be currently non-functional

This :

>>> import gspread
>>> from oauth2client.service_account import ServiceAccountCredentials
>>> scope = ['https://spreadsheets.google.com/feeds'] 
>>> creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
>>> client = gspread.authorize(creds)
>>> file_id = '1rYQ84SIKTFUOTl1xyFkHMNLt9CW3tOMFbOelOvVwx1k'
>>> url = "https://docs.google.com/spreadsheets/d/{0}".format(file_id)
>>> doc = client.open_by_url(url)
>>> sheet = doc.get_worksheet(0)
>>> sheet.updated
>>> 

My original thought was save a copy of the non-modified imported csv and compare the any new imports to that before creating my new export file. Maybe that's a simple enough solution as it's only a few hundred lines of data.

Any recommendations?

tehhowch
  • 9,645
  • 4
  • 24
  • 42
MikeiLL
  • 6,282
  • 5
  • 37
  • 68
  • Could you use the Dev Metadata endpoint at the sheet level to insert a timestamp yourself? https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.developerMetadata#DeveloperMetadata.DeveloperMetadataLocationType – Lane Terry Jun 24 '18 at 23:37
  • 1
    How about retrieving the modified time using ``GET https://www.googleapis.com/drive/v3/files/### file ID ###?fields=modifiedTime`` of [Drive API](https://developers.google.com/drive/api/v3/reference/files/get)? If this was not what you want, I'm sorry. – Tanaike Jun 25 '18 at 00:05
  • @LaneTerry it's not a file I have write access to so that would preclude that solution, wouldn't it? – MikeiLL Jun 25 '18 at 01:17
  • @Tanaike is that an approach that would work for a publicly readable file that I don't have write access to? If so, does the `### file ID ###` in the `docs.google.com/spreadsheets` url correspond with the `### file ID ###` in the `www.googleapis.com/drive` url? – MikeiLL Jun 25 '18 at 01:22
  • Although you have the permission for reading spreadsheet, you don't have the permission for writing it. I could understand about this. But from your question, I couldn't know whether your token has the permission for using Drive API. I'm sorry. And the method is GET for Drive API. You can see about the document of file ID at [here](https://developers.google.com/sheets/api/guides/concepts#spreadsheet_id). – Tanaike Jun 25 '18 at 01:54
  • 1
    Related question: https://stackoverflow.com/questions/50380144/pygsheets-updated-returns-file-not-found – tehhowch Jul 21 '18 at 16:20

0 Answers0