I built a script that created a large number of forms. Some of them were duplicated, and I'm trying to remove those forms and their corresponding destination sheets. Here's what I've got:
function removeDups(){
var ss, maint, sheets, form, destsheet, d1, d2, s1,s2
ss = SpreadsheetApp.getActive();
maint = ss.getSheetByName("Maintenance");
destsheet = SpreadsheetApp.openByUrl(maint.getRange("B1").getValue());
sheets = destsheet.getSheets();
for (var i = 0; i<sheets.length; i++){
s1 = sheets[i]
s2 = sheets[i+1];
d1 = s1.getRange("C1").getValue();
d2 = s2.getRange("C1").getValue();
if (d1.toString() == d2.toString()){
form = FormApp.openByUrl(s2.getFormUrl())
form.removeDestination();
destsheet.deleteSheet(s2);
}
}
}
It throws a "cannot delete sheet with linked form" error each time.
I've tried the following:
- Removed the deleteSheet line, with the intention of having script go back and delete sheets without linked forms. In this case the script completes, but forms remain linked.
- I've tried changing the destination for duplicate forms to another spreadsheet. In this case the form winds up linked to two sheets, and I still cannot delete the original linked sheet.
What am I missing here?
Edit: When I open the form itself, it is no longer linked to a spreadsheet. So the script removes the form destination as it should, but this is not reflected in the spreadsheet.