-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGoogleSheetPDFInvoiceEmail.js
66 lines (54 loc) · 3.05 KB
/
GoogleSheetPDFInvoiceEmail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Global Variables for email copy and alias
var email = "invoice@stevenkaci.com"; // email of the person you want to send the invoice
var copy = "copy@stevenkaci.com";
var alias = "contact@stevenkaci.com"; // email used to send the invoice or reminder
// Adding Menu Invoice to send the PDF file automatically and kind reminder for unpaid invoices
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("Invoice")
.addItem("Send Invoice PDF","EmailPDF")
.addSeparator()
.addItem("Unpaid Invoice Email","EmailUnpaidInvoice")
.addToUi();
}
// Other Global Variables for both function
var ss = SpreadsheetApp.getActiveSpreadsheet()
var ssID = ss.getId();
var sheetgId = ss.getActiveSheet().getSheetId();
var sheetName = ss.getName();
var token = ScriptApp.getOAuthToken();
var details = ss.getRange("B12").getValue();
// Code for Send Invoice Email aka the Function Email PDF
function EmailPDF() {
// Subject & Body of the mail >> Email PDF
var subject1 = sheetName +" - Invoice " + ss.getSheetName() + " - Steven Kaci";
var body1 = "Hello, " + SpreadsheetApp.getActiveSpreadsheet().getName() +", "
+ "<p>Hope you're doing well. "
+ "<p>Please find attached your invoice number <b>" + ss.getSheetName() + "</b> for: <b>"+ details +"</b>. "
+ "<p>Let me know if you have any quetions. "
+ "<p>Best regards, "
+ "<p>********"
+ "<p><p><b>Steven Kaci</b>";
var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?" + "format=xlsx" + "&gid="+sheetgId+ "&portrait=true" + '&size=A4' + "&exportFormat=pdf";
var result = UrlFetchApp.fetch(url, { headers: { 'Authorization': 'Bearer ' + token
} });
var contents = result.getContent();
GmailApp.sendEmail(email,subject1,body1,{htmlBody: body1, cc: copy, from: alias, attachments:[{fileName:ss.getSheetName()+".pdf", content:contents, mimeType:"application//pdf"}]});
}
// Subject & Body of the mail >> Unpaid Invoice
function EmailUnpaidInvoice() {
var subject2 = sheetName +" - Unpaid Invoice " + ss.getSheetName() + " - Steven Kaci";
var body2 = "Hello, " + SpreadsheetApp.getActiveSpreadsheet().getName() +", "
+ "<p>Hope you're doing well. "
+ "<p>I allow myself to contact you, because unless we are mistaken, we have not yet received the invoice payment. <b>" +ss.getSheetName() + "</b> corresponsant à votre « "+ details +" ». "
+ "<p>>Let me know if you have any quetions. "
+ "<p>Best regards,"
+ "<p>********"
+ "<p><p><b>Steven Kaci</b>";
var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?" + "format=xlsx" + "&gid="+sheetgId+ "&portrait=true" + '&size=A4' + "&exportFormat=pdf";
var result = UrlFetchApp.fetch(url, { headers: { 'Authorization': 'Bearer ' + token
}
});
var contents = result.getContent();
GmailApp.sendEmail(email,subject2,body2,{htmlBody: body2, cc: copy, from: alias, attachments:[{fileName:ss.getSheetName()+".pdf", content:contents, mimeType:"application//pdf"}]});
}