Amit
Amit

Reputation: 11

Google Apps Script - updating Google Forms

I try to update a Google Form from Google Sheets. There is one type of question that I can't find how to activate using Google Apps Script: 'Multiple choice grid'

As you can see in the picture, the is only one option for 'multiple choice'.

I found this article that presents the available types, maybe it can assist you: https://developers.google.com/apps-script/reference/forms/item-type

CODE VERSION #2:

    const populateGoogleForms1 = () => {
      const GOOGLE_SHEET_NAME = 'sheet_name';
      const GOOGLE_FORM_ID = 'form_path';
      const ss = SpreadsheetApp.getActiveSpreadsheet();
    
  const [header, ...data] = ss
    .getSheetByName(GOOGLE_SHEET_NAME)
    .getDataRange()
    .getDisplayValues();

  const choices = {};
  header.forEach((title, i) => {
    choices[title] = data.map((d) => d[i]).filter((e) => e);
  });

  FormApp.openById(GOOGLE_FORM_ID)
    .getItems()
    .map((item) => ({
      item,
      values: choices[item.getTitle()],
    }))
    .filter((values) => values)
    .forEach(({ item, values }) => {
      switch (item.getType()) {


case FormApp.ItemType.CheckboxGrid:
item.asCheckboxGridItem().setChoiceValues(values);
break;
case FormApp.ItemType.Checkbox:
item.asCheckboxItem().setChoiceValues(values);
break;
case FormApp.ItemType.Grid:
item.asGridItem().setChoiceValues(values);
break;
case FormApp.ItemType.List:
item.asListItem().setChoiceValues(values);
break;
case FormApp.ItemType.MultipleChoice:
item.asMultipleChoiceItem().setChoiceValues(values);
break;
case FormApp.ItemType.MultipleChoiceGrid:
item.asMultipleChoiceGridItem().setChoiceValues(values);
break;



        default:
        // ignore item
      }
    });
  ss.toast('Google Form Updated !!');
};  

Upvotes: 1

Views: 1608

Answers (1)

Kristkun
Kristkun

Reputation: 5963

There are only 2 Grid options available in Google Forms, multiple choice grid and checkbox grid.

If you want to get the type of the multiple choice grid, use GRID property

GRID

A question item, presented as a grid of columns and rows, that allows the respondent to select one choice per row from a sequence of radio buttons.

I created a temporary form having multiple choice grid and checkbox grid only, I tried to log the form's items and the results are as follow:

4:25:02 AM  Notice  Execution started
4:25:04 AM  Info    GRID
4:25:04 AM  Info    CHECKBOX_GRID
4:25:03 AM  Notice  Execution completed

UPDATE

When you use asCheckboxGridItem(),It will return a CheckboxGridItem Class which contains all methods available for that class, setChoiceValues() is not available in CheckboxGridItem Class. You need to use setColumns(columns) for the choices or setRows(rows) for the questions.

Sample Code:

function updateForm() {
  var form = FormApp.openById("1AFCnzOrsdJfZ5VZIdVahVZvMdbXm3fOIZnGxxxx");
  var items = form.getItems();
  var questions = ["Question1", "Question2"];
  var answers = ["True", "False"];
  items.forEach(item =>{
    //var choices = item.asListItem().getChoices();
    //var choices = item.asGridItem();

    switch(item.getType()){
      case FormApp.ItemType.GRID:
        var choices = item.asGridItem();
        choices.setColumns(answers);
        choices.setRows(questions);
        break;
      case FormApp.ItemType.CHECKBOX_GRID:
        var choices = item.asCheckboxGridItem();
        choices.setColumns(answers);
        choices.setRows(questions);
        break;
    }
  });
}

OUTPUT:

Before:

enter image description here

After:

enter image description here

Upvotes: 2

Related Questions