简体   繁体   中英

HTMLIFrameElement.contentWindow.print() from GoogleSheets is not opening print preview

I am using GoogleSheets to print a png/image file using HTMLService. I created a temporary Iframe element with an img tag in the modalDialog and call IFrame element's contentWindow.print() function after IFrame element and its image are loaded. (I have not set visibility:hidden attribute of IFrame element to check if image is getting loaded.)

However, I only see the printer dialog without any print preview. I am testing on Firefox. Am I missing anything?

[Updated] - I am using Googles Apps script. performPrint() is in printJsSource.html and openUrl() is in 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 ..." );
}

I think there is some general confusion about the concept

  • First of all, function performPrint() seems to be a client-side Javascript funciton, while function openUrl() is a server-side Apps Script function.

  • While you did not specify either you use Google Apps Script - if you do so, function openUrl() belongs into the code.gs file and function performPrint() into printJsSource.html file

  • function openUrl() allows you to open a modal dialog which can show some data on the UI, eg your image

  • Do not confuse this behavior with actual printing (preview)!

  • It is NOT possible to trigger the opening of a Google Sheets printing preview programamticaly!

  • The Javascript method you are using iframeElement.contentWindow.print() might trigger the printing of the whole content of a browser window (different from the Google Sheets printing dialog, also depends on the browser ), but if you try to incorporate it into the client-side coe of an Apps Script project, you will most likely run into restrictions due to the scopes of modal diloags and usage of iframes.
  • While from your code it is hard to say either you implemented the funcitons in the correct files of the Apps Script project, keep in mind that to work with iframes you need to specify in function openUrl()

html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM