我想改变这个脚本在谷歌表


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());
}
}
}

这个脚本,添加更多的表,我需要添加新的表名和一堆代码的脚本。

所以我想改变他们,如果表格中有特定的字母文本在他的名字,他们自动应用这个脚本。

没有必要一遍又一遍地重复相同的代码块。只需检查编辑的工作表是否在'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")
}
}
}

相关内容

  • 没有找到相关文章

最新更新