I created a google form with a drop-down question and I wanted to auto-populate the drop-down list using a specific column in the attached spreadsheet.
I searched the web for solutions and I found two effective ways: An Add-on called the "Form Ranger" and a code to add to the sheet script editor
I tried the Add-on first for easier and faster installation and it worked well with one major problem is that it takes much time to update the list in my form which was unacceptable so I decided rather add the below code to the sheet script editor:
function updateForm(){
// call your form and connect to the drop-down item
var form = FormApp.openById("Your Form ID");
var namesList = form.getItemById("The Drop-Down List ID").asListItem();
// identify the sheet where the data resides needed to populate the drop-down
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("Name of Sheet in Spreadsheet");
// grab the values in the first column of the sheet - use 2 to skip header row
var namesValues = names.getRange(2, 1, names.getMaxRows() - 1).getValues();
var studentNames = [];
// convert the array ignoring empty cells
for(var i = 0; i < namesValues.length; i++)
if(namesValues[i][0] != "")
studentNames[i] = namesValues[i][0];
// populate the drop-down with the array data
namesList.setChoiceValues(studentNames);
}
The above code worked much better because it updates the form right away on submission with only two problems:
- Instead of removing the option completely, it replaces the option with "NOT_FOUND"
- It doesn't remove the last option at all; imagine I have 7 options so it works perfectly with only six options but never removes the entire list options at all
So if anyone can please help me get the job done in a better way, I will be very much grateful