I looked through all of the questions, and I can't quite figure out what I am doing wrong. Here is the background:
I created a spreadsheet that has multiple tabs. Each tab corresponds to types of events (e.g parades, festivals, sports). Each of these tabs is used to assign specific job numbers to a specific event. Parade job numbers are in the 2000 range, festivals 3000 range, sports 4000 range.
I then have a different tab which defines an array that collects all of the job numbers and events, and sorts them alphabetically. This tab is called "Active_Job_Numbers".
My goal is to create several forms that call to this spreadsheet for the values on the "Active_Job_Numbers" tab. I sort of have this working, but not completely. Each form has a purpose such as submitting a request to accounting to process a credit card transaction; another form to submit a request to cut a check.
All of these requests have to be tied to an ever changing list of job numbers. When I update the spreadsheet, the dropdown in the spreadsheet will populate based on the "Active_Job_Numbers" data.
The code that I am using does work, BUT if I update a job number or event name on the spreadsheet, the form still sees the old value. I have triggers set up for onSubmit and onOpen and still, the only way I can get the dropdown in the form to update is if I change one of the "var" labels. Then save it. Then reopen the form. This is a showstopper.
Here is the code I am using:
function updateForm(){
// call your form and connect to the drop-down item
var creditCardRequestGoogleForm = FormApp.openById("1LqtPLGEhocGdkg75mkdldfplxSxWJmYZCp3u1bMwVQQ");
var jobNumberDropDown = creditCardRequestGoogleForm.getItemById("1632374512").asListItem();
// identify the sheet where the data resides needed to populate the drop-down
var ss = SpreadsheetApp.openById("17fgel9Jfl4Aske85mfkwsphBQpZgtGhR8Q");
SpreadsheetApp.setActiveSpreadsheet(ss);
var activeJobNumbers = ss.getSheetByName('Active_Job_Numbers');
// grab the values in the first column of the sheet - use 2 to skip header row
var jobNumValues = activeJobNumbers.getRange(2, 1, activeJobNumbers.getMaxRows() - 1).getValues();
var jobNums = [];
// convert the array ignoring empty cells
for(var i = 0; i < jobNumValues.length; i++)
if(jobNumValues[i][0] != "")
jobNums[i] = jobNumValues[i][0];
// populate the drop-down with the array data
jobNumberDropDown.setChoiceValues(jobNums);
}
A separate but related problem...there are some forms I have that have two different drop-down elements that call to the same spreadsheet but to different tabs. I am not sure how to group this script so I have to separated out like so:
function updateForm(){
// call your form and connect to the drop-down item
var ccrForm = FormApp.openById("1r5n6Sr7fhtGCqht_QHy9qWR1v2ESabxdE-bMxjR-M");
var jobNumberDropDown = ccrForm.getItemById("1678955392").asListItem();
// identify the sheet where the data resides needed to populate the drop-down
var ss = SpreadsheetApp.openById("17eGAlcnBB7ejdfqp3HBQBK9wJAKWYhBQpZgtGhR8Q");
SpreadsheetApp.setActiveSpreadsheet(ss);
var currentJobNumbers = ss.getSheetByName('Active Job Numbers');
// grab the values in the first column of the sheet - use 2 to skip header row
var jobNumValues = currentJobNumbers.getRange(2, 1, currentJobNumbers.getMaxRows() - 1).getValues();
var jobNums = [];
// convert the array ignoring empty cells
for(var i = 0; i < jobNumValues.length; i++)
if(jobNumValues[i][0] != "")
jobNums[i] = jobNumValues[i][0];
// populate the drop-down with the array data
jobNumberDropDown.setChoiceValues(jobNums);
}
function updateForm(){
// This call your form and connect to the drop-down item
var form = FormApp.openById("1r5n6Sr7fhtGCqht_QHy9qWR1v2ESabxdE-bMxjR-M");
var poNumberDropDown = form.getItemById("57865789991").asListItem();
// This is the second dropdown which calls the same spreadsheet but different tab NEED HELP COMBINING THESE TWO MORE ELEGANTLY
var ss = SpreadsheetApp.openById("17eGAlcnBB7ejdfqp3HBQBK9wJAKWYhBQpZgtGhR8Q");
SpreadsheetApp.setActiveSpreadsheet(ss);
var currentPONumbers = ss.getSheetByName('PO_Number');
// grab the values in the first column of the sheet - use 2 to skip header row
var poNumValues = currentPONumbers.getRange(2, 1, currentPONumbers.getMaxRows() - 1).getValues();
var poNums = [];
// convert the array ignoring empty cells
for(var i = 0; i < poNumValues.length; i++)
if(poNumValues[i][0] != "")
poNums[i] = poNumValues[i][0];
// populate the drop-down with the array data
poNumberDropDown.setChoiceValues(poNums);
}
Any insight would be much appreciated.
onOpen trigger for a form, as mentioned in documentation:
This event does not occur when a user opens a form to respond, but
rather when an editor opens the form to modify it
Instead, you can use an installable trigger to activate your code when the sheet is edited.
Furthermore, instead of modifying all form each time an edit is made on the spreadsheet. You can check for which sheet is modified in the spreadsheet to modify the corresponding form. To determine the which sheet is modified you can use event objects.
Here is example code, you can modify this for your use.
function installedEdit(evtObj){
Logger.log("Edit running")
var sheetName = evtObj.range.getSheet().getSheetName()
Logger.log(sheetName)
var itemId="None"
switch (sheetName){
case "Active Job Numbers":
itemId = "1678955392"
updateForm(sheetName,itemId)
break;
case "PO_Number":
itemId = "57865789991"
updateForm(sheetName,itemId)
break;
}
}
function updateForm(sheetName,itemId){
// call your form and connect to the drop-down item
var form = FormApp.openById("1r5n6Sr7fhtGCqht_QHy9qWR1v2ESabxdE-bMxjR-M");
var dropDown = form.getItemById(itemId).asListItem();
// identify the sheet where the data resides needed to populate the drop-down
var ss = SpreadsheetApp.openById("17eGAlcnBB7ejdfqp3HBQBK9wJAKWYhBQpZgtGhR8Q");
SpreadsheetApp.setActiveSpreadsheet(ss);
var sheet = ss.getSheetByName(sheetName);
// grab the values in the first column of the sheet - use 2 to skip header row
var choiceValues = sheet.getRange(2, 1, sheet.getLastRow() - 2).getValues();
var choices = [];
// convert the array ignoring empty cells
for(var i = 0; i < choiceValues.length; i++)
if(choiceValues[i][0] != "")
choices[i] = choiceValues[i][0];
// populate the drop-down with the array data
dropDown.setChoiceValues(choices);
}
Note: Make sure installedEdit() is set up to run when the spreadsheet is edited and is bound to the spreadsheet.
Related
My code here:
function script1(){
//What is the form and data item ID?
var form = FormApp.openById("Form ID");
var namesList = form.getItemById("Item ID").asListItem();
//What sheet is used to update the form?
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("Update");
// Grab the values in the sixth column of the sheet - use 2 to skip header row
var namesValues = names.getRange(2, 6, names.getMaxRows() - 1).getValues();
var departmentNames = [];
// Convert the array ignoring empty cells
for(var i = 0; i < namesValues.length; i++)
if(namesValues[i][0] != "")
departmentNames[i] = namesValues[i][0];
// Populate the drop-down
namesList.setChoiceValues(departmentNames);
}
appears to triggers the form to submit causing problems. I have an onFormSubmit trigger set up to copy the data to a database and then delete the row. Unfortunately, the headers keep getting copied to the database since the form does not actually send any data and the only row with data is the headers. How can I update the form without the form submitting? Screenshot example is included below.
[
I hope I was able to make myself clear. Thank you for any help.
Modified Screenshot
Here is a screenshot of the trigger and its executions.
I am looking for a script to run as soon as a range of cells (example: C2:C) change.
When the script runs I want it to call a Form and update the Form drop-down list with the new values in the cell range C2:C.
I have tried this with an installable trigger, however the trigger fires even when other tabs in the spreadsheet are changed, i.e sheet 2 etc.
function updatetestForm(){
// call your form and connect to the drop-down item
var form = FormApp.openById("formID");
var namesList = form.getItemById("12345678").asListItem();
// identify the sheet where the data resides needed to populate the drop-down
var ss = SpreadsheetApp.openById("sheetID");
var names = ss.getSheetByName("sheet1");
// grab the values in the first column of the sheet - use 2 to skip the first 1 row (limit of 1000)
var namesValues = names.getRange(2, 1, names.getLastRow() - 0).getValues();
var test = [];
// convert the array ignoring empty cells
for(var i = 0; i < namesValues.length; i++)
if(namesValues[i][0] != "")
test[i] = namesValues[i][0];
// populate the drop-down with the array data
namesList.setChoiceValues(test);
}
I tried using an installable trigger but the trigger fired when other sheet tabs changed.
You could check the event that launched the trigger. For example, by changing the title to add the e to grab the event, you can check the sheet involved. You can add this first line and wrap the rest of the function with that opening curly bracket (close at the end, obviously)
`function updatetestForm(e){
if(e.range.getSheet().getName() == "sheet1") {
You can add more conditions to check the column inside the if statement:
if(e.range.getSheet().getName() == "sheet1" && e.range.getColumn() == 3) {
And so on
Currently using the below app script function to update one column of data into one section of my google form. However, I want to write a code where I can update multiple rows of data into multiple sections of the google form.
Example: Column 1 update to Section 1, Column 2 update to Section 2 and etc..
Google Sheet - Employee Names
function updateForm(){
// call your form and connect to the drop-down item
var form = FormApp.openById("Form Id");
var namesList = form.getItemById("Dropdown List ID").asListItem();
// identify the sheet where the data resides needed to populate the drop-down
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("Sheet Name");
// grab the values in the first column of the sheet - use 2 to skip header row
var namesValues = names.getRange(2, 10, names.getMaxRows() - 1).getValues();
var shopperNames = [];
// convert the array ignoring empty cells
for(var i = 0; i < namesValues.length; i++)
if(namesValues[i][0] != "")
shopperNames[i] = namesValues[i][0];
// populate the drop-down with the array data
namesList.setChoiceValues(shopperNames);
}
For better organisation, please arrange the data as below
Name - Address
This way you can add new persons easily and change the address easily.
After that, it becomes easy to filter the range by column B and you can easily get the list of persons in the street.
ArrayFormula makes it very easy to do this
https://sites.google.com/site/scriptsexamples/custom-methods/2d-arrays-library
I have the following scenario
I currently update my form using appscript which is working fine however I have this problem , my form has question to select a country (from drop down list of countries ) and each option has each section mapped , (say if I select country as Netherlands whilst filling the form , it will take me to next section to select the town in Netherlands in the specified section ), so what happens now is whenever there is new country added in the sheet its updated in the form via below script and it works fine but it resets the section map that is set for each country , so we have to redo the section based answer again ! wanted to understand is there any way that to avoid resetting the section based options thats set.
Code
var ssID = "sheet ID"; //Global Variable sheet ID
// This is start of function1 to update form Monthly
function formUpdateCC() {
// call your form and connect to the drop-down item
var form = FormApp.openById("form ID");
var country = form.getItemById("ItemID").asListItem();
// identify the sheet where the data resides needed to populate the drop-down - Countrylist
var countrysheet = SpreadsheetApp.openById(ssID).getSheetByName("Country");
// grab the values in the first column of the country tab in sheet - use 2 to skip header row
var countrylist = countrysheet.getRange(2, 1, countrysheet.getMaxRows() - 1).getValues();
var data = [];
// convert the array ignoring empty cells
for (var i = 0; i < countrylist.length; i++)
if (countrylist[i][0] != "")
data[i] = countrylist[i][0];
country.setChoiceValues(data);
}
You can use setChoices with createChoice(value, navigationItem) instead of setChoiceValues to add navigation to choices. Create a new column (in this example, it's column B) with the section ID for the section you'd like to navigate for that country. Then use the code below to update the form.
var form = FormApp.openById("Form Id");
var country = form.getItemById("List Id").asListItem();
// identify the sheet where the data resides needed to populate the drop-down - Countrylist
var countrysheet = SpreadsheetApp.openById(ssID).getSheetByName("Country");
// create a new column next to the country name for its target pagebreak ID from the form
// grab the values in the first & second columns of the country tab in sheet - use 2 to skip header row
var countrylist = countrysheet.getRange(2, 1, countrysheet.getMaxRows() - 1).getValues();
var targetlist = countrysheet.getRange(2, 2, countrysheet.getMaxRows() - 1).getValues();
var data = [];
// convert the array ignoring empty cells
var s = 0;
for (var i = 0; i < countrylist.length; i++)
if (countrylist[i][0] != "") {
// use createChoice(value, navigationItem) to add the section navigation
data[s] = country.createChoice(countrylist[i][0], form.getItemById(targetlist[i][0]).asPageBreakItem());
s++;
}
country.setChoices(data);
I have a Google Apps Script that fetches the data in a specific column in a spreadsheet and pushes that into a drop down menu in a Google Form.
This all works perfectly.
However, I'm trying to sort the data via the script so that before pushing it to the form, it sorts it alphabetically (A --> Z).
The data in the spreadsheet is not sorted, and I cannot just sort it in the spreadsheet because data is constantly being added.
Here is the Google Apps Script that works, except for the sorting:
function updateClientForm(){
// call your form and connect to the drop-down item
var form = FormApp.openById("1qszM48QILtnf-2QZa078ypqhjYHdB-VK2c0VMJDrsXo");
//Inspect the element on the form to find the ID value
var clientnamesList = form.getItemById("449574637").asListItem();
// identify the sheet where the data resides needed to populate the drop-down
var ss = SpreadsheetApp.getActive();
var clientnames = ss.getSheetByName("Client Names with ID");
// grab the values in the first column of the sheet - use 2 to skip header row
var namesValues = clientnames.getRange(2, 1, clientnames.getMaxRows() - 1).getValues();
var customerNames = [];
// convert the array ignoring empty cells
for(var i = 0; i < namesValues.length; i++)
if(namesValues[i][0] != "")
customerNames[i] = namesValues[i][0];
// populate the drop-down with the array data
clientnamesList.setChoiceValues(customerNames);
}
Any ideas? Thanks in advance!
Google Apps Script is based on javascript, so you have access to pretty much everything you can do in javascript.
namesValues is an array and javascript arrays come with a built-in sort function
sortedNamesValues = namesValues.sort();