I have a Google sheets spreadsheet with few sheets named: "Form Responses 1", "Form Responses 2", "Form Responses 3" and "Sheet1". I wrote a code that delete all sheets except "Sheet1":
When I make run the function it gives me an error:
You cannot delete a sheet with a linked form. Please unlink the form first.
How can I unlink the Form from the sheet? See the code of the function below.
function clearForms()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var numsheets = ss.getNumSheets()-1;
var sheets = ss.getSheets();
var i=numsheets;
while (i >=0)
{
if ( sheets[i].getName() != "Sheet1") {
Logger.log(sheets[i].getName()+" Deleted");
ss.deleteSheet(sheets[i]);
}
i--;
}
}
Thanks.
Use the workaround as follows;
var formUrl = SpreadsheetApp.getActive().getActiveSheet().getFormUrl();
if (formUrl)
FormApp.openByUrl(formUrl).removeDestination();
This will work even if the Form has been trashed using;
DriveApp.getFileById(id).setTrashed(true);
But it still wont work if the file has been permanently deleted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With