Google App Script GAS工作表中的上次修改日期

时间:2019-03-04 15:04:12

标签: google-apps-script google-sheets-api

我正在尝试从我正在开发的GAS附件中获取工作表的最后修改日期。

我当前的想法是获取云端硬盘修订版列表,然后获取最后一个值。仅仅修改最后一个似乎有点矫kill过正,我也担心如果此链接建议的修订数量超过1000,这会破坏。

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

理想情况下,我想知道范围也发生了变化,但我认为这是不可能的。

我无法使用onEdit事件,因为我想跟踪未安装该加载项的用户所做的编辑。

  var fileId = SpreadsheetApp.getActiveSpreadsheet().getId();
  var revisions = Drive.Revisions.list(fileId);
  var revisionLength = revisions.items.length;

  if(revisionLength > 0){
    var revision = revisions.items[revisionLength-1];
    var date = new Date(revision.modifiedDate);
    Logger.log(date.toString());
  }

2 个答案:

答案 0 :(得分:0)

鉴于您需要附加组件的用户才能访问非附加用户的修订信息,因此Drive修订列表正是您所需要的。令人高兴的是,您可以获取修订的内容,因此,如果您希望可以计算差异。我不知道您的数据是什么样子,所以这可能很简单,也几乎不可能。

此外:大约有1000个以上的修订,如果有1000个以上(或页面大小不限)的修订,您将得到一个nextPageToken像这样:

{
 "kind": "drive#revisionList",
 "nextPageToken": "BHNMJKHJKHKVJHyugaiohasdzT1JyUmlQWG10RUJ1emx1S2xNDg4EgQzMzY1GAI=",
 "revisions": [
    ...
 ]
}

如果看到您需要再次列出修订版本,请提供该令牌。

无论如何,当您列出修订时,每个修订将看起来像这样:

{
 "kind": "drive#revision",
 "etag": "\"som3-e-tAg\"",
 "id": "3365",
 "selfLink": "https://www.googleapis.com/drive/v2/files/dummydummydummy/revisions/3365",
 "mimeType": "application/vnd.google-apps.spreadsheet",
 "modifiedDate": "2018-10-19T19:05:41.762Z",
 "published": false,
 "exportLinks": {
  "application/x-vnd.oasis.opendocument.spreadsheet": "https://docs.google.com/spreadsheets/export?id=dummydummydummy&revision=3365&exportFormat=ods",
  "text/tab-separated-values": "https://docs.google.com/spreadsheets/export?id=dummydummydummy&revision=3365&exportFormat=tsv",
  "application/pdf": "https://docs.google.com/spreadsheets/export?id=dummydummydummy&revision=3365&exportFormat=pdf",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "https://docs.google.com/spreadsheets/export?id=dummydummydummy&revision=3365&exportFormat=xlsx",
  "text/csv": "https://docs.google.com/spreadsheets/export?id=dummydummydummy&revision=3365&exportFormat=csv",
  "application/zip": "https://docs.google.com/spreadsheets/export?id=dummydummydummy&revision=3365&exportFormat=zip",
  "application/vnd.oasis.opendocument.spreadsheet": "https://docs.google.com/spreadsheets/export?id=dummydummydummy&revision=3365&exportFormat=ods"
 },
 "lastModifyingUserName": "Joe User",
 "lastModifyingUser": {
  "kind": "drive#user",
  "displayName": "Joe User",
  "picture": {
   "url": "https://lh3.googleusercontent.com/-asdfsadf/AAAAAAAAAAI/AAAAAAAAFOk/OIPUYOIUGO/s64/photo.jpg"
  },
  "isAuthenticatedUser": true,
  "permissionId": "123456789",
  "emailAddress": "user@gmail.com"
 }
}

如果您的数据并非异常复杂或庞大,则可以通过text/csv导出目标获取要比较的修订版本的目标,然后在Apps Script中进行比较。

可能看起来像这样:

var fileId = SpreadsheetApp.getActiveSpreadsheet().getId();
var revisions = Drive.Revisions.list(fileId);
var revisionLength = revisions.items.length;

if(revisionLength > 1) {  // something to compare!
  var revision = revisions.items[revisionLength-1];
  var newContent = UrlFetchApp.fetch(revision.exportLinks["text/csv"]).getContent();
  newContent = Utilities.parseCsv(newContent);

  var oldRevision = revisions.items[revisionLength-2];
  var oldContent = UrlFetchApp.fetch(oldRevision.exportLinks["text/csv"]).getContent();
  oldContent = Utilities.parseCsv(oldContent);

  # TODO check they're the same size!

  # where do they differ?
  for (var row = 0; row < newContent.length; row++) {
    for (var col = 0; col < newContent[0].length; col++) {
      if (newContent[row][col] != oldContent[row][col]) {
        Logger.log('Change on row ' + (row + 1) + ' column ' + (col + 1));
      }
  }

  # when did it change?
  var date = new Date(revision.modifiedDate);
  Logger.log(date.toString());
}

答案 1 :(得分:0)

我相信您可以按照以下步骤进行操作

var lastUpdated = DriveApp.getFileById(fileId).getLastUpdated();

请参见函数reference