-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.gs
More file actions
335 lines (267 loc) · 11.4 KB
/
Copy pathcode.gs
File metadata and controls
335 lines (267 loc) · 11.4 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Application constants
const APP_TITLE = 'Generate and send PDFs';
const OUTPUT_FOLDER_NAME = "Factures_CA2627_send";
const INVOICE_TEMPLATE_SHEET_NAME = 'Invoice';
const str_error = "Error mail" ;
const nomber_of_email_type = 6 ;
var ok_emails =["Pending Approval", "Confirmed", "Paiement Received", "Final Info", "Reminder"] ;
var date_emails=["Date Pending Approval", "Date Confirmed", "Date Paiement", "Date Final Info", "Date Reminder"] ;
var subject_emails = ["Pending Approval Subject", "Confirmed Subject", "Paiement received subject", "Final info subject", "Reminder subject"] ;
var msg_emails = ["Pending Approval Msg", "Confirmed Msg", "Paiement received Msg", "Final Info Msg", "Reminder Paiement Msg"] ;
ok_emails.push("Invoice")
date_emails.push("Date Invoice")
subject_emails.push("Invoice Subject")
msg_emails.push("Invoice Msg")
var ss = SpreadsheetApp.getActiveSpreadsheet()
var tech_mails = ss.getSheetByName("Tech_mails") ;
var interrup_indice = getCellValueByColumnName(tech_mails, "Indice Interruption", 2)
var form_name = getCellValueByColumnName(tech_mails, "Name Form", 2)
var attendee_mail_column = getCellValueByColumnName(tech_mails, "Col email name", 2)
var attendee_name_column = getCellValueByColumnName(tech_mails, "Col nom name", 2)
var attendee_surname_column = getCellValueByColumnName(tech_mails, "Col prenom name", 2)
var attendee_adress_column = getCellValueByColumnName(tech_mails, "Col adress name", 2)
var attendee_code_column = getCellValueByColumnName(tech_mails, "Col code name", 2)
var attendee_price_column = getCellValueByColumnName(tech_mails, "Col price name", 2)
//const str_dm_fact = "Date Invoice" ;
//const str_dm_part = "Date mail attente part" ;
const invoiceTemplateSheet = ss.getSheetByName("Invoice");
const ssId = ss.getId()
var now = new Date();
const IS_OPERATIONAL = true // false = debug mode, envoie les mails à raphael et n'écrit aucune date
function OK_recep(){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var rep_form = ss.getSheetByName(form_name) ;
var last_row = rep_form.getLastRow() ;
var ok_email= ok_emails[0] ;
var date_email = date_emails[0] ;
var subject_email = subject_emails[0] ;
var msg_email = msg_emails[0] ;
for (var i=last_row; i > 2; i--){
if (getCellValueByColumnName(rep_form, ok_email, i) == "OK"){
break;
}
setCellValueByColumnName(rep_form, ok_email, i, "OK")
message = getCellValueByColumnName(rep_form, msg_email, i)
topic = getCellValueByColumnName(rep_form, subject_email, i)
if (IS_OPERATIONAL) {
mail = getCellValueByColumnName(rep_form, attendee_mail_column, i)
} else {
mail = "raphael.of.p@gmail.com" ;
}
try {
GmailApp.sendEmail(mail,topic,message);
if (IS_OPERATIONAL) {
setCellValueByColumnName(rep_form, date_email, i, now) // If mail is send, write the date
}
} catch(e) {
Logger.log("Error with email (" + mail + "). " + e);
setCellValueByColumnName(rep_form, str_error, i, "Error with email")
}
}
}
function emails() {
// Récupère les données
var userEmail = Session.getActiveUser().getEmail();
//if (userEmail =="westievillageinfo@gmail.com" || userEmail =="raphael.of.p@gmail.com"){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var rep_form = ss.getSheetByName(form_name) ;
//var tech_mails = ss.getSheetByName("Tech_mails") ;
//var template_mail = ss.getSheetByName("Template mails") ;
var last_row = rep_form.getLastRow() ;
var is_fact_mail = false
var mail = ""
var interrup_indice = getCellValueByColumnName(tech_mails, "Indice Interruption", 2)
var start_indice = getCellValueByColumnName(tech_mails, "Indice start", 2)
if(start_indice < interrup_indice){
start_indice = interrup_indice ;
}
for (var i=start_indice; i < last_row+1; i++){
setCellValueByColumnName(tech_mails, "Indice Interruption", 2, i)
var message = "";
var attachments = [{}] ;
for (var j=0 ; j < nomber_of_email_type; j++){
var ok_email= ok_emails[j] ;
var date_email = date_emails[j] ;
var subject_email = subject_emails[j] ;
var msg_email = msg_emails[j] ;
is_OK = getCellValueByColumnName(rep_form, ok_email, i)
is_date = getCellValueByColumnName(rep_form, date_email, i)
if (is_OK=="OK" && is_date==""){
message = getCellValueByColumnName(rep_form, msg_email, i)
topic = getCellValueByColumnName(rep_form, subject_email, i)
if (subject_email == "Invoice Subject"){
customer_name = getCellValueByColumnName(rep_form, attendee_name_column, i) + " " + getCellValueByColumnName(rep_form, attendee_surname_column, i) ;
customer_adress = getCellValueByColumnName(rep_form, attendee_adress_column, i)
invoiceNumber = getCellValueByColumnName(rep_form, attendee_code_column, i);
price = getCellValueByColumnName(rep_form, attendee_price_column, i)
var pdfID = processDocuments(customer_name, customer_adress, price, invoiceNumber, invoiceTemplateSheet, ss)
var attachment = DriveApp.getFileById(pdfID);
}
if (IS_OPERATIONAL) {
mail = getCellValueByColumnName(rep_form, attendee_mail_column, i)
} else {
mail = "raphael.of.p@gmail.com" ;
}
try {
if (subject_email == "Invoice Subject"){
GmailApp.sendEmail(mail, topic, message, {
attachments: [attachment.getAs(MimeType.PDF)],
name: 'West In Lille',
});
}
else{
GmailApp.sendEmail(mail,topic,message);
}
if (IS_OPERATIONAL) {
setCellValueByColumnName(rep_form, date_email, i, now) // If mail is send, write the date
}
} catch(e) {
Logger.log("Error with email (" + mail + "). " + e);
setCellValueByColumnName(rep_form, str_error, i, "Error with email")
}
}
}
}
setCellValueByColumnName(tech_mails, "Indice Interruption", 2, 2)
//}
// if (is_facture == "OK"){
// if (is_mail_fact == ""){
// message = getCellValueByColumnName(rep_form, "Mail facture", i)
// pass = getCellValueByColumnName(rep_form, "Type d'inscription ", i)
// prix = getCellValueByColumnName(rep_form, "Prix", i)
// customer_name = getCellValueByColumnName(rep_form, "NOM", i) + " " + getCellValueByColumnName(rep_form, "Prénom", i) ;
// customer_adress = getCellValueByColumnName(rep_form, "Tu veux une facture ?", i)
// invoiceNumber = "CA252600" + getCellValueByColumnName(rep_form, "Code", i);
// pdfblob = processDocuments(customer_name, customer_adress, prix, pass, invoiceNumber, ss)
// setCellValueByColumnName(rep_form, str_dm_fact, i, now)
// str_case_date = str_dm_fact
// is_fact_mail = true ;
// is_send_new_mail = true ;
// }
//}
// else if (parseInt((now-is_mail_confirm)/(24*3600*1000)) > nbre_jour_relance){ // Tous les XXj
// if (is_relance != "NON"){
// message = getCellValueByColumnName(rep_form, "Mail relance paiement", i)
//setCellValueByColumnName(rep_form, str_dm_confirm, i, now) ;
// str_case_date = str_dm_confirm
// is_send_new_mail = true ;
// }
// }
}
function getCellValueByColumnName(sheet, columnName, row) {
let cell = getCellRangeByColumnName(sheet, columnName, row);
if (cell != null) {
return cell.getValue();
}
}
function getCellRangeByColumnName(sheet, columnName, row) {
let data = sheet.getDataRange().getValues();
let column = data[0].indexOf(columnName);
if (column != -1) {
return sheet.getRange(row, column + 1, 1, 1);
}
}
function getCellValueByLineName(sheet, lineName, col) {
let cell = getCellRangeByLineName(sheet, lineName, col);
if (cell != null) {
return cell.getValue();
}
}
function getCellRangeByLineName(sheet, lineName, col) {
let data = sheet.getDataRange().getValues();
transposedata = Transpose(data);
let row = transposedata[0].indexOf(lineName);
if (row != -1) {
return sheet.getRange(row + 1, col, 1, 1);
}
}
function Transpose(a){
return Object.keys(a[0]).map( function (c) { return a.map(function (r) { return r[c];}); });
}
function setCellValueByColumnName(sheet, columnName, row, new_value) {
let cell = getCellRangeByColumnName(sheet, columnName, row);
return cell.setValue(new_value);
}
/**
* Populating the template sheet with customer data, then saves as a PDF document.
*
* Called by user via custom menu item.
*/
function processDocuments(customer_name, customer_adress, price, invoiceNumber, templateSheet, ss) {
//ss.toast("Creating Invoices", APP_TITLE, 1);
//const invoices = [];
//ss.toast(`Creating Invoice for ${customer.customer_name}`, APP_TITLE, 1);
const pdfID = createInvoiceForCustomer(
customer_name,
customer_adress,
price,
invoiceNumber,
templateSheet,
ss.getId(),
);
//invoices.push(invoice);
// Writes invoices data to the sheet.
return pdfID
}
function createInvoiceForCustomer(customer_name, customer_adress, price, invoiceNumber, templateSheet, ssId) {
// Clears existing data from the template.
//clearTemplateSheet();
// Calulates dates.
const todaysDate = new Date().toDateString()
// Sets values in the template.
templateSheet.getRange('B9').setValue(customer_name)
templateSheet.getRange('B10:B13').setValue(customer_adress)
templateSheet.getRange('F9').setValue(invoiceNumber)
templateSheet.getRange('F12').setValue(todaysDate)
templateSheet.getRange('F19:F20').setValue(price)
// Cleans up and creates PDF.
SpreadsheetApp.flush();
Utilities.sleep(500); // Using to offset any potential latency in creating .pdf
const pdf = createPDF(
ssId,
templateSheet,
`Invoice#${invoiceNumber}-${customer_name}`,
);
return pdf.getId()
}
/**
* Resets the template sheet by clearing out customer data.
* You use this to prepare for the next iteration or to view blank
* the template for design.
*
* Called by createInvoiceForCustomer() or by the user via custom menu item.
*/
function clearTemplateSheet() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const templateSheet = ss.getSheetByName(INVOICE_TEMPLATE_SHEET_NAME);
// Clears existing data from the template.
const rngClear = templateSheet.getRangeList(['B9', 'B10:B13', 'F9', 'F12', 'B19:B20', 'F19:F20']).getRanges()
rngClear.forEach(function (cell) {
cell.clearContent();
});
}
/**
* Creates a PDF for the customer given sheet.
* @param {string} ssId - Id of the Google Spreadsheet
* @param {object} sheet - Sheet to be converted as PDF
* @param {string} pdfName - File name of the PDF being created
* @return {file object} PDF file as a blob
*/
function createPDF(ssId, sheet, pdfName) {
const fr = 0;
const fc = 0;
const lc = 9;
const lr = 27;
const url = `https://docs.google.com/spreadsheets/d/${ssId}/export?format=pdf&size=7&fzr=true&portrait=true&fitw=true&gridlines=false&printtitle=false&top_margin=0.5&bottom_margin=0.25&left_margin=0.5&right_margin=0.5&sheetnames=false&pagenum=UNDEFINED&attachment=true&gid=${sheet.getSheetId()}&r1=${fr}&c1=${fc}&r2=${lr}&c2=${lc}`;
const params = {
method: "GET",
headers: { authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
};
const blob = UrlFetchApp.fetch(url, params)
.getBlob()
.setName(`${pdfName}.pdf`);
// Gets the folder in Drive where the PDFs are stored.
const folder = getFolderByName_(OUTPUT_FOLDER_NAME);
const pdfFile = folder.createFile(blob);
return pdfFile;
}