I am trying to email a PDF of a single Google Sheet and have compiled the below so far. However, the recipient receives a PDF file that fails to load or even preview in Gmail.
function sendEmail(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('Sheet1');
const url_base = ss.getUrl().replace(/edit$/,'');
const url_ext = 'export?exportFormat=pdf&format=pdf'
+ '&gid=' + sheet.getSheetId();
const opt = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
}
};
const response = UrlFetchApp.fetch(url_base + url_ext, opt);
const blob = response.getBlob().setName('Sheet1.pdf');
MailApp.sendEmail({
to: 'email@address.com',
subject: 'subject',
htmlBody: 'body',
attachments: blob
});
}