Skip to content

Instantly share code, notes, and snippets.

function appointmentSlots() {
// get spreadsheet data
var ss = SpreadsheetApp.getActiveSpreadsheet();
var configSheet = ss.getSheetByName('Config');
var configData = configSheet.getDataRange().getValues();
var optionsSheet = ss.getSheetByName('Options');
var optionsData = optionsSheet.getDataRange().getValues();
// get Form Url from spreadsheet
var formUrl = configData[0][1];
Logger.log('formUrl is: ' + formUrl);
// open Form
var form = FormApp.openByUrl(formUrl);
/* create emtpy array to push only available Options into for
updating Form */
var options = [];
// loop through available slots and push into array *************
var optionsDataLength = optionsData.length;
for (var i=1; i<optionsDataLength; i++) {
var choice = optionsData[i][0];
var left = optionsData[i][2];
// check if slot not blank and the option available (not been used)
if ((choice != '') && (left > 0)) {
options.push(choice); // add to array
}
}
// end of loop through available slots and push into array ******
// set item type to Multiple Choice for accessing Form Options
var formList = FormApp.ItemType.MULTIPLE_CHOICE;
// access Form Options list
var formItems = form.getItems(formList);
// access first list on Form and rewrite all options to ones that are available
formItems[0].asMultipleChoiceItem().setChoiceValues(options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment