Google 脚本:适配脚本以适用于多个工作表,快速更正
Google script: adapting script to apply to multiple sheets, quick correction
我试图添加额外的行,以便此时间戳脚本适用于我 sheet 中的多个选项卡。
如果我在 sheet 'Sanshiro' 上的 E 列中输入数据,时间戳就会起作用。
但是,如果在任何其他指定的 sheet 上输入 Col E,则不会发生任何事情。
有什么需要改变的吗?
这是 sheet 我正在尝试使用它。
https://docs.google.com/spreadsheets/d/1K8QhVKWSsvHTFKDHNv3ySb5bcaU9I7tczIZvIkegbw0/edit#gid=1582815105
提前致谢!!
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sanshiro" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
if( s.getName() == "Josh" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
if( s.getName() == "Suil" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
我解决了自己的问题,但还是感谢您的关注!
我想我会 post 对于那些还不熟悉脚本的人。
我没有正确关闭每个部分 }
所以我在每个 sheet 的每个部分后需要两个 }。
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sanshiro" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
} }
if( s.getName() == "Josh" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
} }
if( s.getName() == "Suil" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
} }
if( s.getName() == "Yujiro" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
} }
if( s.getName() == "Martin" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
您的脚本似乎可以简化为:
function onEdit(e) {
var sheets = ["Sanshiro", "Josh", "Suil", "Yujiro", "Martin"];
if (sheets.indexOf(e.source.getActiveSheet()
.getName()) === -1 || e.range.columnStart !== 5) return;
e.range.offset(0, -4)
.setValue(new Date());
}
说明:
- 您希望脚本处理的所有 sheet 名称都在数组 'sheets'
中
- if 语句检查是否可以在数组 'sheets' 中找到当前编辑的 sheet (e.source.getActiveSheet.getName) 的名称。如果不是 indexOf() 将 return -1 在这种情况下我们退出脚本。
- if 语句的第二部分检查编辑的列是否为第 5 列,如果不是,则退出。
- 如果确实满足两个条件,则在已编辑的 sheet 的 A 列中设置时间戳(第 5 列 --> 偏移 0 行和 -4 列 = 相同的第 1 (A) 列行)。
希望对您有所帮助?
我试图添加额外的行,以便此时间戳脚本适用于我 sheet 中的多个选项卡。
如果我在 sheet 'Sanshiro' 上的 E 列中输入数据,时间戳就会起作用。 但是,如果在任何其他指定的 sheet 上输入 Col E,则不会发生任何事情。 有什么需要改变的吗?
这是 sheet 我正在尝试使用它。 https://docs.google.com/spreadsheets/d/1K8QhVKWSsvHTFKDHNv3ySb5bcaU9I7tczIZvIkegbw0/edit#gid=1582815105
提前致谢!!
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sanshiro" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
if( s.getName() == "Josh" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
if( s.getName() == "Suil" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
我解决了自己的问题,但还是感谢您的关注! 我想我会 post 对于那些还不熟悉脚本的人。
我没有正确关闭每个部分 } 所以我在每个 sheet 的每个部分后需要两个 }。
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sanshiro" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
} }
if( s.getName() == "Josh" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
} }
if( s.getName() == "Suil" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
} }
if( s.getName() == "Yujiro" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
} }
if( s.getName() == "Martin" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 5 ) { //checks the column
var nextCell = r.offset(0, -4);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
}
}
您的脚本似乎可以简化为:
function onEdit(e) {
var sheets = ["Sanshiro", "Josh", "Suil", "Yujiro", "Martin"];
if (sheets.indexOf(e.source.getActiveSheet()
.getName()) === -1 || e.range.columnStart !== 5) return;
e.range.offset(0, -4)
.setValue(new Date());
}
说明:
- 您希望脚本处理的所有 sheet 名称都在数组 'sheets' 中
- if 语句检查是否可以在数组 'sheets' 中找到当前编辑的 sheet (e.source.getActiveSheet.getName) 的名称。如果不是 indexOf() 将 return -1 在这种情况下我们退出脚本。
- if 语句的第二部分检查编辑的列是否为第 5 列,如果不是,则退出。
- 如果确实满足两个条件,则在已编辑的 sheet 的 A 列中设置时间戳(第 5 列 --> 偏移 0 行和 -4 列 = 相同的第 1 (A) 列行)。
希望对您有所帮助?