1

I'm trying to set a dynamic dropdown in Google Sheet using Apps Script. I managed to get most parts working except setting the data validation in the cells necessary:

function sheetByName(ssId, sheetName) {
  var ss = SpreadsheetApp.openById(ssId);
  var sheet = ss.getSheetByName(sheetName);

  return sheet;
};

function columnByName(sheet, columnName) {
  var data = sheet.getDataRange().getValues();
  var column = data[0].indexOf(columnName);

  return column;
};

function columnValues(sheet, index) {
  var data = sheet.getDataRange().getValues();
  var values = [];
  
  for(n=1; n<data.length; ++n) {
    values.push(data[n][index]);
  }

  return values;
}

function columnSetDataValidation(sheet, index, options) {
  var data = sheet.getDataRange().getValues();
  var rule = SpreadsheetApp.newDataValidation()
    .requireValueInList(options)
    .setAllowInvalid(true)
    .build();

  for(n=1; n<data.length; ++n) {
    var cell = data[n][index];
  };
};

function dropDownBedrijven() {
  var sheetCollegas = sheetByName("<<ID HERE>>", "Collegas");
  var sheetBedrijven = sheetByName("<<ID HERE>>", "Bedrijven");
  
  var getColumnIndexInBedijven = columnByName(sheetBedrijven, "Bedrijf");
  var getColumnIndexInCollegas = columnByName(sheetCollegas, "Bedrijf");

  var bedrijven = columnValues(sheetBedrijven, getColumnIndexInBedijven).filter(item => item);  

  columnSetDataValidation(sheetCollegas, getColumnIndexInCollegas, bedrijven);
  
};

I can't manage to get the function columnSetDataValidation to set data validation in the required cells.

Do you have any idea how to go about it?

1
  • Is the sheet blank if so the getDataRange() get's nothing Commented Apr 9, 2021 at 1:06

2 Answers 2

2

You need to use range.setDataValidation(rule) with a range.

In your function columnSetDataValidation you are correctly building the rule, but are failing to assign the rule to a range. You are looping over the values of the range and then changing the value of var cell until the loop ends. Nowhere did you call range.setDataValidation(rule).

Try the following solution:

function columnSetDataValidation(sheet, index, options) {
  var range = sheet.getDataRange();
  var rule = SpreadsheetApp.newDataValidation()
    .requireValueInList(options)
    .setAllowInvalid(true)
    .build();

  for(n = 1; n < range.getLastRow(); ++n) {
    var cell = range.getCell(n,index);
    cell.setDataValidation(rule);
  };
};

References:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, works like a charm! I made minor adjustments to get the result I was seeking for. But you really helped me out.
0

Try this:

function columnSetDataValidation() {
  const ss=SpreadsheetApp.getActive();
  const sheet=ss.getSheetByName('Sheet1');
  const range = sheet.getRange(2,4,sheet.getLastRow()-1);//putting validation in column 4
  const options=[1,2,3,4,5];
  const rule = SpreadsheetApp.newDataValidation()
    .requireValueInList(options)
    .setAllowInvalid(true)
    .build();
  range.setDataValidation(rule);
}

1 Comment

Thanks for looking into this. I went with the solution Aerials posted because it fit my purpose precisely.

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.