-
Notifications
You must be signed in to change notification settings - Fork 4
/
Code.js
331 lines (259 loc) · 10.5 KB
/
Code.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
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
/*================================================================================================================*
Invoice Generator
================================================================================================================
Version: 1.0.0
Project Page: https://github.com/Sheetgo/invoice-generator
Copyright: (c) 2018 by Sheetgo
License: GNU General Public License, version 3 (GPL-3.0)
http://www.opensource.org/licenses/gpl-3.0.html
----------------------------------------------------------------------------------------------------------------
Changelog:
1.0.0 Initial release
1.1.0 Auto configuration
*================================================================================================================*/
/**
* Project Settings
* @type {JSON}
*/
SETTINGS = {
// Spreadsheet name
sheetName: "Data",
// Document Url
documentUrl: null,
// Template Url
templateUrl: '14oTfL_zUbBdRD4VXY8U0NAJjQ4cKNxHGBax-bfH5NDs',
// Set name spreadsheet
spreadsheetName: 'Invoice data',
//Set name document
documentName: 'Invoice Template',
// Sheet Settings
sheetSettings: "Settings",
// Column Settings
col: {
templateId: "B1",
count: "B2",
folderId: "B3",
systemCreated: "B4"
}
};
/**
* This funcion will run when you open the spreadsheet. It creates a Spreadsheet menu option to run the spript
*/
function onOpen() {
// Adds a custom menu to the spreadsheet.
SpreadsheetApp.getUi()
.createMenu('Invoice Generator')
.addItem('Generate Invoices', 'sendInvoice')
.addToUi();
}
/**
* This function Create system
*/
function createSystem() {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get name tab
var sheetSettings = ss.getSheetByName(SETTINGS.sheetSettings);
// Checks function createSystem is run
var systemCreated = sheetSettings.getRange(SETTINGS.col.systemCreated);
if (!systemCreated.getValue()) {
systemCreated.setValue('True');
} else {
showUiDialog('Warnning', 'Solution has already been created!');
return;
}
// Checks if cell Count exists
var count = sheetSettings.getRange(SETTINGS.col.count);
if (!count.getValue()) {
count.setValue(0);
}
// Create the Solution folder on users Drive
var invoiceFolder = DriveApp.createFolder('Invoice Folder');
var folder = invoiceFolder.createFolder('Invoices');
// Set URL Invoice Folder in tab Instructions
ss.getSheetByName('Instructions').getRange('C15').setValue(invoiceFolder.getUrl());
// Move the current Dashboard spreadsheet into the Solution folder
var file = DriveApp.getFileById(SpreadsheetApp.getActive().getId());
file.setName(SETTINGS.spreadsheetName);
// Move the sheet for invoice folder
moveFile(file, invoiceFolder);
// Move the current Dashboard template into the Solution folder
var doc = DriveApp.getFileById(SETTINGS.templateUrl);
var docCopy = doc.makeCopy(SETTINGS.documentName);
// Set tab settings document ID
sheetSettings.getRange(SETTINGS.col.templateId).setValue(docCopy.getId());
// Move an copy for invoice folder
moveFile(docCopy, invoiceFolder);
// Set folder ID
sheetSettings.getRange(SETTINGS.col.folderId).setValue(folder.getId());
// End process
showUiDialog('Success', 'Your solution is ready');
return true;
} catch (e) {
// Show the error
showUiDialog('Something went wrong', e.message)
}
}
/**
* Reads the spreadsheet data and creates the PDF invoice
*/
function sendInvoice() {
try {
// Opens the spreadsheet and access the tab containing the data
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName(SETTINGS.sheetName);
var sheetSettings = ss.getSheetByName(SETTINGS.sheetSettings);
// Checks if cell Count exists
var count = sheetSettings.getRange(SETTINGS.col.count).getValue();
if (!count) {
sheetSettings.getRange(SETTINGS.col.count).setValue(0);
}
// Checks function createSystem is run
var control = sheetSettings.getRange(SETTINGS.col.systemCreated).getValue();
if (!control) {
showUiDialog('Warnning', 'Run "Install Solution" in tab Instructions');
return;
}
// Gets all values from the instanciated tab
var sheetValues = dataSheet.getDataRange().getValues();
// Gets the PDF url column index
var pdfIndex = sheetValues[0].indexOf("PDF Url");
// Gets the user's name (will be used as the PDF file name)
var clientNameIndex = sheetValues[0].indexOf("client_name");
var counter, invoiceNumCount, pdfInvoice, invoiceId, key, values, pdfName, invoiceNumber, invoiceDate;
// Duplicate teh template on Google Drive to manipulate the data
var docId = sheetSettings.getRange(SETTINGS.col.templateId).getValue();
for (var i = 1; i < sheetValues.length; i++) {
// Creates the Invoice
if (!sheetValues[i][pdfIndex]) {
// Copy tamplate document
var invoiceId = DriveApp.getFileById(docId).makeCopy('Template Copy').getId();
// Instantiate the document
var docBody = DocumentApp.openById(invoiceId).getBody();
// Iterates over the spreadsheet columns to get the values used to write the document
for (var j = 0; j < sheetValues[i].length; j++) {
// Key and Values to be replaced
key = sheetValues[0][j];
values = sheetValues[i][j];
if (key === "date") {
// Invoice Date
invoiceDate = (values.getMonth() + 1) + "/" + values.getDate() + "/" + values.getFullYear();
replace('%date%', invoiceDate, docBody); // Write data
} else if (values) {
// Everything else appart from date values
if (key.indexOf("price") > -1 || key === "discount" || key.indexOf("total") > -1) {
replace('%' + key + '%', '$' + values.toFixed(2), docBody); // Replace values
} else if (key === "tax_id") {
replace('%' + key + '%', "Tax ID: " + values, docBody); // Replace the tax_id
} else {
replace('%' + key + '%', values, docBody)
}
} else {
replace('%' + key + '%', '', docBody) // Replace empty string
}
}
// Get last invoice count from the tab 'Count'
counter = sheetSettings.getRange(SETTINGS.col.count);
invoiceNumCount = counter.getValue() + 1;
// Format invoice name pdf
invoiceNumber = invoiceNumCount.padLeft(4, '0') + "/" + invoiceDate.split("/")[2];
replace('%number%', invoiceNumber, docBody);
// Rename the invoice document
pdfName = sheetValues[i][clientNameIndex] + " " + invoiceNumber;
DocumentApp.openById(invoiceId).setName(pdfName).saveAndClose();
// Convert the Invoice Document into a PDF file
pdfInvoice = convertPDF(invoiceId);
// Set the PDF url into the spreadsheet
dataSheet.getRange(i + 1, pdfIndex + 1).setValue(pdfInvoice[0]);
// Update invoice counter
counter.setValue(invoiceNumCount);
// Delete the original document (will leave only the PDF)
Drive.Files.remove(invoiceId);
}
}
} catch (e) {
// Show the error
showUiDialog('Something went wrong', e.message)
}
}
/**
* Move a file from one folder into another
* @param {Object} file A file object in Google Drive
* @param {Object} dest_folder A folder object in Google Drive
*/
function moveFile(file, dest_folder, isFolder) {
if (isFolder === true) {
dest_folder.addFolder(file)
} else {
dest_folder.addFile(file);
}
var parents = file.getParents();
while (parents.hasNext()) {
var folder = parents.next();
if (folder.getId() != dest_folder.getId()) {
if (isFolder === true) {
folder.removeFolder(file)
} else {
folder.removeFile(file)
}
}
}
}
/**
* Convert a Google Docs into a PDF file
* @param {string} id - File Id
* @returns {*[]}
*/
function convertPDF(id) {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SETTINGS.sheetSettings);
var doc = DocumentApp.openById(id);
var docBlob = DocumentApp.openById(id).getAs('application/pdf');
docBlob.setName(doc.getName() + ".pdf"); // Add the PDF extension
var invFolder = ss.getRange(SETTINGS.col.folderId).getValue();
var file = DriveApp.getFolderById(invFolder).createFile(docBlob);
var url = file.getUrl();
id = file.getId();
return [url, id];
}
/**
* Replace the document key/value
* @param {String} key - The document key to be replaced
* @param {String} text - The document text to be inserted
* @param {Body} body - the active document's Body.
* @returns {Element}
*/
function replace(key, text, body) {
return body.editAsText().replaceText(key, text);
}
/**
* Returns a new string that right-aligns the characters in this instance by padding them with any string on the left,
* for a specified total length.
* @param {Number} n - Number of characters to pad
* @param {String} str - The string to be padded
* @returns {string}
*/
Number.prototype.padLeft = function (n, str) {
return Array(n - String(this).length + 1).join(str || '0') + this;
};
/**
* Loads the showDialog
*/
function showDialog() {
var html = HtmlService.createHtmlOutputFromFile('iframe.html')
.setWidth(200)
.setHeight(150)
SpreadsheetApp.getUi().showModalDialog(html, 'Creating Solution..')
}
/**
* Show an UI dialog
* @param {string} title - Dialog title
* @param {string} message - Dialog message
*/
function showUiDialog(title, message) {
try {
var ui = SpreadsheetApp.getUi()
ui.alert(title, message, ui.ButtonSet.OK)
} catch (e) {
// pass
}
}