3

I'm trying this script to create data validation but it's always doesn't work

   function test_validation()
      {
      var Spread = SpreadsheetApp.getActiveSpreadsheet();
      var Sheet = Spread.getSheetByName('Sheet1'); 
      var validate1 = Sheet.getRange("A1").getDataValidation();
      Logger.log(validate1.getCriteria());
      Logger.log(validate1.getCriteriaValues());
      var option = new Array();
      option[0]="true";
      option[1]="1";
      option[2]="2";
      option[3]="3";
      var criteria="ITEM_IN_LIST";
      var helpText="Test Help Text"
      var Combobox = Sheet.getRange("A2").getDataValidation();
      //The line below display error 
      //"Cannot find method  (class)setCriteria(string,String[]). "
      Combobox.setCriteria(criteria,option);
      Combobox.setHelpText(helpText);
      var validate2 = Sheet.getRange("A2").setDataValidation(Combobox);
      Logger.log(Sheet.getRange("A2").getDataValidation().getCriteria());
      Logger.log(Sheet.getRange("A2").getDataValidation().getCriteriaValues());
       }

3 Answers 3

7

By now, some functions like getDataValidation() are not working anymore, instead use:

function test()
    {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
    var option = new Array();
    option[0]="0";
    option[1]="1";
    option[2]="2";
    option[3]="3";
    var dv = SpreadsheetApp.newDataValidation();
    dv.setAllowInvalid(false);
    dv.setHelpText("Some help text here");
    dv.requireValueInList(option, true);
    sheet.getRange("A3").setDataValidation(dv);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I found the answer finally in issue 2680

the working code

function test()
{
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
    var option = new Array();
    option[0]="0";
    option[1]="1";
    option[2]="2";
    option[3]="3";
    var dv = sheet.getRange("A3").getDataValidation();
    dv.setAllowInvalidData(false);
    dv.setHelpText("Some help text here");
    dv.setCriteria(SpreadsheetApp.DataValidationCriteria.ITEM_IN_LIST,true,option );
    sheet.getRange("A3").setDataValidation(dv);
}

1 Comment

1

Working example from 2021:

function set_data_validation() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var dest_range = ss.getActiveSheet().getRange('A1:A10');
  var list_range = ss.getSheetByName('List').getRange('A1:A');
  var rule = SpreadsheetApp.newDataValidation()
                           .requireValueInRange(list_range)
                           .setAllowInvalid(false)
                           .build();
  var rules = dest_range.getDataValidations().map(x=>[rule]);
  dest_range.setDataValidations(rules);
}

It will set data validation 'List from a range' for cells 'A1:A10' on current sheet. The range is column 'A' on a sheet with name 'List'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.