Last modified date in Google App Script GAS sheets

你说的曾经没有我的故事 提交于 2021-02-11 02:44:21

问题


I am trying to get the last modified date of a sheet from a GAS add on which I am developing.

My current idea is to get the Drive revision list and then take the last value. This seems a bit overkill for just getting the last modified, I am also worried that this will break if the number of revisions exceeds 1000 as per this link suggests.

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

Ideally I would like to know the range which has changed too, but I do not think this is possible.

I cannot use the onEdit event because I would like to track edits made by users who have not installed the add-on.

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

回答1:


I believe you can do that as follow

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

See function reference




回答2:


Given that you need users of your add-on to have access to revision information from non-add-on users, the Drive revision list is precisely what you need. Happily, you can get the content of revisions, so if you wish you can compute diffs. I don't know what your data looks like, so that might be easy or nigh-impossible.

Aside: to your point about more than 1000 revisions, if there are more than 1000 (or whatever your page size is) revisions, you'll get a nextPageToken like so:

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

If you see that you'll need to list revisions again, providing that token.

Anyway, when you list revisions, each revision will look something like this:

{
 "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"
 }
}

Provided your data isn't insanely complex or large, you could fetch the target the text/csv export link for the revisions you wish to compare, and then do that comparison in Apps Script.

That might look something like:

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


来源:https://stackoverflow.com/questions/54985937/last-modified-date-in-google-app-script-gas-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!