我正在尝试从 Google 表格上的脚本启动 Google 表单。目前,我正在通过表单的唯一 ID 执行此操作,这就是我所拥有的:
function addtoDatabase() {
var formaddnew = FormApp.openById('uniqueformID');
}
我确信我使用了正确的表单 ID。该脚本运行时不会返回任何错误或异常,但表单未启动。我是应用程序脚本的新手,我想我可能忽略了一些东西。
感谢您的帮助。
最佳答案
您(也和我一样)对函数 openById
的奇怪名称感到困惑:
var formaddnew = FormApp.openById('uniqueformID');
此代码不会“打开”表单,而是将表单分配给变量 formaddnew
。添加这行代码就可以检查一下:
Logger.log(formaddnew);
运行代码并按[Ctrl]+[Enter]查看日志。
如何使用脚本打开表单
没有。没有这样的选项,因为脚本无法访问浏览器。表单实际上是在浏览器中打开的,并且 google-apps-script无法在浏览器中打开新选项卡。
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/