[英]How to get Date & Time using onEdit(e) function for multipal Sub Sheets in Google Spread Sheet
一個 Google 表格,每個用戶都有多個子表格。 每個用戶都在進行輸入,我需要根據數據輸入日期/時間計算性能。 我使用以下 onEdit(e) function 它適用於一張工作表......但是當我為其他子工作表制作相同腳本的多個副本時......它停止工作。
function onEdit(e) {
var s = e.source.getActiveSheet(),
watchcols = [2],
offsetcols = [-1],
ind = watchcols.indexOf(e.range.columnStart)
if (s.getName() !== "Sheet1" || ind === -1) return ;
e.range.offset(0,offsetcols[ind])
.setValue(!e.value ? null : Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm:SS"))
}
有人可以幫我解決這個問題嗎?
預先感謝您閱讀我的帖子並幫助我!
function onEdit(e) {
var s = e.source.getActiveSheet(),
watchcols = [2],
offsetcols = [-1],
ind = watchcols.indexOf(e.range.columnStart)
if (ind === -1) return ;
if (s.getName() === "Sheet1") {
e.range.offset(0,offsetcols[ind])
.setValue(!e.value ? null : Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm:SS"))
}
else if (s.getName() === "Sheet2") {
}
else if (s.getName() === "Sheet3") {
}
}
更新 - 使用 Utilities.formatDate(date, time zone, format) 和本地時區。 從此處查找您當地的時區 ID
如何將 function 與 onEdit() 分開:
function onEdit(e) {
var s = e.source.getActiveSheet();
var range = e.range;
var col = range.getColumn();
if (e.value){
if (s.getName() === "AAA" && col === 4) { setDateTime(range, -3)};
if (s.getName() === "BBB" && col === 6) { setDateTime(range, -5)};
if (s.getName() === "CCC" && col === 10) { setDateTime(range, -6)};
}
}
function setDateTime(range, offsetcols){
var date = Utilities.formatDate(new Date(),"America/New_York", "MM/dd/yyyy HH:mm:ss");
Logger.log(date);
range.offset(0,offsetcols).setValue(date);
}
嘗試這個:
function onEdit(e) {
const sh = e.range.getSheet();
const shts = ['Sheet1',"add others"]
const idx = shts.indexOf(sh.getName());
const watchcols = [2];
const offsetcols = [-1];
const ind = watchcols.indexOf(e.range.columnStart);
if (~idx && ~ind) {
e.range.offset(0,offsetcols[ind]).setValue(!e.value ? null : Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm:SS"))
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.