16

I'm using the Google Sheets API v4 in Android.

https://developers.google.com/sheets/api/quickstart/android

I need to know when the last modification to the sheet was made (including by user); I need this guy:

enter image description here

I'd like to do something like this:

    Spreadsheet spreadsheet = sheetsService.spreadsheets().get(spreadsheetId).setIncludeGridData(true).execute();
    Date date = spreadsheet.getProperties().getLastEditDate();

But, of course, no such getLastEditDate() property method exists. Is there a parameter or another API method to call to get this data?

Even better would be to get the modified date for each cell... but I'd settle for the date of the entire spreadsheet or sheet.

LimaNightHawk
  • 6,613
  • 3
  • 41
  • 60

3 Answers3

14

This is not available in the Sheets API, but you may be able to use the Drive API's files.get method, which includes a 'modifiedTime' in the response. (Note that by default it will not include the modified time, you have to explicitly ask for it in the 'fields' parameter.)

Sam Berlin
  • 3,603
  • 12
  • 23
  • 2
    I think I'll add my own answer for others (later), but I'm certainly giving you the credit. Sometimes it is critical just to know "you cant!" Also, where to go (Drive API files.get). Thank you VERY much! – LimaNightHawk Apr 14 '17 at 23:25
  • In v3 of the Drive API, the code to do this looks like this: `driveService.files().get(documentKey).setFields("modifiedTime").execute()` – Luke Needham Nov 05 '21 at 13:28
6

It looks like this cannot be done with Sheets API v4.

However...it does look like it can be done with the compatible Google Drive API v3.

Note: the best part about this solution was that I could use the same method of authentication and credential gathering for both APIs. E.g., once I had the code for getting the credentials, I could use it for both API's interchangeably and consecutively.

Here's what I did:

Added this to my build.gradle (shown below my Sheets API declaration)

compile('com.google.apis:google-api-services-sheets:v4-rev468-1.22.0') {
    exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-drive:v3-rev69-1.22.0') {
    exclude group: 'org.apache.httpcomponents'
}

I was already using the EasyPermissions method for getting account and credentials. Great example here.

Then...

import com.google.api.services.drive.Drive;

...

protected Drive driveService = new Drive.Builder(transport, jsonFactory, credential)
            .setApplicationName("My Application Name")
            .build();

... async:

private DateTime getSheetInformation() throws IOException {

    String spreadsheetId = settings.getSpreadsheetId();
    Drive.Files.Get fileRequest = driveService.files().get(spreadsheetId).setFields("id, modifiedTime");
    File file = fileRequest.execute();
    if (file != null) {
        return file.getModifiedTime();
    }
    return null;
}
LimaNightHawk
  • 6,613
  • 3
  • 41
  • 60
1

The sheets api v3 will be deprecated in March 2020, when that happens, your best bet is to use the drive API.

https://developers.google.com/drive/api/v3/reference/files/list

you can pass

Derwent
  • 606
  • 1
  • 5
  • 13