Skip to content Skip to sidebar Skip to footer

Google Script - Forms - Issues Deleting Page Breaks/sections - "invalid Data Updating Form"

I am having an issue with the following code when trying to iterate through the items in a form and delete them to make way for new sections/questions. However, I sometimes get th

Solution 1:

The issue I had with this was that the PageBreakItem I was trying to delete was the destination for a conditional answer earlier in the form.

Below is my code where I needed to delete everything after a certain item, which linked to the sections I needed to delete, so I was able to iterate backwards with a while loop.

functiongetParentNameItem_(form, form_items){
  //finds the item with conditional navigation to what you want to deletevar parent_name_item = find_(form_items, "Your Name");

  parent_name_item = parent_name_item.asListItem();

  //clears all choices which breaks the navigation dependency//this frees up the items to be deleted
  parent_name_item.setChoices([parent_name_item.createChoice("")]);

  var size = form_items.length - 1;

  //iterates from the end back to the last question I want to keepwhile(form_items[size].getTitle() != "Your Name"){

    //this can take either the item itself or the index as I've done
    form.deleteItem(size);

    size--;
  }

  /*I rebuild the choices for parent_name_item
  later based on information from a spreadsheet 
  which I also use to determine the content of 
  the PageBreakItems I just deleted*/return parent_name_item;
}

Solution 2:

I found out the issue! It was the clear++ at the end of the loop. With the number of items in the going down with each iteration, the clear++ was causing it to skip over the page break items. Below is my finished code:

function clearForm()
{
    var clearQ = find(f_items, "Select Appointment Date")+1;
    var f_i_len = f.getItems().length-1;
    var clear = clearQ;
    while(clear <= f_i_len && clear >= clearQ)
    {
        var item = f.getItems()[clear];
        if(item.getType() == "PAGE_BREAK")
        { item.asPageBreakItem().setTitle(""); }
        f.deleteItem(clear); //}
        f_i_len = f.getItems().length-1;
    }
}

Post a Comment for "Google Script - Forms - Issues Deleting Page Breaks/sections - "invalid Data Updating Form""