Search code examples
google-apps-scriptgoogle-sheetsgoogle-forms

Launch a Google-Form via Spreadsheet menu


I need two separate forms to be accessible from the menu of my Spreadsheet. After some troubles I found this great answer from Mogsdad: Single Google Form for multiple Sheets

I used his code for embedding the form into the uiapp using the htmlservice. So my code looks exactly like his:

function launchTecForm() {
  var TecformID = '1_some_form_ID';
  var Tecform = FormApp.openById(TecformID);
  var TecformUrl = Tecform.getPublishedUrl();
  var response = UrlFetchApp.fetch(TecformUrl);
  var formHtml = response.getContentText();

  var htmlApp = HtmlService
      .createHtmlOutput(formHtml)
      .setTitle('Tec-Response')
      .setWidth(500) 
      .setHeight(450);

  SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}

So this code worked perfectly until about 2-3 weeks ago (can't say exactly), but since then I either get the login-prompt for logging into google-apps, or I get back a server-error. The Forms are set to public, so there should not be any need to login. Finally login in doesn't work either, cause then it demands to allow cookies (which are allowed in browser settings. I guess the html-service is not capable of processing the login to the cookie.)

So how do i get the script back to work again? I didn't change anything and it is still identical to the code in the answer from Mogsdad.


Solution

  • You got caught by changes in the HtmlService. As announced in May 2013, Google has been changing the defaults for the ECMA sandbox mode used by scripts.

    The good news is that you just need to specify the NATIVE sandbox mode, and the form embedding will resume functioning.

      var htmlApp = HtmlService
          .createHtmlOutput(formHtml)
          .setSandboxMode(HtmlService.SandboxMode.NATIVE)
          .setTitle('Tec-problem')
          .setWidth(500) 
          .setHeight(450);