Google脚本:当有人添加新行时,如何为特定工作表应用数据验证规则

时间:2014-06-10 01:12:11

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

情况:

我有以下脚本,当有人添加新行时运行。此脚本应检测工作表,并仅在特定工作表具有新行时应用数据验证或操作。

问题:

此脚本仅运行名为“ISP1”的工作表的数据验证,当我在工作表“ISP2”中添加新行时,在工作表“ISP1”中重新应用数据验证。

应该为每个工作表运行此脚本的某些部分,但只有当某个特定工作表有新行时,才会有部分工作。

AddNew Rows的功能:a link

脚本:

function initializeTrigger(){ // run this only once to create a trigger if necessary
  var sheet = SpreadsheetApp.getActive();
 ScriptApp.newTrigger("dataValidation")
   .forSpreadsheet(sheet)
   .onChange()
   .create();
}

function dataValidation(e){
  Logger.log(e.changeType);
  if(e.changeType=='INSERT_ROW'){
    // do Something

    //DataValidation - In Column B
    var cell1 = SpreadsheetApp.getActiveSheet().getRange('B3:B');
    var rule1 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['Open', 'Closed']).build();
      cell1.setDataValidation(rule1);  

            //DataValidation - In Column C
    var cell2 = SpreadsheetApp.getActiveSheet().getRange('C3:C');
    var rule2 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(true).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['Y','N']).build();
      cell2.setDataValidation(rule2);


      //DataValidation - In Column D & I for ISP1
    var ss = SpreadsheetApp.getActiveSheet();
    if(ss.getName() == "ISP1"){
    var cell3 = ss.getRange('D3:D');
    var rule3 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['DATCO', 'XFR MDA2', 'SCH CALL']).build();
      cell3.setDataValidation(rule3);

    var cell4 = ss.getRange('I3:I');
    var rule4 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['1:00:00', '2:00:00', '12:00:00', '24:00:00']).build();
      cell4.setDataValidation(rule4);

      }

      //DataValidation - In Column D & I for ISP2
    var ss = SpreadsheetApp.getActiveSheet();
    if(ss.getName() == "ISP2"){
    var cell5 = ss.getRange('D3:D');
    var rule5 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['NOC', 'CUS', 'DATCO', 'COMER']).build();
      cell5.setDataValidation(rule5);

    var cell6 = ss.getRange('I3:I');
    var rule6 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['0:15:00', '0:20:00', '1:00:00']).build();
      cell6.setDataValidation(rule6);
      }

    Browser.msgBox('New row(s) added, Data Validation Completed');
  }
}

只有当有人在特定工作表中添加新行时,才应运行此脚本并应用数据验证 -Column B& C应该为活动表运行。
-Column D& I只有当有人为特定表ISP1或ISP2添加新行时才应运行

其他脚本测试:

两个脚本都在onchange上运行。

function Data_V(){

  var s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  //var ss = event.source.getActiveSheet();
  if (s.getName() == "ISP1") {
  var numRows = s.getMaxRows();
  if(ScriptProperties.getProperty('numberOfRows')){
    var nRows = Number(ScriptProperties.getProperty('numberOfRows'));
    if(nRows<numRows){

      //DataValidation - In Column B
    var cell1 = s.getRange('B3:B');
    var rule1 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['Open', 'Closed']).build();
      cell1.setDataValidation(rule1);

            //DataValidation - In Column C
    var cell2 = s.getRange('C3:C');
    var rule2 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(true).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['Y','N']).build();
      cell2.setDataValidation(rule2);


      //DataValidation - In Column D
    var cell3 = s.getRange('D3:D');
    var rule3 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['DATCO', 'XFR MDA2', 'SCH CALL']).build();
      cell3.setDataValidation(rule3);

      //DataValidation - In Column I
    var cell4 = s.getRange('I3:I');
    var rule4 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['1:00:00', '2:00:00', '12:00:00', '24:00:00']).build();
      cell4.setDataValidation(rule4);

      ScriptProperties.setProperty('numberOfRows',numRows);// update value with current value
    }
    Browser.msgBox('New row(s) added, Data Validation Completed ISP1');
  }
  ScriptProperties.setProperty('numberOfRows',numRows);// create a start value with current value of simply update if the trigger was called for another reason
 }
}

/////////////////////////////////////////////// ////////////////////////////////////////////////// //////////////////

function Data_V2(){

  var s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  //var ss = event.source.getActiveSheet();
  if (s.getName() == "ISP2") {
  var numRows = s.getMaxRows();
  if(ScriptProperties.getProperty('numberOfRows')){
    var nRows = Number(ScriptProperties.getProperty('numberOfRows'));
    if(nRows<numRows){

      //DataValidation - In Column B
    var cell5 = s.getRange('B3:B');
    var rule5 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['Open', 'Closed']).build();
      cell5.setDataValidation(rule5);

            //DataValidation - In Column C
    var cell6 = s.getRange('C3:C');
    var rule6 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(true).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['Y','N']).build();
      cell6.setDataValidation(rule6);


      //DataValidation - In Column D
    var cell7 = s.getRange('D3:D');
    var rule7 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    .requireValueInList(['NOC', 'CUS', 'DATCO', 'COMER']).build();
      cell7.setDataValidation(rule7);

      //DataValidation - In Column I
    var cell8 = s.getRange('I3:I');
    var rule8 = SpreadsheetApp.newDataValidation()
    .setAllowInvalid(false).setHelpText("Seleccione Solo los Valores Indicados")
    ..requireValueInList(['0:15:00', '0:20:00', '1:00:00']).build();
      cell8.setDataValidation(rule8);

      //CopyFormat - In Column H
    //var source = s.getRange('H1');
    //var destination = s.getRange('H3:H');
    //source.copyTo(destination, {formatOnly:true});

      ScriptProperties.setProperty('numberOfRows',numRows);// update value with current value
    }
    Browser.msgBox('New row(s) added, Data Validation Completed ISP2');
  }
  ScriptProperties.setProperty('numberOfRows',numRows);// create a start value with current value of simply update if the trigger was called for another reason
 }
}

两个脚本都运行Onchange。当我在ISP1中添加行时,执行的脚本是ISP1的Data_V,但是当我在ISP2中添加新闻行时,脚本Data_V2没有运行,但Data_V再次运行。 我想也许只有我们可以在改变时有一个脚本,但当我尝试将两个脚本合并为一个脚本以运行一个脚本并检测哪个页面被编辑时不起作用。

0 个答案:

没有答案