Home > Mobile >  How do I get the url for the Sheet linked to a Form?
How do I get the url for the Sheet linked to a Form?

Time:06-03

I have found solutions for getting the url for a Form from a Sheet, but I need to go the other way. I have a script linked to a Form and I need a program that checks to see if the Form is already linked to a Sheet. If it is, it will return the url for the Sheet where the responses are recorded. If not, it will create a Sheet and return the url for that sheet. Here is an example of what I am trying to do:

function sheetChecker() {
  var form = FormApp.getActiveForm();

  if (/*Some statement*/) { //check if sheet is already linked to form
    return sheetUrl //returns the ID for sheet
  }
  else { //if no sheet is linked to Form...
    var ssNew = SpreadsheetApp.create("New Spreadsheet");
    return ssNew.getUrl();
  }
}

I am looking for help completing the if statement. Is this possible?

CodePudding user response:

You can use the method getDestinationId() to get the response destination ID.

As an example:

function myFunction() {
  var form = FormApp.getActiveForm();
  Logger.log(form.getDestinationId())
}

Then you can check if that returns an ID or an exception. If it returns an ID the you can do a SpreadsheetApp.openByID(ID)

  • Related