Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Reseting and deleting all items in Google form failing

36 views
Skip to first unread message

Emma

unread,
Apr 14, 2025, 4:43:54 AMApr 14
to Google Apps Script Community
Hi 
I had my extremely large form working perfectly, it would reset and delete and then reload. But not it suddenly won't. Something is stuck in the branching I suspect and the 'reset' code is failing. 

I am hoping there is a solution. My code has a lot of branching over 53 sections but it is working, until I need to reboot it. I'm not keen on completely recreating the form each time, as my drive is getting rather full with my edits!  

I hope you can help, I'm very new to Apps Script and so close to finishing this!
I am probably missing something obvious!

bw
Emma


I've tried resetting the project and undoing my recently changes. 

Tried a few different versions of deleting but it stacked on additional questions - hence not deleted: 
  if (!form) {
    form = FormApp.create('TEST1 Form Example');
    props.setProperty('TEST1FormId', form.getId());
    Logger.log('🆕 Created new form: ' + form.getEditUrl());
  } else {
    Logger.log('✅ Using existing form: ' + form.getEditUrl());
  }

  // Clear any existing questions (but leave the page breaks intact)
  var items = form.getItems();
  for (var i = items.length - 1; i >= 0; i--) {
    var item = items[i];
    if (item.getType() !== FormApp.ItemType.PAGE_BREAK) {
      form.deleteItem(item);
    }
  }
--------------------------------------------
The one below was working reliably but now I get an error at deleting a page break -

Error
Exception: Invalid data updating form.
Code.gs:39
---------------------------------------
// Step 1: Clear all page navigation (branching) to avoid deletion conflicts
const items = form.getItems();
for (let item of items) {
  if (item.getType() === FormApp.ItemType.MULTIPLE_CHOICE) {
    const mc = item.asMultipleChoiceItem();
    const choices = mc.getChoices().map(choice =>
      mc.createChoice(choice.getValue(), FormApp.PageNavigationType.CONTINUE)
    );
    mc.setChoices(choices);
  }
  Logger.log('📄 Items' +item.getType());
}


// Step 2: Now safely delete all items (questions + page breaks)
for (let i = items.length - 1; i >= 0; i--) {
  Logger.log('📄 Delete' + items[i].getType());
  form.deleteItem(items[i]);
}

Emma

unread,
Apr 14, 2025, 7:15:33 PMApr 14
to Google Apps Script Community
I case anyone else gets stuck like I did. The second method of resetting the form ended up being the most reliable. 

The error I experience was due to the additional branching conditions I added to guide the flow of the form. Normally these are set to 'continue' to the next question. The branching on it's own wasn't the problem but it didn't appear to like letting to shift to the last question and automatically timing out. This appeared to leave residual 'Page Breaks'. Instead ending on an earlier question and using SUBMIT form appeared to resolve the issue. 

thisMainPage.setGoToPage(FormApp.PageNavigationType.SUBMIT);

BTW - once the error occurred the form had to completely recreated with a new title. Once it failed to delete items it wasn't able to recover. Really a better instruction to 'reset()' the form is required, but it doesn't appear to exist. 

Also once you changed any settings on the UI form then again the script failed. So save additional formatting, default settings like email verification only once the form is complete. 

😊
Reply all
Reply to author
Forward
0 new messages