function emailInvoiceAsPDF() {
DocumentApp.getActiveDocument();
DriveApp.getFiles();
// Confirm that the user wishes to send the invoice.
var result = SpreadsheetApp.getUi().alert(
'Please Confirm',
'Email a copy of this invoice to the partner?',
SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
//If user confirms, send out emails
if(result === SpreadsheetApp.getUi().Button.OK) {
// This is the link to my spreadsheet with the Form responses and the Invoice Template sheets
// Add the link to your spreadsheet here
// or you can just replace the text in the link between "d/" and "/edit"
// In my case is the text: 17I8-QDce0Nug7amrZeYTB3IYbGCGxvUj-XMt8uUUyvI
// We are going to get the email address from the cell "D9" from the "Background_Original" sheet
// Change the reference of the cell or the name of the sheet if it is different
const value = ss.getSheetByName("Background_Original").getRange("E9").getValue();
const email = value.toString();
// Subject of the email message
const subject = 'Invoice: ' + ss.getSheetByName("Invoice_Display").getRange("C5").getValue().toString() + " " + ss.getSheetByName("Invoice_Display").getRange("E5").getValue().toString();
const body = "Hi "
+ ss.getSheetByName("Background_Original").getRange("C9").getValue().toString() +
",<br/><br/>Please see attached.<br/><br/>Your invoice amount is $"
+ ss.getSheetByName("Background_Original").getRange("F9").getValue().toString()
+ ".<br/><br/>Please remit to email address via Interac E-Transfer, and include the invoice number in the memo line.<br/><br/>Thank-you.<br/><br/>Kind Regards,<br/>Name<br/>Title.<br/>Number";
// Again, the URL to your spreadsheet but now with "/export" at the end
// Change it to the link of your spreadsheet, but leave the "/export"
const exportOptions =
'exportFormat=pdf&format=pdf' + // export as pdf
'&size=letter' + // paper size letter / You can use A4 or legal
'&portrait=true' + // orientation portal, use false for landscape
'&fitw=true' + // fit to page width false, to get the actual size
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&gid=SpreadsheetID'; // the sheet's Id. Change it to your sheet ID.
// You can find the sheet ID in the link bar.
// Select the sheet that you want to print and check the link,
// the gid number of the sheet is on the end of your link.
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
// Generate the PDF file
var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
// Send the PDF file as an attachement
GmailApp.sendEmail(email, subject, body, {
name: 'Name',
htmlBody: body,
attachments: [{
fileName: "Name Invoice for " + ss.getSheetByName("Background_Original").getRange("D9").getValue().toString() + " " + ss.getSheetByName("Invoice_Display").getRange("O9").getValue().toString() + ".pdf",
content: response.getBytes(),
mimeType: "application/pdf"
}]
});
// Save the PDF to Drive in the Invoices folder.
const nameFile = "Name Invoice for " + ss.getSheetByName("Background_Original").getRange("D9").getValue().toString() + " " + ss.getSheetByName("Invoice_Display").getRange("O9").getValue().toString() + ".pdf"
var folder = DriveApp.getFolderById('folderID');// get the folder
folder.createFile(response.setName(nameFile));//create the file directly in the folder
//Let the user know that the emails were sent successfully or not.
var emailsent = SpreadsheetApp.getUi().alert(
'Success',
'Invoice sent.',
SpreadsheetApp.getUi().ButtonSet.OK);
} else {
}
}