google sheets launch form from script

我怕爱的太早我们不能终老 提交于 2019-12-08 11:17:09

问题


I'm trying to launch a form from a script from my script following the code I borrowed from here:

Single Google Form for multiple Sheets

which I did as follows:

function CreateForm() {
  var form = FormApp.create('New Form');
  var item = form.addCheckboxItem();
  item.setTitle('Is this working now?');
  item.setChoices([
          item.createChoice('yes'),
          item.createChoice('no')
  ]);
  form.addMultipleChoiceItem()
          .setTitle('What should I do about it?')
          .setChoiceValues(['Complain', 'Rejoice', 'Keep Trying'])
          .showOtherOption(true);
  Logger.log('Published URL: ' + form.getPublishedUrl());
  Logger.log('Editor URL: ' + form.getEditUrl());  
  Logger.log('Form ID: ' + form.getId());
}


function launchForm() {
  var formTitle = 'Weekly Report Questionnaire';
  var formID = '1NjNO7JTlt9H2IB6L3QowGitR38o2KrnZxpKB3MWoVXE';
  var form = FormApp.openById(formID);
  var formUrl = form.getPublishedUrl();
  var response = UrlFetchApp.fetch(formUrl);
  var formHtml = response.getContentText(); 
  var htmlApp = HtmlService
  .createHtmlOutput(formHtml)
  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
  .setTitle(formTitle)
  .setWidth(500) 
  .setHeight(450);
  SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}

So the problem is, when I execute launchForm from my menu add-on, the form appears, but it doesn't work - I can't check the boxes, enter text, or submit the form, although the cursor changes when I hover over an item. I can only close the form. I want the form to store the values from the input fields in my form as variables for later output in my email html text.


回答1:


How about this workaround? In this workaround, the form URL is directly used and opened in the iframe.

Modified script :

I modified only launchForm() as follows.

function launchForm() {
  var formTitle = 'Weekly Report Questionnaire';
  var formID = '1NjNO7JTlt9H2IB6L3QowGitR38o2KrnZxpKB3MWoVXE';
  var form = FormApp.openById(formID);
  var formUrl = form.getPublishedUrl();
  var htmlApp = HtmlService
  .createHtmlOutput('<script>location.href = "' + formUrl + '"</script>')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
  .setTitle(formTitle)
  .setWidth(500) 
  .setHeight(450);
  SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}

If this was not useful for you, I'm sorry.



来源:https://stackoverflow.com/questions/47840313/google-sheets-launch-form-from-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!