Skip to content

Instantly share code, notes, and snippets.

@davegurnell
Last active November 7, 2021 13:19
Show Gist options
  • Select an option

  • Save davegurnell/f40f7c4081e1aee64e8a94afca0511c2 to your computer and use it in GitHub Desktop.

Select an option

Save davegurnell/f40f7c4081e1aee64e8a94afca0511c2 to your computer and use it in GitHub Desktop.
Script to copy the responses from a text field in one Google Form as choices in a multiple choice field in another Google Form
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
START OF CONFIGURATION
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~
Configuration: Form URLs
~~~~~~~~~~~~~~~~~~~~~~~~
You can get these from the address bar
when you're editing the source and destination forms.
To refer to the *current* form (the one you've attached the script to),
you can write:
var blahUrl = FormApp.getActiveForm().getEditUrl()
TO refer to a different form (for example the projects form),
you can enter the URL of the "edit form" page in Google Drive:
var blahUrl = "https://docs.google.com/forms/d/abcdefghijklmnopqrstuvwxyz/edit"
Note: there should be double quotes around the URL in the second example
but no double quotes in the first example.
*/
var organisationFormUrl = FormApp.getActiveForm().getEditUrl()
var projectFormUrl = "https://docs.google.com/forms/d/16Nt9Z0x6c8CxoihFvrwyIJMfKxzcafptO0cE37sPcaU/edit"
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Configuration: Item Indexes
~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Items" are fields in the forms. We need to specify:
- which item to copy from on the organisation form;
- which item to copy to on the project form.
We specify items using their "indexes" (numeric positions) on the form.
Note that computers count from 0, so:
- the first item on a form is item 0;
- the second item is item 1;
- and so on.
BE CAREFUL: If you reorder the items on either form,
you must edit the values below to reflect the new indexes.
If you don't, the script will fail OR it may overwrite the wrong item.
*/
var organisationItemIndex = 12
var projectItemIndex = 0
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
END OF CONFIGURATION -- EDIT BELOW AT YOUR OWN RISK
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~
Code: Script entry point
~~~~~~~~~~~~~~~~~~~~~~~~
This is where we kick everything off.
The implementation below copies the responses from the "organisation item"
and overwrites the choices in the "project item".
You can copy multiple things if you want by duplicating this line
and replacing the parameters between the parentheses as appropriate.
*/
function onSubmit() {
copyResponsesToChoices(organisationFormUrl, organisationItemIndex, projectFormUrl, projectItemIndex)
}
/*
~~~~~~~~~~~~~~~~~~~
Code: Copy function
~~~~~~~~~~~~~~~~~~~
This function implements the majority of the functionality of the script.
The assumptions are:
- the "source form" and "destination form" both exist and are owned by you;
- the "source item" exists and is a text or paragraph item;
- the "destination item" exists is a checkbox list, list, or multiple choice item.
The function will *replace* the options in the destination field
with the text responses from the source field.
*/
function copyResponsesToChoices(srcFormUrl, srcItemIndex, desFormUrl, desItemIndex) {
var srcForm = FormApp.openByUrl(srcFormUrl)
var desForm = FormApp.openByUrl(desFormUrl)
if(srcForm == null) throw new Error("Source form not found: " + srcFormUrl)
if(desForm == null) throw new Error("Destination form not found: " + desFormUrl)
var srcItems = srcForm.getItems()
var desItems = desForm.getItems()
// For debugging purposes:
// var srcTitles = srcItems.map(function (item) { return item.getTitle() });
// var desTitles = desItems.map(function (item) { return item.getTitle() });
var srcItem = srcItems[srcItemIndex]
var desItem = desItems[desItemIndex]
if(srcItem == null) throw new Error("Source item not found")
if(desItem == null) throw new Error("Destination item not found")
switch(srcItem.getType()) {
case FormApp.ItemType.TEXT:
case FormApp.ItemType.PARAGRAPH_TEXT:
var options = srcForm.getResponses()
.map(function(response) { return response.getResponseForItem(srcItem).getResponse() })
.sort()
break
default:
throw new Error("Source item must be a text or paragraph item: " + srcItem.getType())
}
if(options.length == 0) {
throw new Error("The source item has no responses")
}
switch(desItem.getType()) {
case FormApp.ItemType.CHECKBOX:
desItem.asCheckboxItem().setChoiceValues(options)
break
case FormApp.ItemType.LIST:
desItem.asListItem().setChoiceValues(options)
break
case FormApp.ItemType.MULTIPLE_CHOICE:
desItem.asMultipleChoiceItem().setChoiceValues(options)
break
default:
throw new Error("Destination item must be a multiple choice item: " + desItem.getType())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment