franciscofcosta
franciscofcosta

Reputation: 843

Launching Google Form from App Script

I am trying to launch an Google Form from a script on Google sheets. At the moment, i am doing it trough the form's unique ID and this is what I have:

function addtoDatabase() {

var formaddnew = FormApp.openById('uniqueformID');

}

I am sure i am using the correct form's ID. The script runs without returning any errors or exceptions but the form isn't launched. I am new to App script and I think I might be overlooking something.

Thank you for your help.

Upvotes: 2

Views: 7496

Answers (1)

Max Makhrov
Max Makhrov

Reputation: 18707

You (as me too) were confused with a strange name of function openById:

var formaddnew = FormApp.openById('uniqueformID');

This code does not "open" a form, it assigns a form to the variable formaddnew. You may check it if you add this line of code:

Logger.log(formaddnew);

run the code and press [Ctrl]+[Enter] to see the log.


How to open a form with a script

No. One has no such option because scripts have no access to a browser. A Form is actually opened in a browser, and cannot open new tabs in a browser.

Is there any way though to open a form from a pop-up or message box?

Please try the method described here:

Google Apps Script to open a URL

Here's a tested sample code based on this answer:

var C_URL = 'https://stackoverflow.com/questions/48947678/launching-google-form-from-app-script'; // change

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Menu')
      .addItem('Show Window', 'testNew')
      .addToUi();
}

function testNew(){
  showAnchor('Open this link',C_URL);
}

function showAnchor(name,url) {
  var html = '<html><body><a href="'+url+'" target="blank" onclick="google.script.host.close()">'+name+'</a></body></html>';
  var ui = HtmlService.createHtmlOutput(html)
  SpreadsheetApp.getUi().showModelessDialog(ui,"demo");
}

Notes:

  • the script creates a custom menu and opens the window with an URL. User has to click the URL.

Upvotes: 5

Related Questions