PSAB Plalangan
PSAB Plalangan

Reputation: 355

Choices Set To a Section, Next Section, or Submit Form in Google Forms

I want to generate Google Forms with Google Apps Script from data in a spreadsheet. The question is a multiple-choice question, and what I'd like to do is add the choices variable to it and have those choices set to a section, Next Section, or Submit Response in Google Forms. To set choices to a section, where every section in Forms have their ID, I can do the settings, as in the post here.

I'm still having trouble set choices to Next Section, or Submit Form, because I haven't found a way to recognize each ID.

The sample data sheet contain Text Choices and Sections ID looks like this : LIST TEXT CHOICES AND SECTIONS ID

The sample form looks like this: SAMPLE FORM

function editForm() {
  var form               = FormApp.openById("MY FORM ID");
  var multipleChoiceQstn = form.getItemById(1288364781).asMultipleChoiceItem();
  var spreadsheet        = SpreadsheetApp.openById("MY SPREADSHEET ID");
  var sheet              = spreadsheet.getSheetByName("MY SHEET NAME");
  var choices            = sheet.getRange(2, 2, 9, 1).getValues();
  var questionsId        = sheet.getRange(2, 3, 9, 1).getValues();
  var formChoices        = [];

 for (var k = 0; k < choices.length; k++) {
      var choice = choices[k][0];
      var questionId = questionsId[k][0];
      var sectionQuest = form.getItemById(questionId).asPageBreakItem();
    
  formChoices.push(multipleChoiceQstn.createChoice(choice, sectionQuest))
}
multipleChoiceQstn.setChoices(formChoices);
};

Until now I am still having difficulty determining the method for setting choices number 1 and 2, so that they can be set to Submit Form or set to Continue to next section.

Any help would be greatly appreciated.

Upvotes: 0

Views: 514

Answers (1)

Century Tuna
Century Tuna

Reputation: 1762

SUGGESTION:

You could do it like this:

function editForm() {
  var form = FormApp.openById("MY FORM ID");
  var multipleChoiceQstn = form.getItemById(1288364781).asMultipleChoiceItem();
  var spreadsheet = SpreadsheetApp.openById("MY SPREADSHEET ID");
  var sheet = spreadsheet.getSheetByName("MY SHEET NAME");
  var choices = sheet.getRange(2, 2, 9, 1).getValues();
  var questionsId = sheet.getRange(2, 3, 9, 1).getValues();
  var formChoices = [];

  formChoices.push(multipleChoiceQstn.createChoice("Don't Have Choice - Send Responses", FormApp.PageNavigationType.SUBMIT));
  formChoices.push(multipleChoiceQstn.createChoice('Go To Next Section', FormApp.PageNavigationType.CONTINUE));

  for (var k = 2; k < choices.length; k++) {
    var choice = choices[k][0];
    var questionId = questionsId[k][0];
    var sectionQuest = form.getItemById(questionId).asPageBreakItem();

    formChoices.push(multipleChoiceQstn.createChoice(choice, sectionQuest));
  }
  multipleChoiceQstn.setChoices(formChoices);
}

Added the following to your source code:

  formChoices.push(multipleChoiceQstn.createChoice("Don't Have Choice - Send Responses", FormApp.PageNavigationType.SUBMIT));
  formChoices.push(multipleChoiceQstn.createChoice('Go To Next Section', FormApp.PageNavigationType.CONTINUE));

Sample output:

image

Upvotes: 1

Related Questions