如何使用OnSubmit触发器在google电子表格上运行函数?

问题描述 投票:0回答:1

我将尝试解释这种情况,以便你能更好地理解我的问题。我有一个包含3张表的电子表格:第一张包含一个表格的答案,第二张包含第二个表格的答案,第三张是一个通过ARRAYFORMULA收集第一张和第二张表数据的表,它是确定的。我使用了 "OnSubmit "触发器,允许电子表格从第三张表中获取数据,并将它们放在第四张表的某个特定单元格中,然后允许我将第四张表下载为PDF。问题是,这个脚本只有在我从编辑器中尝试时才会工作,但当我提交表格时,它却无法工作。在 "OnSubmit "触发器上报告了这样一个错误 "Exception: impossible to call SpreadsheetApp.getUi() from this context"。有什么方法可以解决吗?有办法解决吗?

function downloadSheetAsPDF() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetId = ss.getSheetByName("DDT").getSheetId();
var url = "https://docs.google.com/a/mydomain.org/spreadsheets/d/" + ss.getId() + "/export?exportFormat=pdf&gid=" + sheetId + "&access_token=" + ScriptApp.getOAuthToken();
var str = '<input type="button" value="Download" onClick="location.href=\'' + url + '\'" >';
var html = HtmlService.createHtmlOutput(str);
SpreadsheetApp.getUi().showModalDialog(html, "Download PDF");
} ```


google-apps-script google-sheets google-sheets-formula google-form
1个回答
0
投票

正如Cooper在评论中提到的,在Sheets中不可能触发打开UI窗口,因为它需要用户交互。也不可能进行自动下载。

更多关于Trigger限制的信息。

https:/developers.google.comapps-scriptguidestriggers#限制。

© www.soinside.com 2019 - 2024. All rights reserved.