代码中的数据验证(Google表格)

时间:2014-02-11 15:24:50

标签: google-sheets validation

我有以下脚本在'onEdit'触发器上运行(该部分工作正常)

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var myRange = SpreadsheetApp.getActiveRange();

  if(sheet.getName() == "Plan" && myRange.getColumn() == 2 && myRange.getRow() > 3){
    var option = new Array();
    option[0]="0";
    option[1]="1";
    option[2]="2";
    var dv = sheet.getRange(myRange.getRow(),myRange.getColumn() + 1).getValidation();
    dv.setAllowInvalidData(false);
    dv.setHelpText("Some help text here");
    dv.setCriteria(SpreadsheetApp.DataValidationCriteria.ITEM_IN_LIST,true,option );
    sheet.getRange(myRange.getRow(),myRange.getColumn() + 1) .setValidation(dv); 
  }
}

大多数代码来自this问题的答案。问题是

dv.setCriteria(SpreadsheetApp.DataValidationCriteria.ITEM_IN_LIST,true,option );

代码行不起作用,编译器甚至不会让我保存它。在我看看为什么,似乎谷歌已经改变了它的处理方式,并脱机了他们的文档。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

是的,似乎有些事情发生了变化......

试试:

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var myRange = SpreadsheetApp.getActiveRange();

  if(sheet.getName() == "Plan" && myRange.getColumn() == 2 && myRange.getRow() > 3){
    var option = new Array();
    option[0]="0";
    option[1]="1";
    option[2]="2";
    //    var dv = sheet.getRange(myRange.getRow(),myRange.getColumn() + 1).getValidation();
    var dv = sheet.getRange(myRange.getRow(),myRange.getColumn() + 1).getDataValidation();
    var dv = SpreadsheetApp.newDataValidation();
    //    dv.setAllowInvalidData(false);
    dv.setAllowInvalid(false);  
    dv.setHelpText("Some help text here");
    dv.requireValueInList(option, true);
//    dv.setCriteria(SpreadsheetApp.DataValidationCriteria.ITEM_IN_LIST,true,option );
//    sheet.getRange(myRange.getRow(),myRange.getColumn() + 1).setValidation(dv); 
    sheet.getRange(myRange.getRow(),myRange.getColumn() + 1).setDataValidation(dv.build()); 
  }
}