使用Sheets API v4获取Google Sheets文档的最后编辑日期(Java)

16

我正在使用Android中的Google Sheets API v4。

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

我需要知道表格的最后修改时间(包括用户的修改)。 我需要这个选项:

enter image description here

我想做类似于这样的事情:

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

当然,这样的getLastEditDate()属性方法并不存在。是否存在参数或另一个API方法可调用以获取此数据?

更好的方法是获取每个单元格的修改日期...但我会接受整个电子表格或工作表的日期。

3个回答

15

此功能在Sheets API中不可用,但您可以尝试使用Drive API的files.get方法,在响应中包括“modifiedTime”信息。(请注意,默认情况下它不会包括修改时间,您需要在“fields”参数中明确请求。)


2
我想我会为其他人添加自己的答案(稍后),但我肯定会给你信用。有时候知道“你不能!”是至关重要的。此外,还需要知道去哪里(Drive API files.get)。非常感谢! - LimaNightHawk
在Drive API的v3版本中,实现此功能的代码如下:driveService.files().get(documentKey).setFields("modifiedTime").execute() - Luke Needham

6
看起来这似乎无法使用Sheets API v4完成。
然而...使用兼容的Google Drive API v3似乎是可行的。
注意:这种解决方案最好的部分是,我可以为两个API使用相同的身份验证和凭据收集方法。例如,一旦我获得了获取凭据的代码,我可以交替和连续地使用它们。
这是我所做的:
在我的build.gradle中添加了以下内容(在我的Sheets API声明下面显示)。
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'
}

我已经在获取账户和凭据方面使用了EasyPermissions方法。这里有一个很好的例子

然后...

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

...

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

...异步:

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;
}

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接