Abhi
Abhi

Reputation: 177

How to Edit existing google form item (question) using google Apps Script

I have a Google script to construct a google form. The script fills the form using a Spreadsheet which contains the questions and corresponding options.

The question displayed in the form needs to be updated at regular intervals. I hope to update the question in the form by changing the question in the Spreadsheet as follows:

  1. I use onOpen() for the script, so that each time the form is accessed the script reconstructs the most updated form.

However, currently each time I run the Script a new question is added to the form and the previous outdated questions still stay on the form. I need to edit the existing question on the Form using the script to update it to show the latest question. I could not find a way to edit an existing form question using google script. Anyone know how?

The question and options are updated in the spreadsheet at regular intervals.

I want the script to be able to edit the question automatically. All my attempts to find a function which is able to edit an already available question on a Form have been in vain! (PS : All functions I found are able to create a new question and its options/ but not able to edit an existing form question/option)

Upvotes: 5

Views: 12573

Answers (1)

Alan Wells
Alan Wells

Reputation: 31300

In order to update an existing item (question), the code must first get that item by it's item type, and there are many different methods for getting items by their type.

There is a different method for each type of question. The types of questions are:

  • CHECKBOX
  • DATE
  • DATETIME
  • DURATION
  • GRID
  • LIST
  • MULTIPLE_CHOICE
  • PARAGRAPH_TEXT
  • SCALE
  • TEXT
  • TIME

In order to update an existing item, the code must first get that item by it's item type. Here are some examples:

  • asCheckboxItem()
  • asDateItem()
  • asListItem()
  • Etc

For example:

var myCheckBoxItem = FormApp.openById(id).getItemById(id).asCheckboxItem();

Once the code has obtained an item as the correct item, you can change it the same way that you created it in the first place.

function editFormItem() {
  var form = FormApp.getActiveForm();
  var allItems = form.getItems();
  var i,
      L=0,
      thisItem,
      thisItemType,
      myCheckBoxItem;
  
  L = allItems.length;

  for (i=0;i<L;i++) {
    thisItem = allItems[i];
    thisItemType = thisItem.getType();
    //Logger.log('thisItemType: ' + thisItemType);
    
    if (thisItemType===FormApp.ItemType.CHECKBOX) {
      myCheckBoxItem = thisItem.asCheckboxItem();
      myCheckBoxItem.setChoiceValues(values)
    };
  };
};

The above script is not complete. You need to somehow match up what item goes with the new changes. If all your Form questions are the same item type, then you won't need to test for what the item type is.

There are 3 item types that get returned by getItems() that are not question items. They are:

  • IMAGE
  • PAGE_BREAK
  • SECTION_HEADER

So, if you have any of those 3 in your form, you should check the item type.

Upvotes: 11

Related Questions