【发布时间】:2019-08-08 04:39:53
【问题描述】:
我正在通过运行 onEdit 触发器的 Apps 脚本在 Google 表格中创建一个自定义模式对话框弹出框。这个想法是,用户单击列中某个单元格中的复选框。触发器检测到此编辑,并调用利用 Apps Script UI 和 HtmlService 类的函数。这将创建一个使用一些 html 构建的简单模式对话框。在 html 中,我有一个调用 window.print() 的按钮。但是,通过调用它,什么也没有发生。我认为这是因为同源政策问题。 Html 服务可能使用另一个域名来启动不同于 docs.google.com 的对话框。因此,窗口调用可能存在问题。还有其他方法吗?如何为 Google Apps 创建自定义打印?我已经看到了一些动态创建 pdf 并打印它们的变体,但这对于最终用户来说似乎效率很低。
当复选框被点击时,会调用以下函数:
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('html') ;
SpreadsheetApp.getUi()
.showModalDialog(html, 'Print Receipt');
}
这是下面的html:
<!DOCTYPE html>
<html>
<head><base target="_top">
</head>
<body>
<img src="https://i.imgur.com/someimage.png" alt="Logo" width="100" height="100">
<h3>testing</h3>
<button onclick="print()">Print</button>
</body>
<script>
function print() {
window.print();
}
</script>
</html>');
【问题讨论】:
-
显示控制台错误
-
大卫,你的开发者控制台是怎么说的?当我在 Chrome 中尝试您的版本时,我得到超出最大调用堆栈大小,因为您递归调用“打印”,因为您已经用您的打印屏蔽了 window.print。
onclick=print()没有<script>部分就足够了。但是,是的,它不会真正起作用,因为您只会打印侧边栏,而不是整个文档。
标签: javascript google-apps-script google-sheets