我一直在尝试找到一种简单的方法来在 Google 表格中添加“上次修改”时间戳。我发现这篇文章对我来说非常有效,完全符合我的需要。但是,我注意到,当我在大约一天后再次打开工作表时,所有时间戳都将更新为打开时间,而不是我上次实际修改单元格范围的时间。
与我类似的问题的许多其他答案对于我的需要来说太复杂了,而且我还没有成功地将它们编辑得更简单。
我对 Apps 脚本缺乏经验,不幸的是找不到一个好地方来研究它的文档。
这将最后更新的数据存储在 PropertiesService 文档属性中
function onEdit(e) {
//e.source.toast("Entry");
Logger.log(JSON.stringify(e));
const sh = e.range.getSheet();
let pupd = PropertiesService.getDocumentProperties().getProperty("pupd");
let name = sh.getName()
if (pupd) {
let upd = JSON.parse(pupd);
if (upd.hasOwnProperty(name)) {
//e.source.toast("gate1");
upd[name] = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy hh:mm:ss")
} else {
//e.source.toast('gate2');
upd.pa.push(name);
upd[name] = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy hh:mm:ss");
}
PropertiesService.getDocumentProperties().setProperty("pupd", JSON.stringify(upd));
} else {
//e.source.toast('gate3');
let upd = { pa: [] };
upd[name] = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy hh:mm:ss");
upd.pa.push(name);
PropertiesService.getDocumentProperties().setProperty("pupd", JSON.stringify(upd));
}
}
显示文档属性pupd
function dispDocProps() {
let p = PropertiesService.getDocumentProperties().getProperty("pupd");
const ui = SpreadsheetApp.getUi();
ui.showModelessDialog(HtmlService.createHtmlOutput(`<textarea cols="50",rows="12">${p}</textarea>`),"Doc Property pupd")
Logger.log(p);
}
将文档属性 pupd 重置为 null
function resetPupd() {
PropertiesService.getDocumentProperties().setProperty("pupd", '');
}
稍微增强了更新的显示
function enhancedDispOfPupd() {
const obj = JSON.parse(PropertiesService.getDocumentProperties().getProperty("pupd"));
const ui = SpreadsheetApp.getUi();
let s = `Past Updates \n`;
obj.pa.forEach(p => {
s += `${p} - ${obj[p]}\n`
});
ui.showModelessDialog(HtmlService.createHtmlOutput(`<textarea cols="40" rows="10">${s}</textarea>`),"Document Property pupd");
}
您现在可以通过使用“数据提取”功能来实现:
这是一个示例:https://docs.google.com/spreadsheets/d/1DAnC0TCgoPZ3GPFxAOoFVDRegNFVTFMyzGw2NmI-UXY/
Business Standard
,
Business Plus
,
Enterprise Essentials
,
Enterprise Standard
,
Enterprise Plus
,
Education Fundamentals
,
Education Plus
,
Education Standard
,
Teaching
和
Learning Upgrade
用户只能使用。https://support.google.com/docs/answer/13524011?hl=en#zippy=%2Cfile-smart-chip