I am trying to create a button that, when clicked, prompts the user to print a specific sheet of information.
The script successfully saves the document as a PDF. It also manages to open the document in the print GUI to send it to the printer. However, the page that needs to print is blank, only showing the URL at the bottom, and excludes all the cells and formatting, etc. If I copy the URL from the bottom of the page and paste it into my internet search engine, the correct sheet opens up and displays properly with all the cells and formatting... Here is an example of what its doing
function exportRangeAsPdf(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName("Job Specifications");
const range = sheet.getRange("A1:E27");
const url = "https://docs.google.com/spreadsheets/d/" + ss.getId() + "/export? format=pdf" + "&gid=" + sheet.getSheetId() + "&range=" + range.getA1Notation();
const token = ScriptApp.getOAuthToken();
const response = UrlFetchApp.fetch(url, {
headers: {
Authorization: "Bearer " + token
},
muteHttpExceptions: true
});
const blob = response.getBlob().setName("Printed_Range.pdf");
DriveApp.createFile(blob);
const htmlOutput = HtmlService.createHtmlOutput('<script>window.print(); google.script.host.close();</script>')
.setWidth(10)
.setHeight(10);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Print');
}
All I need is for the contents of the sheet found at the correct URL to actually display on the page, so that it can be printed on a physical printer.