I am using the below code to populate options on Google Forms from Google Sheets. And It is working. The issue I am facing is that options are populated/updated when I refresh/reopen the edit mode of Form after changing options in Google Sheet. I want that Form automatically populate/update the options from Google Sheets when the view/submission mode of the form is refreshed or reopened.
{
populateQuestions();
}
function populateQuestions() {
var form = FormApp.getActiveForm();
var googleSheetsQuestions = getQuestionValues();
var itemsArray = form.getItems();
itemsArray.forEach(function(item){
googleSheetsQuestions[0].forEach(function(header_value, header_index) {
if(header_value == item.getTitle())
{
var choiceArray = [];
for(j = 1; j < googleSheetsQuestions.length; j++)
{
(googleSheetsQuestions[j][header_index] != '') ? choiceArray.push(googleSheetsQuestions[j][header_index]) : null;
}
//item.asMultipleChoiceItem().setChoiceValues(choiceArray);
// If using Dropdown Questions use line below instead of line above.
item.asListItem().setChoiceValues(choiceArray);
}
});
});
}
function getQuestionValues() {
var ss= SpreadsheetApp.openById('1VUxlMl100R33lfbiiZ8JiNIQjiqW-SyvCQc197Vkc4k');
var questionSheet = ss.getSheetByName('Services');
var returnData = questionSheet.getDataRange().getValues();
return returnData;
}