Save user choice into a variable?

124 views
Skip to first unread message

john s.

unread,
Nov 9, 2019, 12:58:23 PM11/9/19
to Google Apps Script Community
Hi, I've finished my beginner-level javascript classes and there's a project I'd like to do using google script/form. I was wondering if it was possible to save a user's choice made in a dropdown list into a variable. I would like to use that variable within the form before it gets submitted.

For example:

function testListToFive(){
  var form = FormApp.create('Numbers 1 to 5');
  form.setDescription('Choose from 1 or 2')
 
  var programName = form.addTextItem();
  programName.setTitle('Choose a number');

  var targetNumber = form.addListItem();
  var arr = [];
  targetNumber.setTitle('How many targets?')
  for (var i = 1; i <= 5; i++) {
    arr.push(targetNumber.createChoice(i));
  }
  targetNumber.setChoices(arr);

  Logger.log('Published URL: ' + form.getPublishedUrl());
  Logger.log('Editor URL: ' + form.getEditUrl());
}

If I chose 3 from the dropdown list, how do I store it in a variable?

CBMServices Web

unread,
Nov 9, 2019, 2:09:50 PM11/9/19
to google-apps-sc...@googlegroups.com
Congratulations of finishing the beginner classes.

As far as I know, google forms do not permit you to dynamically modify the form based on entry.

There is limited support where you can check entries for values or direct the user to a different section of questions based on his answer. But you can not add/delete form questions or modify questions on the fly based on his answers.

If you want to do post processing of the form submission post entry, then I would suggest you save the submissions to a spreadsheet, then trigger the spreadsheet script to go through the answers to process them.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/22df8599-5817-49b1-9463-50c07909af90%40googlegroups.com.

Clark Lind

unread,
Nov 11, 2019, 7:46:52 AM11/11/19
to Google Apps Script Community
Another option is to create your own form via the Html services, and you can interact with it directly using javascript.

john s.

unread,
Nov 14, 2019, 9:24:00 AM11/14/19
to Google Apps Script Community
This is the code that I went with so far. It allows me to choose from the numbers 1 to 20 from a dropdown list and it will generate a corresponding page based on that number.

function testList(){
  var form = FormApp.create('Choose number');
  form.setDescription('Choose from 1 to 20'); 
 
  var programName = form.addTextItem();
  programName.setTitle('Choose a number');

  var targetNumber = form.addListItem().setTitle('How many targets?');
  var pages = [];
  for (var i=0; i<20; i++) {
    pages.push(form.addPageBreakItem().setTitle('Page ' + (i+1)));
  }

  var choices = [];
  for (var i=0; i<20; i++) {
    choices.push(targetNumber.createChoice((i+1), pages[i]));
  }
  targetNumber.setChoices(choices);

  Logger.log('Published URL: ' + form.getPublishedUrl());
  Logger.log('Editor URL: ' + form.getEditUrl());
}

I'll have to check out creating dynamic google forms using HTML services. Looks interesting and I'll keep improving my code.

Clark Lind

unread,
Nov 14, 2019, 3:10:11 PM11/14/19
to Google Apps Script Community
Not sure if this helps at all. Martin Hawksey had a post last year about Creating custom branching in Google Forms with Google Apps Script. It might do what you need done. It is very similar anyway :)
Reply all
Reply to author
Forward
0 new messages