【问题标题】:How can I set one onOpen function for several sheets?如何为多张纸设置一个 onOpen 函数?
【发布时间】:2021-02-24 10:56:46
【问题描述】:

我有一个包含 6 个类似工作表的工作簿。我有一个脚本可以帮助我根据 A 列中的日期保护行。每一行都有一个日期。这些行根据日期排序,我们几乎每天都会添加带有新日期和数据的信息。当日期早于今天时,该行将受到保护,因此我们可以在当前日期和未来的行中输入新信息。

因此,今天黄色行受到保护。这是worksheet的链接。

我使用的脚本:

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

它似乎无法在多个页面上正常工作。如果所有行都具有相似的结构(A 列中的日期),我如何在多个工作表中使用此脚本保护行?

【问题讨论】:

    标签: google-apps-script google-sheets scripting


    【解决方案1】:

    问题:

    您不能有多个 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();
           }                          
      });
    }
    

    解决方案 2:为每个工作表创建多个函数:

    如果您想为不同的工作表运行不同的代码,建议使用此解决方案:

    function onOpen(e){
      onOpen1(e);
      onOpen2(e);
    }
    
    function onOpen1(e) {
      var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
      // rest of the code
    }
    
    function onOpen2(e) {
      var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ISKUR");
      // rest of the code
    }
    

    如果不同的工作表具有不同的结构,或者您想为各个工作表应用独特的逻辑,该解决方案会很方便。

    【讨论】:

    • 非常感谢您的帮助!!第一个解决方案正在奏效。我肯定会在另一张纸上使用第二个变体!
    猜你喜欢
    • 2016-04-14
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多