Maor Uliel
Maor Uliel

Reputation: 53

Unlink Form from Sheets

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.

Upvotes: 4

Views: 11560

Answers (4)

Shobeira
Shobeira

Reputation: 189

On the Google Sheet menu bar follow: Tools > Manage forms > Unlink form

Upvotes: 0

Juan Diego Antezana
Juan Diego Antezana

Reputation: 912

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

enter image description here

Upvotes: 6

jws
jws

Reputation: 91

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.

Upvotes: 9

Nitin Dhomse
Nitin Dhomse

Reputation: 2622

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.

Upvotes: 1

Related Questions