英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论