-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
335 lines (297 loc) · 13.2 KB
/
Copy pathCode.gs
File metadata and controls
335 lines (297 loc) · 13.2 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
// Release on 22/12/2022 by devHanif AKA Hanif Firdaus, made with love <3
function onOpen() {
functionMenuV2()
}
function functionMenuV2() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Form Generator - devHanif');
menu.addItem('Generate GRN Form', 'createGRNFormV22023')
menu.addToUi();
}
function createGRNFormV22023() {
// Declare variables to hold the Google Doc templates
const googleDocTemplate = DriveApp.getFileById('1j0eNPGmHoQbQ0UqXsimhB7XQDHnEuwqPGnoOQ6kxILo');
const googleDocTemplateMoreThan13 = DriveApp.getFileById('1kXnkiMahqSCPA2Ne5qVThjODVDAQcuEbA02uQiepQVs');
const googleDocTemplateMoreThan26 = DriveApp.getFileById('1ahVNErjHhbmfiELASAIFFCwXROlB3a3pgd0ebpbK3bo');
const googleDocTemplateMoreThan39 = DriveApp.getFileById('1Vt0pD6_DWk3Tg6Fu7dWTE4420d4ikUey6-ePRvxfmrA');
const googleDocTemplateMoreThan52 = DriveApp.getFileById('1hujUld9zsL7mToY3OIwjTAsmV4MrOZQGCtYWzYzoLr8');
const googleDocTemplateMoreThan65 = DriveApp.getFileById('1C6rKhsuUIfgL6tIN3UDiMiuT7Qj3hk9t_VOzaBNcbiE');
const googleDocTemplateMoreThan78 = DriveApp.getFileById('1S4sNbNeEJ8oAz8dKA_yNp1kNMxBKvyF5g1wiUXRinZA');
const googleDocTemplateMoreThan91 = DriveApp.getFileById('1Ueu9NroA3nAWwJveMiZBDu9GzzZyNZoIlgJuqvSvoRg');
const googleDocTemplateMoreThan104 = DriveApp.getFileById('1VtOaQGA7TuQlzQ5yAZZ1OXL6L3a_Pv5czXaeMwzhEks');
const googleDocTemplateMoreThan117 = DriveApp.getFileById('19FK7qU3foS4XA6U0Vufz1dVexpid864NpkBlXigGzzU');
// Declare a variable to hold the destination folder ID
const destinationFolder = DriveApp.getFolderById('1J8DorF2MCIl-k7c6xIuAEbQqsTCiYdiZ')
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Made by devHanif')
// Setup Column
const noCol = 1, dateCol = 2, supplierCol = 3, customerCol = 4, donoCol = 5, grnCol = 6, poCol = 7;
const modelCol = 8, itemnoCol = 9, descCol = 10, pnCol = 11, qtyCol = 12, rmkCol = 13;
const linkCol= 14, statusCol = 15;
// Get rows from DataRange
const rows = sheet.getDataRange().getValues();
// Setup Variable
let currentDate = "", currentGRN = "", currentSupplier = "", currentDONO = "", currentPO = "";
let currentCust = "", currentModel = "";
let currentDoc, currentDocCopy, body;
// Setup flag
let flag = 0, flagtrig13 = false, flagtrig26 = false, flagtrig39 = false, flagtrig52 = false;
let flagtrig65 = false, flagtrig78 = false, flagtrig91 = false, flagtrig104 = false, flagtrig117 = false;
// Setup flag for check if any is grn is generate
let flaggrn = 0;
// Loop to get all data in rows
for (let i = 0; i < rows.length; i++) {
// retrive data from each row.
const row = rows[i], no = row[noCol-1], date = new Date(row[dateCol-1]), supplier = row[supplierCol-1];
const customer = row[customerCol-1], dono = row[donoCol-1], grn = row[grnCol-1], po = row[poCol-1];
const model = row[modelCol-1], itemno = row[itemnoCol-1], desc = row[descCol-1], pn = row[pnCol-1];
const qty = row[qtyCol-1], rmk = row[rmkCol-1];
// Setup for DATE STYLING
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
const formattedDate = day + "/" + month + "/" + year;
// Skip with condition
if (row[1] === "GOOD RECEIVING NOTE (GRN)" || row[1] === "Year : " || row[1] === "DATE" ||
row[14] != "UNFINISHED") {
continue;
}
// If the current row is for a new supplier, create a new doc
if (supplier != currentSupplier && supplier != "") {
if (currentDocCopy) {
if(flag > 0) {
// Clear placeholders from {{NO2}} to {{REMARK13}}
for (let j = 2; j <= 52; j++) {
body.replaceText(`{{NO${j}}}`, "");
body.replaceText(`{{DESC${j}}}`, "");
body.replaceText(`{{PN${j}}}`, "");
body.replaceText(`{{QTYDO${j}}}`, "");
body.replaceText(`{{REMARK${j}}}`, "");
}
}
// Save the previous doc
currentDocCopy.saveAndClose();
flaggrn = 1;
}
currentDate = formattedDate;
currentGRN = grn;
currentSupplier = supplier;
currentDONO = dono;
currentPO = po;
currentCust = customer;
currentModel = model;
flag = 0;
currentDoc = googleDocTemplate.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (1)` ,
destinationFolder)
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
}
// If the current row is for the same customer, append the values to the current doc
else if (supplier == currentSupplier && supplier != "") {
if (currentDocCopy) {
if(flag > 0) {
// Clear placeholders from {{NO2}} to {{REMARK13}}
for (let j = 2; j <= 130; j++) {
body.replaceText(`{{NO${j}}}`, "");
body.replaceText(`{{DESC${j}}}`, "");
body.replaceText(`{{PN${j}}}`, "");
body.replaceText(`{{QTYDO${j}}}`, "");
body.replaceText(`{{REMARK${j}}}`, "");
}
}
// Save the previous doc
currentDocCopy.saveAndClose();
flaggrn = 1;
}
currentDate = formattedDate;
currentGRN = grn;
currentSupplier = supplier;
currentDONO = dono;
currentPO = po;
currentCust = customer;
currentModel = model;
flag = 0;
currentDoc = googleDocTemplate.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (1)` ,
destinationFolder)
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
}
// If flag is more than 13 then save and create new docs for item 14 and More.
if (flag >= 13 && flag <= 26 && !flagtrig13) {
currentDocCopy.saveAndClose();
flaggrn = 1;
currentDoc = googleDocTemplateMoreThan13.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (2)` ,
destinationFolder);
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
// Set the flag to true
flagtrig13 = true;
}
// If flag is more than 26 then save and create new docs for item 27 and More.
else if (flag >=26 && flag <= 39 && !flagtrig26) {
currentDocCopy.saveAndClose();
flaggrn = 1;
currentDoc = googleDocTemplateMoreThan26.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (3)` ,
destinationFolder);
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
// Set the flag to true
flagtrig26 = true;
}
// 39 > < 52
else if (flag >= 39 && flag <= 52 && !flagtrig39) {
currentDocCopy.saveAndClose();
flaggrn = 1;
currentDoc = googleDocTemplateMoreThan39.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (4)` ,
destinationFolder);
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
// Set the flag to true
flagtrig39 = true;
}
// 52 > < 65
else if (flag >= 52 && flag <= 65 && !flagtrig52) {
currentDocCopy.saveAndClose();
flaggrn = 1;
currentDoc = googleDocTemplateMoreThan52.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (5)` ,
destinationFolder);
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
// Set the flag to true
flagtrig52 = true;
}
// 65 > < 78
else if (flag >= 65 && flag <= 78 && !flagtrig65) {
currentDocCopy.saveAndClose();
flaggrn = 1;
currentDoc = googleDocTemplateMoreThan65.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (6)` ,
destinationFolder);
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
// Set the flag to true
flagtrig65 = true;
}
// 78 > < 91
else if (flag >= 78 && flag <= 91 && !flagtrig78) {
currentDocCopy.saveAndClose();
flaggrn = 1;
currentDoc = googleDocTemplateMoreThan78.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (7)` ,
destinationFolder);
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
// Set the flag to true
flagtrig78 = true;
}
// 91 > < 104
else if (flag >= 91 && flag <= 104 && !flagtrig91) {
currentDocCopy.saveAndClose();
flaggrn = 1;
currentDoc = googleDocTemplateMoreThan91.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (8)` ,
destinationFolder);
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
// Set the flag to true
flagtrig91 = true;
}
// 104 > < 117
else if (flag >= 104 && flag <= 117 && !flagtrig104) {
currentDocCopy.saveAndClose();
flaggrn = 1;
currentDoc = googleDocTemplateMoreThan104.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (9)` ,
destinationFolder);
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
// Set the flag to true
flagtrig104 = true;
}
// 117 > < 130
else if (flag >= 117 && flag <= 130 && !flagtrig117) {
currentDocCopy.saveAndClose();
flaggrn = 1;
currentDoc = googleDocTemplateMoreThan117.makeCopy(`[${currentGRN}] ${currentSupplier} - GRN Form (10)` ,
destinationFolder);
currentDocCopy = DocumentApp.openById(currentDoc.getId())
body = currentDocCopy.getBody();
//get the URL of the doc
const docUrl = currentDoc.getUrl();
sheet.getRange(i+1, linkCol).setValue(docUrl);
// Set the flag to true
flagtrig117 = true;
}
// Replace the placeholders with the values from the Spreadsheet
//HEADER
body.replaceText("{{DATE}}", currentDate);
body.replaceText('{{GRNNO}}', currentGRN);
body.replaceText('{{SUPPLIER}}', currentSupplier);
body.replaceText('{{SDONO}}', currentDONO);
body.replaceText('{{PO}}', currentPO);
body.replaceText('{{CUS}}', currentCust);
body.replaceText('{{MODEL}}', currentModel);
//BODY
body.replaceText("{{NO" + itemno + "}}", itemno);
body.replaceText("{{DESC" + itemno + "}}", desc);
body.replaceText("{{PN" + itemno + "}}", pn);
body.replaceText("{{QTYDO" + itemno + "}}", qty);
body.replaceText("{{REMARK" + itemno + "}}", rmk);
// Update "Status" column in current row to "FINISHED"
sheet.getRange(i+1, statusCol).setValue("FINISHED");
//FOR FLAG
flag++;
}
// Save and close the last document
if (currentDocCopy) {
if(flag > 0) {
// Clear placeholders from {{NO2}} to {{REMARK130}}
for (var i = 2; i <= 130; i++) {
body.replaceText(`{{NO${i}}}`, "");
body.replaceText(`{{DESC${i}}}`, "");
body.replaceText(`{{PN${i}}}`, "");
body.replaceText(`{{QTYDO${i}}}`, "");
body.replaceText(`{{REMARK${i}}}`, "");
}
}
currentDocCopy.saveAndClose();
flaggrn = 1;
//RESET FLAG
flag = 0;
flagtrig13 = false; flagtrig26 = false; flagtrig39 = false; flagtrig52 = false;
flagtrig65 = false; flagtrig78 = false; flagtrig91 = false; flagtrig104 = false;
flagtrig117 = false;
}
if (flaggrn == 1){
Browser.msgBox("Completed: A new GRN Form has been generated.");
} else {
Browser.msgBox('NO GRN Form was generated.\\n\\nReason: The status is already "FINISHED" OR\\n there is no new line for me to generate.');
}
}