I've successfully learnt through this site how to send a sheet as a PDF attached via google sheets. What I'd now like to do is slightly expand that a bit.
I want to select a constant specific range that will export, along with the last added row within a column range.
J1:S1 to export every time. then the newest row from J2:S2 onwards.
I like to have these two ranges combined, with the constant range (J1:S1) always being on top.
I'm not entirely sure if this is possible as a PDF or if it may need to be a HTML table. I can work with either atm.
I really not sure where to start here, so would appreciate suggestions.
Thank you!
In the example above, the titles remain on every email / PDF. But each newest edition to the rows below is added to the PDF. So in the example above the row with the 2's would be added.
var ss = SpreadsheetApp.getActiveSpreadsheet();
function sendReport() {
var sheetTabNameToGet = "Form response master";
var range = "J1:S1";
var pdfBlob = exportRangeToPDf(range, sheetTabNameToGet);
var message = {
to: "example@example.com",
subject: "Monthly sales report",
body: "Hi team,\n\nPlease find the monthly report attached.\n\nThank you,\nBob",
name: "Bob",
attachments: [pdfBlob.setName("Monthly sales report")]
}
MailApp.sendEmail(message);
}
function exportRangeToPDf(range, sheetTabNameToGet) {
var blob,exportUrl,options,pdfFile,response,sheetTabId,ssID,url_base;
ssID = ss.getId();
sh = ss.getSheetByName(sheetTabNameToGet);
sheetTabId = sh.getSheetId();
url_base = ss.getUrl().replace(/edit$/,'');
exportUrl = url_base + 'export?exportFormat=pdf&format=pdf' +
'&gid=' + sheetTabId + '&id=' + ssID +
'&range=' + range +
'&size=A4' + // paper size
'&portrait=false' + // orientation, false for landscape
'&fitw=true' + // fit to width, false for actual size
'&sheetnames=true&printtitle=false&pagenumbers=true' + //hide optional headers and footers
'&gridlines=false' + // hide gridlines
'&fzr=false'; // do not repeat row headers (frozen rows) on each page
options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
}
}
options.muteHttpExceptions = true;//Make sure this is always set
response = UrlFetchApp.fetch(exportUrl, options);
if (response.getResponseCode() !== 200) {
console.log("Error exporting Sheet to PDF! Response Code: " + response.getResponseCode());
return;
}
blob = response.getBlob();
return blob;
}