Google apps script 如何为多张图纸设置一个onOpen功能?
我有一本有6张类似表格的工作簿。我有一个脚本,可以帮助我根据A列中的日期保护行。每行都有一个日期。行是根据日期排序的,我们几乎每天都用新的日期和数据添加信息。当日期早于今天时,该行将受到保护,因此我们可以在该行中输入当前日期和未来的新信息 因此,今天黄色的行是受保护的。这里是链接 我使用的脚本:Google apps script 如何为多张图纸设置一个onOpen功能?,google-apps-script,google-sheets,scripting,Google Apps Script,Google Sheets,Scripting,我有一本有6张类似表格的工作簿。我有一个脚本,可以帮助我根据A列中的日期保护行。每行都有一个日期。行是根据日期排序的,我们几乎每天都用新的日期和数据添加信息。当日期早于今天时,该行将受到保护,因此我们可以在该行中输入当前日期和未来的新信息 因此,今天黄色的行是受保护的。这里是链接 我使用的脚本: function onOpen(e) { var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ISKUR
function onOpen(e) {
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ISKUR");
var dateRange = sh.getRange(6, 1, sh.getLastRow()-2, 1);
var val = dateRange.getDisplayValues();
var curDate = Utilities.formatDate(new Date(), "GMT+3", "MM/dd/YYYY");
var protectRow;
//check if date is less than the current date
for(var i = 0; i < val.length; i++){
if(val[i][0]>=curDate){
protectRow = i;
break;
}
}
var protection = sh.getProtections(SpreadsheetApp.ProtectionType.RANGE);
//If protection exists, update else add new one.
if(protection.length > 0){
var range = sh.getRange(6, 1, protectRow, 13);
protection[0].setRange(range);
}else{
sh.getRange(6, 1, protectRow, 13).protect();
}
}
function onOpen(e) {
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var dateRange = sh.getRange(6, 1, sh.getLastRow()-2, 1);
var val = dateRange.getDisplayValues();
var curDate = Utilities.formatDate(new Date(), "GMT+3", "MM/dd/YYYY");
var protectRow;
//check if date is less than the current date
for(var i = 0; i < val.length; i++){
if(val[i][0]>=curDate){
protectRow = i;
break;
}
}
var protection = sh.getProtections(SpreadsheetApp.ProtectionType.RANGE);
//If protection exists, update else add new one.
if(protection.length > 0){
var range = sh.getRange(6, 1, protectRow, 13);
protection[0].setRange(range);
}else{
sh.getRange(6, 1, protectRow, 13).protect();
}
}
开启功能(e){
var sh=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“ISKUR”);
var dateRange=sh.getRange(6,1,sh.getLastRow()-2,1);
var val=dateRange.getDisplayValues();
var curDate=Utilities.formatDate(新日期(),“GMT+3”,“MM/dd/YYYY”);
var protectRow;
//检查日期是否小于当前日期
对于(变量i=0;i=curDate){
protectRow=i;
打破
}
}
var保护=sh.getProtections(电子表格应用程序保护类型范围);
//如果存在保护,请更新或添加新的保护。
如果(保护长度>0){
var range=sh.getRange(6,1,protectRow,13);
保护[0]。设置范围(范围);
}否则{
sh.getRange(6,1,protectRow,13).protect();
}
}
功能开启(e){
var sh=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Sheet1”);
var dateRange=sh.getRange(6,1,sh.getLastRow()-2,1);
var val=dateRange.getDisplayValues();
var curDate=Utilities.formatDate(新日期(),“GMT+3”,“MM/dd/YYYY”);
var protectRow;
//检查日期是否小于当前日期
对于(变量i=0;i=curDate){
protectRow=i;
打破
}
}
var保护=sh.getProtections(电子表格应用程序保护类型范围);
//如果存在保护,请更新或添加新的保护。
如果(保护长度>0){
var range=sh.getRange(6,1,protectRow,13);
保护[0]。设置范围(范围);
}否则{
sh.getRange(6,1,protectRow,13).protect();
}
}
似乎它在好几页中都不能正常工作。如果所有行都具有类似的结构(a列中的日期),如何在多个工作表中使用此脚本保护行?问题:
不能有多个onOpen
触发器
解决:
解决方案1(推荐):对多张图纸执行代码,如下所示:
此代码假定您可以为在sheetNames
数组中指定的每张图纸运行相同的代码:
function onOpen(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNames = ['ISKUR','Sheet1']; // put the names of the sheets you want to run the script
sheetNames.forEach(name=>{
var sh = ss.getSheetByName(name);
var dateRange = sh.getRange(6, 1, sh.getLastRow()-2, 1);
var val = dateRange.getDisplayValues();
var curDate = Utilities.formatDate(new Date(), "GMT+3", "MM/dd/YYYY");
var protectRow;
//check if date is less than the current date
for(var i = 0; i < val.length; i++){
if(val[i][0]>=curDate){
protectRow = i;
break;
}
}
var protection = sh.getProtections(SpreadsheetApp.ProtectionType.RANGE);
//If protection exists, update else add new one.
if(protection.length > 0){
var range = sh.getRange(6, 1, protectRow, 13);
protection[0].setRange(range);
}else{
sh.getRange(6, 1, protectRow, 13).protect();
}
});
}
如果不同的工作表具有不同的结构,或者您希望为各个工作表应用独特的逻辑,则该解决方案将变得非常方便。问题:
不能有多个onOpen
触发器
解决:
解决方案1(推荐):对多张图纸执行代码,如下所示:
此代码假定您可以为在sheetNames
数组中指定的每张图纸运行相同的代码:
function onOpen(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNames = ['ISKUR','Sheet1']; // put the names of the sheets you want to run the script
sheetNames.forEach(name=>{
var sh = ss.getSheetByName(name);
var dateRange = sh.getRange(6, 1, sh.getLastRow()-2, 1);
var val = dateRange.getDisplayValues();
var curDate = Utilities.formatDate(new Date(), "GMT+3", "MM/dd/YYYY");
var protectRow;
//check if date is less than the current date
for(var i = 0; i < val.length; i++){
if(val[i][0]>=curDate){
protectRow = i;
break;
}
}
var protection = sh.getProtections(SpreadsheetApp.ProtectionType.RANGE);
//If protection exists, update else add new one.
if(protection.length > 0){
var range = sh.getRange(6, 1, protectRow, 13);
protection[0].setRange(range);
}else{
sh.getRange(6, 1, protectRow, 13).protect();
}
});
}
如果不同的工作表具有不同的结构,或者您希望为各个工作表应用独特的逻辑,该解决方案将非常方便。非常感谢您的帮助!!第一个解决方案是有效的。我肯定会在另一张纸上使用第二种变体!非常感谢你的帮助!!第一个解决方案是有效的。我肯定会在另一张纸上使用第二种变体!