javascript - Apps 脚本自定义模式对话框中的 window.print()
问题描述
我正在通过运行 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>');
解决方案
您应该考虑将打印函数重命名为其他名称,例如“printPage”,否则它可能会调用本机打印 API。此外,可能会删除 HTML 中的额外括号。
<!DOCTYPE html>
<html>
<head><base target="_top">
</head>
<body>
<img src="https://i.imgur.com/someimage.png" />
<h3>testing</h3>
<button onclick="printPage()">Print</button>
</body>
<script>
function printPage() {
window.print();
}
</script>
</html>
推荐阅读
- jquery - 使用材质图标更改 SVG d="" 路径
- html - 使用组件通信的 Crud 操作(@Input(),@Output())在角度中,如何在编辑后更新表格行
- javascript - 选择框html5中不同选项的单独输入选项
- laravel - Laravel - 如何使 CSRF 令牌与 HTML 缓存一起使用?
- java - GSON反序列化没有所有字段的json字符串
- python - 无法连接到 Heroku 中的 Django 数据库
- add-in - 构建一个 VISIO 插件以通过创建矩形来显示形状选择的相关数据并在其中包含文本
- javascript - 数组过滤方法不行
- android - 将服务重新附加到活动?
- javascript - 在动态嵌套元素中分配级联 ID (JavaScript)