1

I am trying to make a href-link (dataUri) work in my google sheet on our Company Terminals' Chrome Browser. The below code works fine on my private PC Chrome Browser, but not in the Company Browser.

I used the code from User TheMaster seen at the following link:
How to download single sheet as PDF (not export to Google Drive)

Chrome Browser Version in Company: Version 83.0.4103.61 (Official Build) (64-Bit)

The problem is, that the automatic download is not initialised. The clickable link does not work on left button press, but one can save the PDF with right click "save under".

Is there any setting that needs to be changed in order to make the automatic download work?

Thank you very much.

function downloadPdfToDesktop() {
  var ss = SpreadsheetApp.getActive(),
    id = ss.getId(),
    sht = ss.getActiveSheet(),
    shtId = sht.getSheetId(),
    url =
      'https://docs.google.com/spreadsheets/d/' +
      id +
      '/export' +
      '?format=pdf&gid=' +
      shtId;
  var val = 'PDFNAME';//custom pdf name here 
  val += '.pdf';
  //can't download with a different filename directly from server
  //download and remove content-disposition header and serve as a dataURI
  //Use anchor tag's download attribute to provide a custom filename
  var res = UrlFetchApp.fetch(url, {
    headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
  });
  SpreadsheetApp.getUi().showModelessDialog(
    HtmlService.createHtmlOutput(
      '<a target ="_blank" download="' +
        val +
        '" href = "data:application/pdf;base64,' +
        Utilities.base64Encode(res.getContent()) +
        '">Click here</a> to download, if download did not start automatically' +
        '<script> \
        var a = document.querySelector("a"); \
        a.addEventListener("click",()=>{setTimeout(google.script.host.close,10)}); \
        a.click(); \
        </script>'
    ).setHeight(50),
    'Downloading PDF..'
  );
}
Kristian
  • 83
  • 1
  • 3
  • 8
  • 2
    Is it possible that you have popups blocked for this page? target="_blank" has to be explicitly allowed by some browsers. – roma May 26 '20 at 10:10
  • Hello Roma, thank you for your input. I am not getting any popups of that sort. Is there are way to green light the popup through some specific settings? Thank you. – Kristian May 26 '20 at 10:32
  • 2
    Also Check the "browser dev console" for errors. – TheMaster May 26 '20 at 10:34
  • 1
    It's possible that disabling pop-ups is company policy. Check the default settings by following the ` > Site settings` menu item from the URL bar (left of the website address) and check the [site specific settings for `Pop-ups and redirects`](https://i.imgur.com/ueEOvj3.png). – Rafa Guillermo May 26 '20 at 11:22
  • The pop ups settings are granted. The blank sheet popup works too, just stating that. The download of the PDF is not being initialised. Neither automatically nor on left click on the "Click here" button. The dev consol says: Error code = 10, Path = /wardeninit, name: "TransportError" If the script is being executed in a cell instead of a button it says: [...] Error code 401 [...] use muteHttpExceptions option to examine full response – Kristian May 26 '20 at 11:58
  • Can you set `muteHttpExceptions` and provide the full response? – Rafa Guillermo May 28 '20 at 10:08

0 Answers0