Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 839
Maor Uliel Avatar asked Dec 07 '22 22:12

Maor Uliel


1 Answers

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.

like image 107
jws Avatar answered Dec 28 '22 07:12

jws