Google Sheets脚本生成文档最后更改的时间、日期和用户。

huangapple go评论69阅读模式
英文:

Google Sheets script that produces the time, date, and user of the last time the document was changed

问题

当我运行脚本时,我收到错误消息:“TypeError: 无法在Sheet对象中找到getLastUpdated函数。\ngetLastModified @ Code.gs:3”。

function getLastModified() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lastModified = sheet.getLastUpdated();
  var user = Session.getActiveUser().getEmail();
  var timeZone = Session.getTimeZone();
  var formattedDate = Utilities.formatDate(lastModified, timeZone, "MM/dd/yyyy HH:mm:ss");
  var message = "Last modified by " + user + " on " + formattedDate;
  sheet.getRange("I1").setValue(message);
}

我正在尝试运行一个脚本,当表格更新时,在单元格I1中自动返回一条消息。

英文:

When I run the script I get the error "TypeError: Cannot find function getLastUpdated in object Sheet.
getLastModified @ Code.gs:3"

function getLastModified() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lastModified = sheet.getLastUpdated();
  var user = Session.getActiveUser().getEmail();
  var timeZone = Session.getTimeZone();
  var formattedDate = Utilities.formatDate(lastModified, timeZone, "MM/dd/yyyy HH:mm:ss");
  var message = "Last modified by " + user + " on " + formattedDate;
  sheet.getRange("I1").setValue(message);
}

I am trying to run a script to automaitcally return a message in cell I1 when the sheet is updated.

答案1

得分: 0

The error you're getting is because getLastUpdated() is not a valid method for a sheet class. You should use the onEdit function as it's an event and just grab your data.

Example:

function onEdit(e) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var rightNow = new Date();
  var user = Session.getActiveUser().getEmail();
  var timeZone = Session.getTimeZone();
  var formattedDate = Utilities.formatDate(rightNow, timeZone, "MM/dd/yyyy HH:mm:ss");
  var message = "Last modified by " + user + " on " + formattedDate;
  sheet.getRange("I1").setValue(message);
}
英文:

The error your'e getting is because getLastUpdated() is not a valid method for a sheet class. You should use the onEdit function as it's an event and just grab your data.

Example:

function onEdit(e) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var rightNow = new Date();
  var user = Session.getActiveUser().getEmail();
  var timeZone = Session.getTimeZone();
  var formattedDate = Utilities.formatDate(rightNow, timeZone, "MM/dd/yyyy HH:mm:ss");
  var message = "Last modified by " + user + " on " + formattedDate;
  sheet.getRange("I1").setValue(message);
}

huangapple
  • 本文由 发表于 2023年3月23日 10:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定