Last active
August 28, 2024 01:55
-
-
Save terrancesnyder/95abf1e01b25b0e39572782d6042cadc to your computer and use it in GitHub Desktop.
Google Form Script to Update Dropdown from Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Main function to run to update our forms. | |
*/ | |
function main(formID) { | |
var dropdown = findDropdown_(formID || '1l3Txklywu6AF6jfkzMRPccdzpsOkhHLoOhNol9OwVV4', 'Agreement Group') | |
dropdown.setChoiceValues(getAgreementGroups_()); | |
} | |
/** | |
* Get the latest agreement groups available to select from. | |
*/ | |
function getAgreementGroups_() { | |
const sheetID = '1_yP2uvYs-eqaVx2VpWUK2-GNKLUWXAsYXF8ziEvKqNE'; | |
// Fetch data from a Google Sheet | |
var sheet = SpreadsheetApp.openById(sheetID).getSheetByName('Groups'); | |
var values = sheet.getRange('A2:A').getValues(); // Assumes data is in column A and has a header (A2-->A***) | |
var labels = sheet.getRange('B2:B').getValues(); | |
// Convert 2D array to 1D array and remove empty values | |
var choices = values.flat().filter(String); | |
return choices; | |
} | |
/** | |
* Given a form find a dropdown that is given the specific title. | |
*/ | |
function findDropdown_(formID, title) { | |
var form = FormApp.openById(formID); | |
var formDropdowns = form.getItems(FormApp.ItemType.LIST); | |
var matches = formDropdowns.filter((v) => { | |
return v.getTitle() == title; | |
}); | |
if (matches.length <= 0) { | |
throw 'Could not find match'; | |
} | |
var input = matches[0]; | |
console.log(input); | |
return input.asListItem(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created for Library
deployment ID: AKfycbyKz8Oja5ZZvt_2us-_Jgzko07XiheLZPtRCuEOFDE4WgwFUbsYmEH2copCKKoGbk-jlg
URL: https://script.google.com/macros/library/d/198wn8krKB12UQM8s10D5DWIKjNc1fnqNELdmFp_hq-MQ45-wSZTB0t_3/1