Reputation: 51
I have a google sheet with two columns, one corresponding to department and the second corresponding to projects. I would like to create a google form in which respondents have to select a department from the dropdown and then in the second question select a project. But I want only those projects that correspond to their selection if department to appear in the second question. I tried using Apps Script and I was able to import the first column into the dropdown for the first question. Could someone help with creating the second conditional dropdown. If that's not possible, I think google forms has an option to redirect respondents to a new section based on a response. Can this route be made automatic by coding? Thanks in advance!
Below is the code I used to create the first dropdown.
function updateDropdown() {
// Get the Google Form
var form = FormApp.getActiveForm();
// Get the target dropdown items by their indices (adjust as needed)
var firstDropdownIndex = 0; // Index of the first dropdown item in the form
var secondDropdownIndex = 1; // Index of the second dropdown item in the form
var firstDropdownItem = form.getItems()[firstDropdownIndex];
var secondDropdownItem = form.getItems()[secondDropdownIndex];
// Get the data from the Google Sheet
var sheetId = '103-vF2U3OEG6_k25IU8bn0oODXxS3XiyuJxokXfqJMU';
var sheetName = 'Sheet1'; // Replace with the actual sheet name
var sheet = SpreadsheetApp.openById(sheetId).getSheetByName(sheetName);
var data = sheet.getRange('A1:B91').getValues(); // Adjust the range as needed
// Extract the unique values from the first column for the first dropdown choices
var firstDropdownChoices = [...new Set(data.map(function(row) {
return row[0]; // Assuming the first column contains the options for the first dropdown
}))];
// Set the choices for the first dropdown item
var firstDropdown = firstDropdownItem.asListItem();
firstDropdown.setChoiceValues(firstDropdownChoices);
Upvotes: 1
Views: 4428
Reputation: 134
In this case there are no options to only show some specific options in the next question based on the answer selected before that question, however the option you have is to use the feature Show questions based on answers
basically you'll need to create multiple sections based on the departments available.
In regards to your question about using coding to implement the Show questions based on answers
feature, I've been searching through the official documentation but the only class I found to add a new section is the class SectionHeaderItem, after multiple testing on my end there's no method available to Add a section
in a Google Form as you need it, you can only add a header to the existing section.
Nevertheless, having the ability to create a new section in Google Forms through Google Apps Scripts would be a nice feature to have. If you have the time, you can open a Feature request
Upvotes: 1