通过多个条件和验证来保护和验证多个范围

时间:2019-06-26 09:08:58

标签: google-apps-script

下面的代码可以正常工作,但是执行时间很长

var j=0;
for(var j=0;j<k;j++)
{
    var cell = billsheet.getRange('M'+(billlastrow+(j+1)));
    var range = contractorsheet.getRange(vrangearray[j][0]);
    var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build();
    cell.setDataValidation(rule);
    var prange = billsheet.getRange('E'+(billlastrow+j+1)+':P'+(billlastrow+j+1));
    var protection = prange.protect();
    //var me = Session.getEffectiveUser();//don't use this line
    protection.removeEditors(protection.getEditors());
    protection.addEditor(vrangearray[j][1]);  
    //Logger.log(j);
} // end for k

我修改了以下代码

var depts = packagesheet.getRange('B1:AJ5').getValues();
var billarray=[];
var crangearray=[];
var prangearray=[];
var vrangearray=[];
var erangearray=[];


var j=0;
var k=0
for(var j=0;j<=34;j++)
{
  if (depts[0][j]=="" || laborvalues[0][(j+1)]=="-") {} else {
    k++
    if (depts[0][j].substring(0,5)=="Extra") {var extra=1} else {var extra=0};
    //                         ID              Lot    ID_Lot                       Customer             Panel          Section      Dept       Qty    Remark Start  COMPLETED,   End Contractor    Rate                  Rate2                 Bill     Supervisor   Fed    Colour    Extra
    billarray.push([ordervalues[0][0], lotno, ordervalues[0][0]+'/'+lotno, ordervalues[0][1], ordervalues[0][2],depts[0][j],depts[1][j],lotqty , "",    today,   "",        "",   ""         ,laborvalues[0][j+1] , laborvalues[0][j+1]  , ""   ,  depts[4][j], today,    color, extra]);    //'=VLOOKUP($C'+(billlastrow+1)+',Completed!$C:J,8,0)'
    crangearray.push([ 'M'+(billlastrow+k+1) ]  ); //contractor drop down cell
    prangearray.push([ 'E'+(billlastrow+k+1)+':P'+(billlastrow+k+1)]); //range to be protected
    vrangearray.push([ "Contractor!"+depts[2][j] ]); //valid contractor list
    erangearray.push([ depts[3][j] ]); //valid emails for giving editing rights
  }//if section=""
} // end for 34
//insert full bill at once
billsheet.getRange("A"+(billlastrow+1)+":T"+(billlastrow+(billarray.length))).setValues(billarray);
//set drop down for contractors
var destinationRange = billsheet.getRange(crangearray);
var sourceRange = contractorsheet.getRange(vrangearray);
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(sourceRange).build();
var rules = destinationRange.getDataValidations();
for (var i = 0; i < rules.length; i++) {
   for (var j = 0; j < rules[i].length; j++) {
     rules[i][j] = rule;
   }
}
destinationRange.setDataValidations(rules);
//set protection for different production people
var prange = billsheet.getRange(prangearray);
var protection = prange.protect();
protection.removeEditors(protection.getEditors());
protection.addEditor(erangearray);  

在下面的行中抛出错误:

var destinationRange = billsheet.getRange(crangearray);

我制作了电子表格的副本:

https://docs.google.com/spreadsheets/d/1bdO6IdEcIxkCwlW2o-93jpy7BZYb8nrFDE00u-VzQ4Y/edit?usp=sharing

任何想法如何针对不同的行使用不同的规则保护和设置验证范围。

1 个答案:

答案 0 :(得分:0)

方法Range.setDataValidation(rule) https://developers.google.com/apps-script/reference/spreadsheet/data-validation实际上必须应用于范围,但是没有理由不能将单行定义为范围。您代码中的问题是另一种问题-范围定义的实现。必须根据A1表示法定义范围。

如果您记录crangearray变量,您将意识到

Logger.log(crangearray);

有输出

[[M152], [M153], [M154], [M155], [M156], [M157], [M158], [M159], [M160], [M161], [M162], [M163], [M164], [M165], [M166], [M167], [M168], [M169], [M170], [M171], [M172], [M173], [M174], [M175], [M176], [M177], [M178], [M179]]

这不是getRange()的有效参数,因此会给您一个错误

var destinationRange = billsheet.getRange(crangearray);

您应该更改代码并为crangearray分配值“M152:M179”(不要忘记双引号!),以使函数正常工作。