google-apps-script - 从应用程序脚本启动 Google 表单

标签 google-apps-script google-sheets google-forms

我正在尝试从 Google 表格上的脚本启动 Google 表单。目前,我正在通过表单的唯一 ID 执行此操作,这就是我所拥有的:

function addtoDatabase() {

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

}

我确信我使用了正确的表单 ID。该脚本运行时不会返回任何错误或异常,但表单未启动。我是应用程序脚本的新手,我想我可能忽略了一些东西。

感谢您的帮助。

最佳答案

您(也和我一样)对函数 openById 的奇怪名称感到困惑:

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

此代码不会“打开”表单,而是将表单分配给变量 formaddnew。添加这行代码就可以检查一下:

Logger.log(formaddnew);

运行代码并按[Ctrl]+[Enter]查看日志。


如何使用脚本打开表单

没有。没有这样的选项,因为脚本无法访问浏览器。表单实际上是在浏览器中打开的,并且 无法在浏览器中打开新选项卡。

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

请尝试此处描述的方法:

Google Apps Script to open a URL

这是基于 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");
}

注释:

  • 该脚本创建一个自定义菜单并打开带有 URL 的窗口。 用户必须点击该 URL。

关于google-apps-script - 从应用程序脚本启动 Google 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48947678/

相关文章:

google-apps-script - 如何从 Forms App 中的 VideoItem 获取 VideoID 或 VideoUrl?

google-apps-script - 如何从同一个 onFormSubmit 触发器获取多个不需要的事件 block ?

javascript - 正则表达式替换不替换(谷歌应用程序脚本)

google-sheets - Google Sheets Text Join 将行合并为列

javascript - 如何通过迭代计算时间

google-sheets - Google Sheet 中按条件自动递增

google-sheets - 可扩展的正则表达式匹配公式

javascript - Google Apps 脚本功能在我手动运行时执行,但在作为触发器运行时失败,引用错误代码 INTERNAL

javascript - 通过将数据作为单个数组而不是单独的列进行优化

javascript - 如何在提交后重置表单