IttayD
IttayD

Reputation: 29113

get last update of google sheet in apps script

I want to get the last update time of the sheet from within an apps script of that sheet. I tried looking at the docs and couldn't find mention of this and DocsList which apparently could be used to return the revision history (though I only want the last change date) is no longer functioning.

Upvotes: 2

Views: 6641

Answers (1)

Kishan
Kishan

Reputation: 1810

DocsList is deprecated, use DriveApp instead.

Try the following code:

// Replace 'SPREADSHEET_KEY_HERE' with the key of spreadsheet of which you want to get the date and time

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('Sheet1');

  var dateCreated = getCreatedDateTime('SPREADSHEET_KEY_HERE');
  s.getRange('A1').setValue(dateCreated);

  var lastUpdated = getLastUpdatedTime('SPREADSHEET_KEY_HERE');
  s.getRange('A2').setValue(lastUpdated);
};

function getLastUpdatedTime(SpreadsheetId) {
  return DriveApp.getFileById(SpreadsheetId).getLastUpdated();
};

function getCreatedDateTime(SpreadsheetId) {
  return DriveApp.getFileById(SpreadsheetId).getDateCreated();
};

Upvotes: 6

Related Questions