Search code examples
formsgoogle-apps-scriptexceptionreset

Apps script to delete all form questions except scpecific one


I have this function that resets my form. But I would like to delete all questions except the first one, so that the item ID and entry of this question remain fixed within a dynamic pre-filled link. Note : the number of questions following the fist one varies, which is why I cannot point at the item IDs directly.

function resetform() {
var form=FormApp.openById(FORM_ID)
var items=form.getItems()
items.forEach(item=>form.deleteItem(item))
//1994309330 ==> the Item ID that I would like to keep in the delete process
}

Is there a way I can delete all items except item ID 1994309330 ?

I tried to replace items.forEach(item=>form.deleteItem(item))

by items.forEach(item !=1994309330 form.deleteItem(item))

but it does not work


Solution

  • I believe your goal is as follows.

    • You want to delete all items except for the item of item ID 1994309330.

    In this case, how about the following modification?

    Modified script:

    Please set your form ID to FORM_ID.

    function resetform() {
      var form = FormApp.openById(FORM_ID);
      var items = form.getItems();
      items.forEach(item => {
        if (item.getId() != 1994309330) {
          form.deleteItem(item);
        }
      });
      //1994309330 ==> the Item ID that I would like to keep in the delete process
    }
    
    • When this script is run, all items except for the item of item ID 1994309330 are deleted.

    Reference: