【问题标题】:HTMLIFrameElement.contentWindow.print() from GoogleSheets is not opening print previewGoogleSheets 中的 HTMLIFrameElement.contentWindow.print() 未打开打印预览
【发布时间】:2020-12-17 18:47:07
【问题描述】:

我正在使用 GoogleSheets 使用 HTMLService 打印 png/图像文件。我在 modalDialog 中创建了一个带有 img 标签的临时 Iframe 元素,并在加载 IFrame 元素及其图像后调用 IFrame 元素的 contentWindow.print() 函数。 (我没有设置可见性:IFrame 元素的隐藏属性来检查图像是否正在加载。)

但是,我只看到没有任何打印预览的打印机对话框。我在火狐上测试。我错过了什么吗?

[更新] - 我正在使用 Google 应用程序脚本。 performPrint() 在 printJsSource.html 中,openUrl() 在 Code.gs 中。

Inside printJsSource.html

function performPrint(iframeElement, params) {
            try {
                iframeElement.focus()

                // If Edge or IE, try catch with execCommand
                if (Browser.isEdge() || Browser.isIE()) {
                    try {
                        iframeElement.contentWindow.document.execCommand('print', false, null)
                    } catch (e) {
                        iframeElement.contentWindow.print() 
                    }
                } else {
                    // Other browsers
                    iframeElement.contentWindow.print()   // as I am using Firefox, it is coming here                
                }
            } catch (error) {
                params.onError(error)
            } finally {
                //cleanUp(params)
            }
        }

Inside Code.gs

function openUrl() {
  var html = HtmlService.createHtmlOutputFromFile("printJsSource");
  html.setWidth(500).setHeight(500);
  var ui = SpreadsheetApp.getUi().showModalDialog(html, "Opening ..." );
}

【问题讨论】:

    标签: google-sheets print-preview printing-web-page


    【解决方案1】:

    我认为这个概念存在一些普遍的混淆

    • 首先,function performPrint() 似乎是一个客户端 Javascript 函数,而 function openUrl() 是一个服务器端 Apps Script 函数。

    • 虽然您没有指定使用 Google Apps 脚本 - 如果您这样做,function openUrl() 属于 code.gs 文件,function performPrint() 属于 printJsSource.html 文件

    • function openUrl() 允许您打开一个modal dialog,它可以在 UI 上显示一些数据,例如你的头像

    • 不要将此行为与实际打印(预览)混淆!

    • 不可能以编程方式触发打开 Google 表格打印预览!

    • 您使用的 Javascript 方法 iframeElement.contentWindow.print() 可能会触发浏览器窗口的全部内容的打印(不同于 Google 表格打印对话框,也有 depends on the browser),但如果您尝试将其合并到客户端中 -作为 Apps Script 项目的副 coe,您很可能会因为模态对话框的范围和 iframe 的使用而受到限制。
    • 虽然从您的代码中很难说您在 Apps 脚本项目的正确文件中实现了这些功能,但请记住,要使用 iframe,您需要在 function openUrl() 中指定

    html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);

    【讨论】:

    • 很抱歉给您带来了困惑。我正在使用 Googles Apps 脚本。 performPrint() 在 printJsSource.html 中,openUrl() 在 Code.gs 中。
    • 我正在尝试通过 url 打印图像文件。我在 printJsSource.html 中创建了一个 IFrameElement,并设置了一个带有 src 的 img 标签作为 Iframe 中的 url。正在创建 IFrame 元素,但 Iframe.ContentWindow.print 仅打开没有预览的打印机对话框
    • 你期待什么样的预览?谷歌表格预览?如前所述 - 不可能以编程方式触发后者,因为它是无法通过 Javascript 方法访问的服务器端功能。您可以在Google Issue Tracker 上提交功能请求。
    • IFrameElement 内容的打印预览。我认为这是 Firefox 的行为,就像在 Chrome 中一样,它提供了正确的预览。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多