-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathInvoice-Generator
More file actions
190 lines (141 loc) · 5.72 KB
/
Copy pathInvoice-Generator
File metadata and controls
190 lines (141 loc) · 5.72 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
function insertTables() {
var doc = DocumentApp.openById("YOUR_DOCUMENT_ID");
var body = doc.getBody();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Invoice_Data");
var nrows = sheet.getRange("H2").getValue();
var dataAll = sheet.getRange(2, 1, nrows, 5).getDisplayValues();
var headerRow = ["Art", "Date", "Details", "Time h", "Cost €"];
var data = [];
var subTotalX = sheet.getRange("H6").getDisplayValue();
var subTotalY = sheet.getRange("H7").getDisplayValue();
var subTotalZ = sheet.getRange("H8").getDisplayValue();
var taxPercent = sheet.getRange("H10").getDisplayValue();
var taxValue = sheet.getRange("H11").getDisplayValue();
var total = sheet.getRange("H13").getDisplayValue();
var finalTotalRow = [["", "", "", "", ""],
["SubTotal", "net", "", "", subTotalZ],
["Tax Deduction", taxPercent, "", "", taxValue],
["Total", "", "", "", total]];
if(nrows <= 15) {
var dataZ = data.concat([headerRow]);
dataZ = dataZ.concat(dataAll);
dataZ = dataZ.concat(finalTotalRow);
var table1 = body.insertTable(2, dataZ);
formatTable(table1);
}
if(nrows > 15 && nrows <= 40) {
var dataY = data.concat([headerRow]);
dataY = dataY.concat(dataAll.slice(0, 14));
dataY = dataY.concat([["Sub Total", "", "", "", subTotalY]]);
//var dataZ = data.concat([["Invoice:{{invoiceno}}","","","","{{date}}"]]);
var dataZ = data.concat([headerRow]);
dataZ = dataZ.concat([["Transfer", "", "", "", subTotalY]]);
dataZ = dataZ.concat(dataAll.slice(15, nrows - 1));
dataZ = dataZ.concat(finalTotalRow);
var table1 = body.insertTable(2, dataY);
formatTable(table1, "first");
body.insertPageBreak(3);
body.insertParagraph(4, "Invoice:{{invoiceno}}\t\t\t\t\t\t\t\t\t {{date}}");
// var tableA = body.insertTable(4, [["bling","bling"]]);
// formatTableA(tableA);
var table2 = body.insertTable(5, dataZ);
formatTable(table2, "next");
}
if(nrows > 40 && nrows <= 65) {
var dataX = data.concat([headerRow]);
dataX = dataX.concat(dataAll.slice(0, 14));
dataX = dataX.concat([["Sub Total", "", "", "", subTotalX]]);
var dataY = data.concat([["Invoice:{{invoiceno}}","","","","{{date}}"]]);
dataY = dataY.concat([headerRow]);
dataY = dataY.concat([["Transfer", "", "", "", subTotalX]]);
dataY = dataY.concat(dataAll.slice(15, 39));
dataY = dataY.concat([["Total", "", "", "", subTotalY]]);
var dataZ = data.concat([["Invoice:{{invoiceno}}","","","","{{date}}"]]);
dataZ = dataZ.concat([headerRow]);
dataZ = dataZ.concat([["Transfer", "", "", "", subTotalY]]);
dataZ = dataZ.concat(dataAll.slice(40, nrows - 1));
dataZ = dataZ.concat(finalTotalRow);
var table1 = body.insertTable(2, dataX);
formatTable(table1, "first");
body.insertPageBreak(3);
body.insertParagraph(4, "Invoice:{{invoiceno}}\t\t\t\t\t\t\t\t\t {{date}}");
var table2 = body.insertTable(5, dataY);
formatTable(table2, "next");
body.insertPageBreak(6);
var table3 = body.insertTable(7, dataZ);
formatTable(table3, "next");
}
}
function formatTable(table, position){
_setColumnWidth(table);
table.setAttributes(_formatAttrs("all"));
for(var i = 0; i < table.getNumRows(); i++) {
var row = table.getRow(i);
for(var j = 0; j < row.getNumCells(); j++) {
var cell = row.getCell(j);
_setCellPaddingZero(cell);
if(position == "first") {
if(i == 0 || i == table.getNumRows() - 1) {
cell.setBackgroundColor('#D9D9D9');
cell.setAttributes(_formatAttrs("borderRow"));
}
}
if(position == "next") {
if(i == 1) {
cell.setAttributes(_formatAttrs("borderRow"));
}
if(i == 0 || i == table.getNumRows() - 1) {
cell.setBackgroundColor('#D9D9D9');
cell.setAttributes(_formatAttrs("borderRow"));
}
}
if(j == 3 || j == 4) {
cell.getChild(0).setAttributes(_formatAttrs("numbersCol"));
}
}
}
}
function _formatAttrs(type) {
var cellFormatting = {};
if(type == "all") {
cellFormatting[DocumentApp.Attribute.BORDER_WIDTH] = 0;
cellFormatting[DocumentApp.Attribute.FONT_SIZE] = 9;
cellFormatting[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.LEFT;
}
if(type == "borderRow") {
cellFormatting[DocumentApp.Attribute.BOLD] = true;
cellFormatting[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
}
if(type == "numbersCol") {
cellFormatting[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.RIGHT;
}
return cellFormatting;
}
function _setColumnWidth(table) {
table.setColumnWidth(0, 80);
table.setColumnWidth(1, 40);
table.setColumnWidth(2, 250);
table.setColumnWidth(3, 30);
table.setColumnWidth(4, 50);
}
function _setCellPaddingZero(cell) {
cell.setPaddingTop(2);
cell.setPaddingBottom(2);
cell.setPaddingLeft(2);
cell.setPaddingRight(2);
}
//function formatTableA(table) {
//
// table.setColumnWidth(0,225);
// table.setColumnWidth(1,225);
//
// table.setAttributes(_formatAttrs("all"))
//
// var cell1 = table.getRow(0).getCell(0);
// var cell2 = table.getRow(0).getCell(1);
//
// _setCellPaddingZero(cell1);
// _setCellPaddingZero(cell2);
//
//}