【问题标题】:window.print() in Apps Script custom modal dialog boxApps 脚本自定义模式对话框中的 window.print()
【发布时间】: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() 没有 &lt;script&gt; 部分就足够了。但是,是的,它不会真正起作用,因为您只会打印侧边栏,而不是整个文档。

标签: javascript google-apps-script google-sheets


【解决方案1】:

您应该考虑将打印函数重命名为其他名称,例如“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>

【讨论】:

  • 呜呼!这行得通!非常感谢,更改函数名称就可以了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-26
  • 2013-12-14
  • 1970-01-01
相关资源
最近更新 更多