Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 989
Abhi Avatar asked Oct 23 '15 18:10

Abhi


People also ask

How do I edit a Google form already made?

To make edits, open your form in Google Forms and select an existing question or answer choice to edit or add a new question, section, image, video & more. Whenever you make the edits, the changes will be saved instantly and it will reflected in the form whenever the user opens the form.

Is there an app to edit Google forms?

The answer is simple. Take the help of the Google Drive app. All your forms are saved in Google Drive. Open the Google Drive app on your mobile device and tap on the form you want to edit.


1 Answers

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.

like image 113
Alan Wells Avatar answered Sep 30 '22 17:09

Alan Wells