var SHEET_NAME = ["1", "2", "3", "????", "???"];
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "1" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 19);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
if( s.getName() == "2" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 19);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
if( s.getName() == "3" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 19);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
if( s.getName() == "????" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 19);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
if( s.getName() == "???" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 19);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setNumberFormat("MM/dd HH:mm:ss")
nextCell.setValue(new Date());
}
}
}
使用此腳本,要添加更多作業表,我需要在腳本上添加新作業表名稱和一堆代碼。
所以我想改變它們,如果作業表在他的名字中有特定的字母文本,它們會被這個腳本自動應用。
uj5u.com熱心網友回復:
無需一遍又一遍地重復相同的代碼塊。只需檢查編輯過的作業表是否在“sheetNames”陣列中。看看這是否有幫助
function onEdit(e) {
const sheetNames = ["1", "2", "3", "????", "???"];
const sheet = e.source.getActiveSheet();
if (sheetNames.includes(sheet.getName()) && e.range.columnStart === 1) {
const offset = e.range.offset(0, 19)
if (!offset.getValue()) {
offset.setValue(new Date()).setNumberFormat("MM/dd HH:mm:ss")
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/343995.html
