Google 脚本 - 表单 - 删除页面的问题 Breaks/Sections - "Invalid data updating form"

Google Script - Forms - Issues Deleting Page Breaks/Sections - "Invalid data updating form"

我在尝试遍历表单中的项目并删除它们以为新项目让路时遇到以下代码问题 sections/questions。但是,我有时会收到以下错误 "Invalid data updating form"。我已经多次解决这个问题,但它一直在恢复。我目前的解决方法是将部分标题设置为“”,这样就可以删除它了。以前,直到今天我才需要这样做。

我的问题:遍历表单中的项目并从起点删除它们并且不会遇到此错误的最佳方法是什么?

参考:
f = 当前活动形式
f_items = 数组中表单的所有项目

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

function find(src, name)
{
    var s_len = src.length;
    for(var iter = 0; iter < s_len; iter++)
    {
        var s = src[iter].getTitle();
        if(s == name)
        { return iter; }
    }
    return -1;
}

我发现问题了!这是循环结束时的 clear++。随着每次迭代中的项目数量下降,clear++ 导致它跳过分页项目。下面是我完成的代码:

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;
    }
}

我遇到的问题是我试图删除的 PageBreakItem 是表单前面条件答案的目的地。

下面是我的代码,我需要删除某个项目之后的所有内容,该项目链接到我需要删除的部分,因此我能够使用 while 循环向后迭代。

function getParentNameItem_(form, form_items){
  //finds the item with conditional navigation to what you want to delete
  var 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 keep
  while(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;
}