gootyfer
gootyfer

Reputation: 33

Create personalized Google Forms automatically from a Google App Script

I want to create a personalised form using a Google Apps script, but couldn't find the way in the documentation. For example, the for a different user the questions would be:

... and so on

Is this possible with Google Apps Script?

Upvotes: 3

Views: 4605

Answers (2)

pashute
pashute

Reputation: 4053

Now in 2014, yes, you can make a dynamic form:

There are three places where you change the form.

  1. You can create several standard forms in advance and serve out a different form to each different user, or group of users: First make the first form. Then make a script that mails this form to the users you want, then iterate through all the items and replace the text. (See code at bottom of this post)

  2. You can create a standard form, then replace certain text each time before serving the form. This can be done with the onOpen() trigger.

Note: It could be that this does not work as documented. I was not able to use this method, and the onOpen() was working only for opening the document FOR EDITING not for FILLING. I don't want to go into this too deeply, but it seems contrary to the documentation, which states that the OPEN-FOR-EDITING limitation applies only to the installable trigger and not oto the simple trigger.

  1. After each user submits the form, in an installed trigger that runs the setQuiz() function, the trigger is set to act ON SUBMIT. Each time a user submits a response, replace the texts as in #1 above. This works, BUT, if the next user clicks before the function completed replacing all texts in the form, the next user may get parts of the form before they were replaced!!

Not a very good idea.

Here's the code for the first solution (and the third)

  function setQuiz(keyword, replacement){
    var keyword = ...
    var replacement = ...
    var form = FormApp.getActiveForm();
    var items = form.getItems();
    var txt;
    for (i=0; i<items.length; i++){
        txt = items[i].getTitle();
        txt = txt.replace(replacement, keyword);
        items[i].setTitle(txt);
        txt = "";
        txt = items[i].getHelpText();
        txt = txt.replace(replacement, keyword);
        items[i].setHelpText(txt);
     }
     return form;
     // CREATE A COPY of the form
     // and send email with link to the form's COPY in next method, which will be done, only after this method replaced all texts. (see issue with solution 3)
     // note you'll have to iterate between the replacement texts each time. So if you start out with tree to mountain, you must remember to change mountain to river... 

Upvotes: 1

megabyte1024
megabyte1024

Reputation: 8650

The short answer is in the comments. A little bit detailed answer is

  1. Create a spreadsheet. Fill a pair of columns there like the following
[email protected]      tree
[email protected]      mountain
[email protected]      river
  1. In your script: open the spreadsheet, read a range containing the filled columns to an array. Documentation here and here
  2. In your script: get the user email using the Session.getActiveUser().getEmail() function, find the email in the array.
  3. In your script: create your form with a label. The label text should be something like label.setText('Describe a "' + usersWord + '"');

Upvotes: 0

Related Questions