-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabels.js
More file actions
739 lines (664 loc) · 32.4 KB
/
Copy pathLabels.js
File metadata and controls
739 lines (664 loc) · 32.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
// ============================================================
// Labels.gs — Label Generation (4x6, 4x4, 3x2, 2x1), Print Checkboxes
// Commodity Sampler Services
//
// NOTE: getQRCodeUrl lives in QRcode.gs (quickchart.io version)
// getBarcodeUrl is unique to this file
// ============================================================
// ============================================================
// BARCODE HELPER (unique to Labels.gs)
// ============================================================
function getBarcodeUrl(data) {
var encoded = encodeURIComponent(data);
return 'https://barcodeapi.org/api/128/' + encoded;
}
// ============================================================
// GENERATE LABELS WITH SIZE (Router)
// ============================================================
function generateLabelsWithSize(size) {
var samples = _getSamplesFromCache(false);
if (!samples) return _expiredHtml();
switch(size) {
case '3x2': return generateLabelsHtml3x2(samples);
case '4x4': return generateLabelsHtml4x4(samples);
case '2x1': return generateLabelsHtml2x1(samples);
case '4x6': default: return generateLabelsHtml4x6(samples);
}
}
function generateLabelsWithSizeNoCheckbox(size) {
var samples = _getSamplesFromCache(true);
if (!samples) return _expiredHtml();
switch(size) {
case '3x2': return generateLabelsHtml3x2NoCheckbox(samples);
case '4x4': return generateLabelsHtml4x4NoCheckbox(samples);
case '2x1': return generateLabelsHtml2x1NoCheckbox(samples);
case '4x6': default: return generateLabelsHtml4x6NoCheckbox(samples);
}
}
function _getSamplesFromCache(removeAfter) {
var cache = CacheService.getUserCache();
var json = cache.get('samplesToPrint');
if (!json) return null;
var samples = JSON.parse(json);
if (removeAfter) cache.remove('samplesToPrint');
return samples;
}
function _expiredHtml() {
return '<p style="padding:20px;font-family:Arial;">Session expired. Please try again.</p>';
}
// ============================================================
// SHARED: clearAndClose script block
// ============================================================
var CLEAR_SCRIPT = `
<script>
function clearAndClose() {
google.script.run.withSuccessHandler(function() {
google.script.host.close();
}).clearPrintCheckboxesFromCache();
}
</script>`;
// ============================================================
// 4x6 LABEL (Standard)
// ============================================================
function generateLabelsHtml4x6(samples) { return _build4x6(samples, true); }
function generateLabelsHtml4x6NoCheckbox(samples) { return _build4x6(samples, false); }
function _build4x6(samples, hasCheckbox) {
var html = `
<!DOCTYPE html>
<html>
<head>
<style>
@page { size: 4in 6in; margin: 0; }
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.label {
width: 4in; height: 6in; padding: 0.15in; box-sizing: border-box;
page-break-after: always; border: 1px solid #ccc;
display: flex; flex-direction: column;
}
.label:last-child { page-break-after: auto; }
.header { text-align: center; border-bottom: 2px solid #2E5339; padding-bottom: 6px; margin-bottom: 8px; }
.company-name { font-size: 14pt; font-weight: bold; color: #2E5339; margin: 0; }
.tagline { font-size: 9pt; font-style: italic; color: #666; margin: 2px 0; }
.contact { font-size: 8pt; color: #333; }
.content { flex: 1; }
.address-row { display: flex; gap: 10px; margin-bottom: 8px; }
.address-box { flex: 1; padding: 8px; border-radius: 4px; }
.ship-from { background: #fff3cd; border: 2px solid #ffc107; }
.ship-to { background: #e8f5e9; border: 2px solid #4A7C59; }
.address-label { font-size: 9pt; font-weight: bold; color: #666; margin-bottom: 3px; }
.address-value { font-size: 12pt; font-weight: bold; color: #000; }
.warehouse-box { background: #e3f2fd; border: 2px solid #1976d2; padding: 8px; border-radius: 4px; margin-bottom: 8px; text-align: center; }
.warehouse-label { font-size: 9pt; font-weight: bold; color: #666; }
.warehouse-value { font-size: 13pt; font-weight: bold; color: #1976d2; }
.shipping-method { text-align: center; font-size: 12pt; font-weight: bold; margin-bottom: 8px; padding: 6px; background: #f5f5f5; border-radius: 4px; }
.field { font-size: 11pt; margin: 4px 0; }
.field-label { font-weight: bold; display: inline-block; width: 90px; }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; }
.codes-row { display: flex; justify-content: space-between; align-items: center; margin-top: auto; padding-top: 8px; border-top: 1px solid #ddd; }
.qr-code { width: 1in; height: 1in; }
.barcode-container { flex: 1; text-align: center; padding-left: 10px; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.barcode { height: 0.7in; width: 100%; max-width: 2.6in; }
.barcode-text { font-size: 9pt; font-weight: bold; margin-top: 2px; }
.print-btn { display: block; margin: 20px auto; padding: 15px 40px; font-size: 16pt; background: #4A7C59; color: white; border: none; cursor: pointer; border-radius: 5px; }
.print-btn:hover { background: #3a6249; }
.clear-btn { display: block; margin: 10px auto; padding: 10px 30px; font-size: 12pt; background: #dc3545; color: white; border: none; cursor: pointer; border-radius: 5px; }
.close-btn { display: block; margin: 10px auto; padding: 10px 30px; font-size: 12pt; background: #6c757d; color: white; border: none; cursor: pointer; border-radius: 5px; }
@media print { .no-print { display: none; } body { margin: 0; } .label { border: none; } }
</style>
</head>
<body>
<div class="no-print" style="text-align: center; padding: 15px; background: #f0f0f0;">
<button class="print-btn" onclick="window.print()">🖨️ Print ${samples.length} Labels (4×6)</button>
${hasCheckbox
? '<button class="clear-btn" onclick="clearAndClose()">Clear Checkboxes & Close</button>'
: '<button class="close-btn" onclick="google.script.host.close()">Close</button>'}
</div>
`;
samples.forEach(function(s) {
var qrData = 'CS:' + s.csSample + '|' + s.container + '|' + s.mark + '|' + s.cargo;
var qrUrl = getQRCodeUrl(qrData, 120);
var barcodeUrl = getBarcodeUrl(s.csSample);
html += `
<div class="label">
<div class="header">
<div class="company-name">COMMODITY SAMPLER SERVICES</div>
<div class="tagline">Integrity Through Independence</div>
<div class="contact">info@commoditysampler.com | 732-583-0666</div>
</div>
<div class="content">
<div class="address-row">
<div class="address-box ship-from">
<div class="address-label">📤 SHIP FROM</div>
<div class="address-value">${s.sender || 'N/A'}</div>
</div>
<div class="address-box ship-to">
<div class="address-label">📦 SHIP TO</div>
<div class="address-value">${s.receiver || 'N/A'}</div>
</div>
</div>
<div class="warehouse-box">
<div class="warehouse-label">🏭 WAREHOUSE</div>
<div class="warehouse-value">${s.warehouse || 'N/A'}</div>
</div>
<div class="shipping-method">🚚 ${s.shippingProcess || 'Standard Shipping'}</div>
<div class="field-grid">
<div class="field"><span class="field-label">Container:</span> ${s.container || '-'}</div>
<div class="field"><span class="field-label">Mark:</span> ${s.mark || '-'}</div>
<div class="field"><span class="field-label">Cargo:</span> ${s.cargo || '-'}</div>
<div class="field"><span class="field-label">Reference:</span> ${s.reference || '-'}</div>
<div class="field"><span class="field-label">Description:</span> ${s.description || '-'}</div>
<div class="field"><span class="field-label">Bags:</span> ${s.bagCount || '-'}</div>
<div class="field"><span class="field-label">Sample Wt:</span> ${s.sampleWeight || '-'}</div>
<div class="field"><span class="field-label">Order:</span> ${s.sampleOrderNum || '-'}</div>
</div>
</div>
<div class="codes-row">
<img src="${qrUrl}" class="qr-code" alt="QR">
<div class="barcode-container">
<img src="${barcodeUrl}" class="barcode" alt="Barcode">
<div class="barcode-text">${s.csSample}</div>
</div>
</div>
</div>
`;
});
html += (hasCheckbox ? CLEAR_SCRIPT : '') + `</body></html>`;
return html;
}
// ============================================================
// 4x4 LABEL (Compact)
// ============================================================
function generateLabelsHtml4x4(samples) { return _build4x4(samples, true); }
function generateLabelsHtml4x4NoCheckbox(samples) { return _build4x4(samples, false); }
function _build4x4(samples, hasCheckbox) {
var html = `
<!DOCTYPE html>
<html>
<head>
<style>
@page { size: 4in 4in; margin: 0; }
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.label {
width: 4in; height: 4in; padding: 0.1in; box-sizing: border-box;
page-break-after: always; border: 1px solid #ccc;
display: flex; flex-direction: column;
}
.label:last-child { page-break-after: auto; }
.header { text-align: center; border-bottom: 2px solid #2E5339; padding-bottom: 4px; margin-bottom: 6px; }
.company-name { font-size: 11pt; font-weight: bold; color: #2E5339; margin: 0; }
.tagline { font-size: 7pt; font-style: italic; color: #666; margin: 1px 0; }
.contact { font-size: 7pt; color: #333; }
.content { flex: 1; }
.address-row { display: flex; gap: 6px; margin-bottom: 5px; }
.address-box { flex: 1; padding: 5px; border-radius: 3px; }
.ship-from { background: #fff3cd; border: 2px solid #ffc107; }
.ship-to { background: #e8f5e9; border: 2px solid #4A7C59; }
.address-label { font-size: 7pt; font-weight: bold; color: #666; margin-bottom: 2px; }
.address-value { font-size: 10pt; font-weight: bold; color: #000; }
.warehouse-box { background: #e3f2fd; border: 2px solid #1976d2; padding: 5px; border-radius: 3px; margin-bottom: 5px; text-align: center; }
.warehouse-label { font-size: 7pt; font-weight: bold; color: #666; }
.warehouse-value { font-size: 10pt; font-weight: bold; color: #1976d2; }
.shipping-method { text-align: center; font-size: 9pt; font-weight: bold; margin-bottom: 5px; padding: 4px; background: #f5f5f5; border-radius: 3px; }
.field { font-size: 9pt; margin: 2px 0; }
.field-label { font-weight: bold; display: inline-block; width: 75px; }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 8px; }
.codes-row { display: flex; justify-content: space-between; align-items: center; margin-top: auto; padding-top: 5px; border-top: 1px solid #ddd; }
.qr-code { width: 0.85in; height: 0.85in; }
.barcode-container { flex: 1; text-align: center; padding-left: 8px; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.barcode { height: 0.55in; width: 100%; max-width: 2.2in; }
.barcode-text { font-size: 9pt; font-weight: bold; margin-top: 2px; }
.print-btn { display: block; margin: 20px auto; padding: 15px 40px; font-size: 16pt; background: #4A7C59; color: white; border: none; cursor: pointer; border-radius: 5px; }
.clear-btn { display: block; margin: 10px auto; padding: 10px 30px; font-size: 12pt; background: #dc3545; color: white; border: none; cursor: pointer; border-radius: 5px; }
.close-btn { display: block; margin: 10px auto; padding: 10px 30px; font-size: 12pt; background: #6c757d; color: white; border: none; cursor: pointer; border-radius: 5px; }
@media print { .no-print { display: none; } .label { border: none; } }
</style>
</head>
<body>
<div class="no-print" style="text-align: center; padding: 15px; background: #f0f0f0;">
<button class="print-btn" onclick="window.print()">🖨️ Print ${samples.length} Labels (4×4)</button>
${hasCheckbox
? '<button class="clear-btn" onclick="clearAndClose()">Clear Checkboxes & Close</button>'
: '<button class="close-btn" onclick="google.script.host.close()">Close</button>'}
</div>
`;
samples.forEach(function(s) {
var qrData = 'CS:' + s.csSample + '|' + s.container + '|' + s.mark + '|' + s.cargo;
var qrUrl = getQRCodeUrl(qrData, 100);
var barcodeUrl = getBarcodeUrl(s.csSample);
html += `
<div class="label">
<div class="header">
<div class="company-name">COMMODITY SAMPLER SERVICES</div>
<div class="tagline">Integrity Through Independence</div>
<div class="contact">info@commoditysampler.com | 732-583-0666</div>
</div>
<div class="content">
<div class="address-row">
<div class="address-box ship-from">
<div class="address-label">📤 FROM</div>
<div class="address-value">${s.sender || 'N/A'}</div>
</div>
<div class="address-box ship-to">
<div class="address-label">📦 TO</div>
<div class="address-value">${s.receiver || 'N/A'}</div>
</div>
</div>
<div class="warehouse-box">
<div class="warehouse-label">🏭 WAREHOUSE</div>
<div class="warehouse-value">${s.warehouse || 'N/A'}</div>
</div>
<div class="shipping-method">🚚 ${s.shippingProcess || 'Standard'}</div>
<div class="field-grid">
<div class="field"><span class="field-label">Container:</span> ${s.container || '-'}</div>
<div class="field"><span class="field-label">Mark:</span> ${s.mark || '-'}</div>
<div class="field"><span class="field-label">Cargo:</span> ${s.cargo || '-'}</div>
<div class="field"><span class="field-label">Bags:</span> ${s.bagCount || '-'}</div>
</div>
</div>
<div class="codes-row">
<img src="${qrUrl}" class="qr-code" alt="QR">
<div class="barcode-container">
<img src="${barcodeUrl}" class="barcode" alt="Barcode">
<div class="barcode-text">${s.csSample}</div>
</div>
</div>
</div>
`;
});
html += (hasCheckbox ? CLEAR_SCRIPT : '') + `</body></html>`;
return html;
}
// ============================================================
// 3x2 LABEL (Mid)
// ============================================================
function generateLabelsHtml3x2(samples) { return _build3x2(samples, true); }
function generateLabelsHtml3x2NoCheckbox(samples) { return _build3x2(samples, false); }
function _build3x2(samples, hasCheckbox) {
var html = `
<!DOCTYPE html>
<html>
<head>
<style>
@page { size: 3in 2in; margin: 0; }
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.label {
width: 3in; height: 2in; padding: 0.08in; box-sizing: border-box;
page-break-after: always; border: 1px solid #ccc;
display: flex; flex-direction: column;
}
.label:last-child { page-break-after: auto; }
.header { text-align: center; font-size: 8pt; font-weight: bold; color: #2E5339; border-bottom: 1px solid #2E5339; padding-bottom: 3px; margin-bottom: 4px; }
.tagline { font-size: 6pt; font-style: italic; color: #666; }
.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 8px; font-size: 9pt; margin-bottom: 4px; }
.info-item { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.info-label { font-weight: bold; color: #333; }
.address-row { display: flex; gap: 6px; margin-bottom: 4px; }
.address-box { flex: 1; padding: 3px 5px; border-radius: 3px; font-size: 8pt; }
.ship-from { background: #fff3cd; border-left: 2px solid #ffc107; }
.ship-to { background: #e8f5e9; border-left: 2px solid #4A7C59; }
.address-label { font-size: 6pt; font-weight: bold; color: #666; }
.address-value { font-size: 9pt; font-weight: bold; }
.warehouse { text-align: center; font-size: 8pt; color: #1976d2; font-weight: bold; margin-bottom: 3px; }
.barcode-container { text-align: center; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.barcode { height: 0.45in; max-width: 2.8in; }
.barcode-text { font-size: 7pt; font-weight: bold; margin-top: 1px; }
.print-btn { display: block; margin: 20px auto; padding: 15px 40px; font-size: 16pt; background: #4A7C59; color: white; border: none; cursor: pointer; border-radius: 5px; }
.clear-btn { display: block; margin: 10px auto; padding: 10px 30px; font-size: 12pt; background: #dc3545; color: white; border: none; cursor: pointer; border-radius: 5px; }
.close-btn { display: block; margin: 10px auto; padding: 10px 30px; font-size: 12pt; background: #6c757d; color: white; border: none; cursor: pointer; border-radius: 5px; }
@media print { .no-print { display: none; } .label { border: none; } }
</style>
</head>
<body>
<div class="no-print" style="text-align: center; padding: 15px; background: #f0f0f0;">
<button class="print-btn" onclick="window.print()">🖨️ Print ${samples.length} Labels (3×2)</button>
${hasCheckbox
? '<button class="clear-btn" onclick="clearAndClose()">Clear Checkboxes & Close</button>'
: '<button class="close-btn" onclick="google.script.host.close()">Close</button>'}
</div>
`;
samples.forEach(function(s) {
var barcodeUrl = getBarcodeUrl(s.csSample);
var containerShort = (s.container || '-').substring(0, 18);
var markShort = (s.mark || '-').substring(0, 18);
var cargoShort = (s.cargo || '-').substring(0, 18);
var senderShort = (s.sender || '-').substring(0, 18);
var receiverShort = (s.receiver || '-').substring(0, 18);
var warehouseShort = (s.warehouse || '-').substring(0, 20);
html += `
<div class="label">
<div class="header">
COMMODITY SAMPLER SERVICES
<div class="tagline">Integrity Through Independence</div>
</div>
<div class="address-row">
<div class="address-box ship-from">
<div class="address-label">📤 FROM</div>
<div class="address-value">${senderShort}</div>
</div>
<div class="address-box ship-to">
<div class="address-label">📦 TO</div>
<div class="address-value">${receiverShort}</div>
</div>
</div>
<div class="warehouse">🏭 ${warehouseShort}</div>
<div class="info-grid">
<div class="info-item"><span class="info-label">C:</span> ${containerShort}</div>
<div class="info-item"><span class="info-label">M:</span> ${markShort}</div>
<div class="info-item"><span class="info-label">Cargo:</span> ${cargoShort}</div>
<div class="info-item"><span class="info-label">Bags:</span> ${s.bagCount || '-'}</div>
</div>
<div class="barcode-container">
<img src="${barcodeUrl}" class="barcode" alt="Barcode">
<div class="barcode-text">${s.csSample}</div>
</div>
</div>
`;
});
html += (hasCheckbox ? CLEAR_SCRIPT : '') + `</body></html>`;
return html;
}
// ============================================================
// 2x1 LABEL (Mini)
// ============================================================
function generateLabelsHtml2x1(samples) { return _build2x1(samples, true); }
function generateLabelsHtml2x1NoCheckbox(samples) { return _build2x1(samples, false); }
function _build2x1(samples, hasCheckbox) {
var html = `
<!DOCTYPE html>
<html>
<head>
<style>
@page { size: 2in 1in; margin: 0; }
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.label {
width: 2in; height: 1in; padding: 0.03in; box-sizing: border-box;
page-break-after: always; border: 1px solid #ccc;
display: flex; flex-direction: column;
}
.label:last-child { page-break-after: auto; }
.header { text-align: center; font-size: 5pt; font-weight: bold; color: #2E5339; margin-bottom: 1px; }
.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0px 4px; font-size: 6pt; margin-bottom: 2px; }
.info-item { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.info-label { font-weight: bold; }
.barcode-container { text-align: center; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.barcode { height: 0.3in; max-width: 1.9in; }
.barcode-text { font-size: 5pt; font-weight: bold; }
.print-btn { display: block; margin: 20px auto; padding: 15px 40px; font-size: 16pt; background: #4A7C59; color: white; border: none; cursor: pointer; border-radius: 5px; }
.clear-btn { display: block; margin: 10px auto; padding: 10px 30px; font-size: 12pt; background: #dc3545; color: white; border: none; cursor: pointer; border-radius: 5px; }
.close-btn { display: block; margin: 10px auto; padding: 10px 30px; font-size: 12pt; background: #6c757d; color: white; border: none; cursor: pointer; border-radius: 5px; }
@media print { .no-print { display: none; } .label { border: none; } }
</style>
</head>
<body>
<div class="no-print" style="text-align: center; padding: 15px; background: #f0f0f0;">
<button class="print-btn" onclick="window.print()">🖨️ Print ${samples.length} Labels (2×1)</button>
${hasCheckbox
? '<button class="clear-btn" onclick="clearAndClose()">Clear Checkboxes & Close</button>'
: '<button class="close-btn" onclick="google.script.host.close()">Close</button>'}
</div>
`;
samples.forEach(function(s) {
var barcodeUrl = getBarcodeUrl(s.csSample);
var containerShort = (s.container || '-').substring(0, 14);
var markShort = (s.mark || '-').substring(0, 14);
var cargoShort = (s.cargo || '-').substring(0, 14);
var warehouseShort = (s.warehouse || '-').substring(0, 10);
html += `
<div class="label">
<div class="header">CSS | ${warehouseShort}</div>
<div class="info-grid">
<div class="info-item"><span class="info-label">C:</span> ${containerShort}</div>
<div class="info-item"><span class="info-label">M:</span> ${markShort}</div>
<div class="info-item"><span class="info-label">Cargo:</span> ${cargoShort}</div>
<div class="info-item"><span class="info-label">📦</span> ${(s.receiver || '-').substring(0, 12)}</div>
</div>
<div class="barcode-container">
<img src="${barcodeUrl}" class="barcode" alt="Barcode">
<div class="barcode-text">${s.csSample}</div>
</div>
</div>
`;
});
html += (hasCheckbox ? CLEAR_SCRIPT : '') + `</body></html>`;
return html;
}
// ============================================================
// CLEAR PRINT CHECKBOXES
// ============================================================
function clearPrintCheckboxesFromCache() {
var cache = CacheService.getUserCache();
var samplesJson = cache.get('samplesToPrint');
if (!samplesJson) return;
var samples = JSON.parse(samplesJson);
clearPrintCheckboxes(samples);
cache.remove('samplesToPrint');
}
function clearPrintCheckboxes(samples) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetGroups = {};
samples.forEach(function(s) {
if (!sheetGroups[s.sheet]) sheetGroups[s.sheet] = [];
sheetGroups[s.sheet].push(s.row);
});
for (var sheetName in sheetGroups) {
var sheet = ss.getSheetByName(sheetName);
if (!sheet) continue;
var col = _getColumnMap(sheet);
var printIdx = col['Print'];
if (printIdx === undefined) continue;
sheetGroups[sheetName].forEach(function(row) {
sheet.getRange(row, printIdx + 1).setValue(false);
});
}
}
// ============================================================
// PRINT CHECKED SAMPLES - Entry Point
// ============================================================
function printCheckedSamples() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetName = sheet.getName();
var col = _getColumnMap(sheet);
var printIdx = col['Print'];
if (printIdx === undefined) {
SpreadsheetApp.getUi().alert('No "Print" column found on this sheet.');
return;
}
var data = sheet.getDataRange().getValues();
var samples = [];
for (var i = 1; i < data.length; i++) {
if (data[i][printIdx] === true) {
samples.push({
row: i + 1,
sheet: sheetName,
csOrder: data[i][col['CS Order #']] || '',
csSample: data[i][col['CS Sample #']] || '',
sender: data[i][col['Sender']] || '',
receiver: data[i][col['Receiver']] || '',
warehouse: data[i][col['Warehouse']] || '',
description: data[i][col['Description']] || '',
sampleOrderNum: data[i][col['Sample Order #']] || '',
cargo: data[i][col['Cargo #']] || '',
mark: data[i][col['Mark #']] || '',
container: data[i][col['Container #']] || '',
reference: data[i][col['Reference']] || '',
bagCount: data[i][col['Bag Count']] || '',
weight: data[i][col['Weight']] || '',
sampleWeight: data[i][col['Sample Weight']] || '',
shippingProcess: data[i][col['Shipping Process']] || '',
comments: data[i][col['Comments']] || ''
});
}
}
if (samples.length === 0) {
SpreadsheetApp.getUi().alert('No samples checked for printing. Check the Print column for rows you want to print.');
return;
}
var cache = CacheService.getUserCache();
cache.put('samplesToPrint', JSON.stringify(samples), 300);
var html = HtmlService.createHtmlOutput(generateLabelSizeChooser(samples.length))
.setWidth(400)
.setHeight(300)
.setTitle('Print Labels');
SpreadsheetApp.getUi().showModalDialog(html, 'Print ' + samples.length + ' Labels');
}
function generateLabelSizeChooser(count) {
return `
<div style="font-family:Arial;text-align:center;padding:20px;">
<h2>Print ${count} Label${count > 1 ? 's' : ''}</h2>
<p>Choose label size:</p>
<button onclick="pickSize('4x6')" style="margin:8px;padding:12px 24px;font-size:14pt;background:#4A7C59;color:white;border:none;border-radius:5px;cursor:pointer;">4×6 Standard</button><br>
<button onclick="pickSize('4x4')" style="margin:8px;padding:12px 24px;font-size:14pt;background:#4A7C59;color:white;border:none;border-radius:5px;cursor:pointer;">4×4 Compact</button><br>
<button onclick="pickSize('3x2')" style="margin:8px;padding:12px 24px;font-size:14pt;background:#4A7C59;color:white;border:none;border-radius:5px;cursor:pointer;">3×2 Mid</button><br>
<button onclick="pickSize('2x1')" style="margin:8px;padding:12px 24px;font-size:14pt;background:#4A7C59;color:white;border:none;border-radius:5px;cursor:pointer;">2×1 Mini</button>
</div>
<script>
function pickSize(size) {
google.script.run.withSuccessHandler(function(html) {
document.open();
document.write(html);
document.close();
}).generateLabelsWithSize(size);
}
</script>`;
}
// ============================================================
// GENERATE LABELS (from selected rows)
// ============================================================
function generateLabels() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const ui = SpreadsheetApp.getUi();
const selection = sheet.getActiveRange();
const startRow = selection.getRow();
const numRows = selection.getNumRows();
if (startRow === 1) {
ui.alert('Please select data rows (not header).');
return;
}
var col = _getColumnMap(sheet);
const samples = [];
var allRows = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn()).getValues();
for (let i = 0; i < numRows; i++) {
const rowData = allRows[i];
samples.push({
row: startRow + i,
sheet: sheet.getName(),
csOrder: rowData[col['CS Order #']] || '',
csSample: rowData[col['CS Sample #']] || '',
sender: rowData[col['Sender']] || '',
receiver: rowData[col['Receiver']] || '',
warehouse: rowData[col['Warehouse']] || '',
description: rowData[col['Description']] || '',
sampleOrderNum: rowData[col['Sample Order #']] || '',
cargo: rowData[col['Cargo #']] || '',
mark: rowData[col['Mark #']] || '',
container: rowData[col['Container #']] || '',
reference: rowData[col['Reference']] || '',
bagCount: rowData[col['Bag Count']] || '',
weight: rowData[col['Weight']] || '',
sampleWeight: rowData[col['Sample Weight']] || '',
shippingProcess: rowData[col['Shipping Process']] || '',
comments: rowData[col['Comments']] || ''
});
}
if (samples.length === 0) {
ui.alert('No rows selected');
return;
}
var cache = CacheService.getUserCache();
cache.put('samplesToPrint', JSON.stringify(samples), 300);
var pickerHtml = generateLabelSizePickerNoCheckbox(samples.length);
var htmlOutput = HtmlService.createHtmlOutput(pickerHtml)
.setWidth(400)
.setHeight(320);
ui.showModalDialog(htmlOutput, 'Select Label Size - ' + samples.length + ' Labels');
}
function generateLabelSizePickerNoCheckbox(count) {
return `
<div style="font-family:Arial;text-align:center;padding:20px;">
<h2>Print ${count} Label${count > 1 ? 's' : ''}</h2>
<p>Choose label size:</p>
<button onclick="pickSize('4x6')" style="margin:8px;padding:12px 24px;font-size:14pt;background:#4A7C59;color:white;border:none;border-radius:5px;cursor:pointer;">4×6 Standard</button><br>
<button onclick="pickSize('4x4')" style="margin:8px;padding:12px 24px;font-size:14pt;background:#4A7C59;color:white;border:none;border-radius:5px;cursor:pointer;">4×4 Compact</button><br>
<button onclick="pickSize('3x2')" style="margin:8px;padding:12px 24px;font-size:14pt;background:#4A7C59;color:white;border:none;border-radius:5px;cursor:pointer;">3×2 Mid</button><br>
<button onclick="pickSize('2x1')" style="margin:8px;padding:12px 24px;font-size:14pt;background:#4A7C59;color:white;border:none;border-radius:5px;cursor:pointer;">2×1 Mini</button>
</div>
<script>
function pickSize(size) {
google.script.run.withSuccessHandler(function(html) {
document.open();
document.write(html);
document.close();
}).generateLabelsWithSizeNoCheckbox(size);
}
</script>`;
}
// ============================================================
// REPRINT LABEL
// ============================================================
function reprintLabel() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const ui = SpreadsheetApp.getUi();
const row = sheet.getActiveCell().getRow();
if (row < 2) {
ui.alert('Please select a data row');
return;
}
var col = _getColumnMap(sheet);
var data = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getValues()[0];
var sample = {
row: row,
sheet: sheet.getName(),
csOrder: data[col['CS Order #']] || '',
csSample: data[col['CS Sample #']] || '',
sender: data[col['Sender']] || '',
receiver: data[col['Receiver']] || '',
warehouse: data[col['Warehouse']] || '',
description: data[col['Description']] || '',
sampleOrderNum: data[col['Sample Order #']] || '',
cargo: data[col['Cargo #']] || '',
mark: data[col['Mark #']] || '',
container: data[col['Container #']] || '',
reference: data[col['Reference']] || '',
bagCount: data[col['Bag Count']] || '',
sampleWeight: data[col['Sample Weight']] || '',
shippingProcess: data[col['Shipping Process']] || ''
};
if (!sample.csSample) {
ui.alert('No sample number found in this row');
return;
}
var cache = CacheService.getUserCache();
cache.put('samplesToPrint', JSON.stringify([sample]), 300);
var pickerHtml = generateLabelSizePickerNoCheckbox(1);
var htmlOutput = HtmlService.createHtmlOutput(pickerHtml)
.setWidth(400)
.setHeight(350);
ui.showModalDialog(htmlOutput, 'Reprint Label: ' + sample.csSample);
}
// ============================================================
// FIX ALL PRINT CHECKBOXES
// ============================================================
function fixAllPrintCheckboxes() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = [
CONFIG.mainSheetName,
CONFIG.liveOrdersSheetName,
CONFIG.completedOrdersSheetName || 'Completed Orders',
'View - Danboy1217'
];
sheets.forEach(function(sheetName) {
var sheet = ss.getSheetByName(sheetName);
if (!sheet) return;
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var printIdx = headers.indexOf('Print');
if (printIdx === -1) return;
var lastRow = sheet.getLastRow();
if (lastRow > 1) {
sheet.getRange(2, printIdx + 1, lastRow - 1, 1).insertCheckboxes();
}
});
SpreadsheetApp.getUi().alert('✅ Print checkboxes fixed on all sheets');
}