4
votes

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.

3
In the sheets menu bar, form-> unlink form while in the response sheetJuan Diego Antezana
And yeah, no way to do it programmatically yet: code.google.com/p/google-apps-script-issues/issues/…Juan Diego Antezana

3 Answers

7
votes

Menu bar -> Form-> Unlink form while in the response sheet:

enter image description here

6
votes

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.

1
votes

You can't delete sheet if it is connected / linked with any Google form, Open that form -> go to "Form"=>"Unlink Form" menu. now you can run your script successfully.