TropangCNTC commited on
Commit
5914bd6
·
verified ·
1 Parent(s): 1f32895

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +133 -96
index.html CHANGED
@@ -391,7 +391,20 @@
391
 
392
  const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
393
 
394
- // --- 1. DAILY REPORT GENERATOR FUNCTION ---
 
 
 
 
 
 
 
 
 
 
 
 
 
395
  async function generateDailyReport() {
396
  const genBtn = document.getElementById('dailyGenBtn');
397
  const rawFileInput = document.getElementById('dailyRawDataFile');
@@ -420,53 +433,59 @@
420
  throw new Error("Daily Template file auto-load failed.");
421
  }
422
 
423
- const workbook = new ExcelJS.Workbook();
424
- await workbook.xlsx.load(cachedDailyTemplateArrayBuffer);
425
- const worksheet = workbook.worksheets[0];
426
-
427
  const rawFileBuffer = await rawFileInput.files[0].arrayBuffer();
428
- const rawWorkbook = XLSX.read(rawFileBuffer, { type: 'array', dense: true });
429
  const rawSheet = rawWorkbook.Sheets[rawWorkbook.SheetNames[0]];
430
- const rawMatrix = XLSX.utils.sheet_to_json(rawSheet, { header: 1, raw: false, defval: '' });
 
431
 
432
- progressFill.style.width = '35%';
433
- showStatus('dailyStatusMessage', `Filtering valid records from ${rawMatrix.length.toLocaleString()} raw rows...`, 'info');
434
  await sleep(50);
435
 
 
 
 
 
 
 
 
 
 
 
436
  let cleanDataRows = [];
437
  let skippedCount = 0;
438
- const totalRaw = rawMatrix.length;
439
 
440
- for (let i = 2; i < totalRaw; i++) {
441
- const row = rawMatrix[i];
442
- if (!row || row.length === 0) continue;
443
-
444
- const rawSiteCode = row[0] !== undefined && row[0] !== null ? row[0].toString().trim() : '';
445
  const cleanSiteCode = rawSiteCode.toUpperCase();
446
 
447
- const rawSiteName = row[1] ? row[1].toString().trim() : '';
448
- const apMacRaw = row[2] ? row[2].toString().trim() : '';
449
- const rawRegion = row[3] ? row[3].toString().trim() : '';
450
- const rawProvince = row[4] ? row[4].toString().trim() : '';
451
-
452
- const clientMacRaw = row[5] ? row[5].toString().trim() : '';
453
- const cleanClientMac = clientMacRaw.toUpperCase();
454
-
455
  if (!rawSiteCode || cleanSiteCode === '' || cleanSiteCode === 'NAN' || cleanSiteCode === 'NULL' || cleanSiteCode === 'UNDEFINED') {
456
  skippedCount++;
457
  continue;
458
  }
459
 
 
460
  if (!cleanSiteCode.startsWith("ELRN")) {
461
  skippedCount++;
462
  continue;
463
  }
464
 
 
 
 
465
  if (!clientMacRaw || cleanClientMac === '' || cleanClientMac === 'NAN' || cleanClientMac === 'NULL' || cleanClientMac === 'UNDEFINED') {
466
  skippedCount++;
467
  continue;
468
  }
469
 
 
 
 
 
 
470
  const formattedApMac = formatMacAddress(apMacRaw);
471
  const formattedClientMac = formatMacAddress(clientMacRaw);
472
 
@@ -490,99 +509,117 @@
490
  ""
491
  ]);
492
 
493
- if (i % 200000 === 0) {
494
- progressFill.style.width = `${35 + Math.floor((i / totalRaw) * 25)}%`;
495
- await sleep(10);
496
  }
497
  }
498
 
499
- progressFill.style.width = '65%';
500
- showStatus('dailyStatusMessage', `Updating metadata and writing ${cleanDataRows.length.toLocaleString()} rows into template...`, 'info');
501
- await sleep(50);
502
-
503
- if (project) worksheet.getCell('B4').value = ": " + project;
504
- if (supplier) worksheet.getCell('B5').value = ": " + supplier;
505
- if (lotNumber) worksheet.getCell('B6').value = ": " + lotNumber;
506
- worksheet.getCell('B7').value = formatDateString(dateVal);
507
 
508
- worksheet.getCell('B7').font = {
509
- name: 'Calibri',
510
- size: 11,
511
- bold: true,
512
- color: { argb: 'FFFF0000' }
513
- };
514
 
515
- const templateRow = worksheet.getRow(12);
516
- const templateStyles = [];
517
- for (let c = 1; c <= 7; c++) {
518
- const sampleCell = templateRow.getCell(c);
519
- templateStyles[c] = {
520
- font: sampleCell.font ? JSON.parse(JSON.stringify(sampleCell.font)) : undefined,
521
- alignment: sampleCell.alignment ? JSON.parse(JSON.stringify(sampleCell.alignment)) : undefined,
522
- border: sampleCell.border ? JSON.parse(JSON.stringify(sampleCell.border)) : {
523
- top: { style: 'thin' }, left: { style: 'thin' }, bottom: { style: 'thin' }, right: { style: 'thin' }
524
- },
525
- numFmt: sampleCell.numFmt || undefined
 
 
 
 
 
 
 
 
 
 
 
 
 
 
526
  };
527
- }
528
- const defaultBorder = {
529
- top: { style: 'thin', color: { argb: 'FF000000' } },
530
- left: { style: 'thin', color: { argb: 'FF000000' } },
531
- bottom: { style: 'thin', color: { argb: 'FF000000' } },
532
- right: { style: 'thin', color: { argb: 'FF000000' } }
533
- };
534
-
535
- let startRow = 12;
536
- for (let r = 0; r < cleanDataRows.length; r++) {
537
- const rowData = cleanDataRows[r];
538
- const row = worksheet.getRow(startRow + r);
539
 
 
 
 
540
  for (let c = 1; c <= 7; c++) {
541
- const cell = row.getCell(c);
542
- cell.value = rowData[c - 1];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543
 
 
 
 
544
  const style = templateStyles[c];
545
- if (style) {
546
- if (style.font) cell.font = style.font;
547
- if (style.alignment) cell.alignment = style.alignment;
548
- cell.border = style.border || defaultBorder;
549
- if (style.numFmt) cell.numFmt = style.numFmt;
550
- } else {
551
- cell.border = defaultBorder;
552
- }
553
  }
554
 
555
- row.commit();
 
 
 
556
 
557
- if (r % 50000 === 0) {
558
- progressFill.style.width = `${65 + Math.floor((r / cleanDataRows.length) * 20)}%`;
559
- await sleep(10);
 
 
 
 
 
 
 
 
 
 
560
  }
561
- }
562
 
563
- cleanDataRows = null;
 
564
 
565
- progressFill.style.width = '90%';
566
- showStatus('dailyStatusMessage', 'Building clean Excel file for download...', 'info');
567
- await sleep(50);
568
 
569
- const formattedDateForFile = formatDateForFilename(dateVal);
570
- const lotSuffix = lotNumber ? `_${lotNumber}` : '';
571
- const outputFilename = `DAILY CLIENT MAC ADDRESS REPORT${lotSuffix}_${formattedDateForFile}.xlsx`;
572
 
573
- const buffer = await workbook.xlsx.writeBuffer();
574
- const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
575
- const url = window.URL.createObjectURL(blob);
576
- const a = document.createElement('a');
577
- a.href = url;
578
- a.download = outputFilename;
579
- document.body.appendChild(a);
580
- a.click();
581
- document.body.removeChild(a);
582
- window.URL.revokeObjectURL(url);
583
 
584
  progressFill.style.width = '100%';
585
- showStatus('dailyStatusMessage', `Success! Generated Daily Report using exact template (${skippedCount.toLocaleString()} blank/invalid rows skipped).`, 'success');
 
586
 
587
  } catch (err) {
588
  showStatus('dailyStatusMessage', 'Error during generation: ' + err.message, 'error');
 
391
 
392
  const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
393
 
394
+
395
+ function downloadExcelBuffer(buffer, filename) {
396
+ const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
397
+ const url = window.URL.createObjectURL(blob);
398
+ const a = document.createElement('a');
399
+ a.href = url;
400
+ a.download = filename;
401
+ document.body.appendChild(a);
402
+ a.click();
403
+ document.body.removeChild(a);
404
+ window.URL.revokeObjectURL(url);
405
+ }
406
+
407
+ // --- 1. DAILY REPORT GENERATOR FUNCTION (1M+ ROWS CAPACITY + EXACT EXISTING LOGIC) ---
408
  async function generateDailyReport() {
409
  const genBtn = document.getElementById('dailyGenBtn');
410
  const rawFileInput = document.getElementById('dailyRawDataFile');
 
433
  throw new Error("Daily Template file auto-load failed.");
434
  }
435
 
436
+ // 1. Read raw file directly via cell coordinates (No 1M+ 2D matrix spike)
 
 
 
437
  const rawFileBuffer = await rawFileInput.files[0].arrayBuffer();
438
+ let rawWorkbook = XLSX.read(rawFileBuffer, { type: 'array', dense: true });
439
  const rawSheet = rawWorkbook.Sheets[rawWorkbook.SheetNames[0]];
440
+ const range = XLSX.utils.decode_range(rawSheet['!ref'] || 'A1:A1');
441
+ const totalRaw = range.e.r + 1;
442
 
443
+ progressFill.style.width = '30%';
444
+ showStatus('dailyStatusMessage', `Filtering valid records from ${totalRaw.toLocaleString()} raw rows...`, 'info');
445
  await sleep(50);
446
 
447
+ const getCellVal = (r, c) => {
448
+ if (rawSheet['!data']) {
449
+ const cell = rawSheet['!data'][r] && rawSheet['!data'][r][c];
450
+ return cell && cell.v !== undefined && cell.v !== null ? cell.v.toString().trim() : '';
451
+ }
452
+ const cellAddr = XLSX.utils.encode_cell({ r, c });
453
+ const cell = rawSheet[cellAddr];
454
+ return cell && cell.v !== undefined && cell.v !== null ? cell.v.toString().trim() : '';
455
+ };
456
+
457
  let cleanDataRows = [];
458
  let skippedCount = 0;
 
459
 
460
+ for (let i = 2; i < totalRaw; i++) { // Start Row 3 (Index 2)
461
+ const rawSiteCode = getCellVal(i, 0);
 
 
 
462
  const cleanSiteCode = rawSiteCode.toUpperCase();
463
 
464
+ // RULE 1: Skip Blank Site Code
 
 
 
 
 
 
 
465
  if (!rawSiteCode || cleanSiteCode === '' || cleanSiteCode === 'NAN' || cleanSiteCode === 'NULL' || cleanSiteCode === 'UNDEFINED') {
466
  skippedCount++;
467
  continue;
468
  }
469
 
470
+ // RULE 2: Skip Non-ELRN Site Code
471
  if (!cleanSiteCode.startsWith("ELRN")) {
472
  skippedCount++;
473
  continue;
474
  }
475
 
476
+ // RULE 3: Skip Blank Client MAC
477
+ const clientMacRaw = getCellVal(i, 5);
478
+ const cleanClientMac = clientMacRaw.toUpperCase();
479
  if (!clientMacRaw || cleanClientMac === '' || cleanClientMac === 'NAN' || cleanClientMac === 'NULL' || cleanClientMac === 'UNDEFINED') {
480
  skippedCount++;
481
  continue;
482
  }
483
 
484
+ const rawSiteName = getCellVal(i, 1);
485
+ const apMacRaw = getCellVal(i, 2);
486
+ const rawRegion = getCellVal(i, 3);
487
+ const rawProvince = getCellVal(i, 4);
488
+
489
  const formattedApMac = formatMacAddress(apMacRaw);
490
  const formattedClientMac = formatMacAddress(clientMacRaw);
491
 
 
509
  ""
510
  ]);
511
 
512
+ if (i % 100000 === 0) {
513
+ progressFill.style.width = `${30 + Math.floor((i / totalRaw) * 25)}%`;
514
+ await sleep(5);
515
  }
516
  }
517
 
518
+ // Free SheetJS memory immediately
519
+ rawWorkbook = null;
 
 
 
 
 
 
520
 
521
+ // 2. Auto-split into chunks of 400,000 rows so ExcelJS never hits V8 buffer limit (handles 1M+ rows)
522
+ const CHUNK_SIZE = 400000;
523
+ const totalValid = cleanDataRows.length;
524
+ const totalChunks = Math.max(1, Math.ceil(totalValid / CHUNK_SIZE));
525
+ const formattedDateForFile = formatDateForFilename(dateVal);
526
+ const lotSuffix = lotNumber ? `_${lotNumber}` : '';
527
 
528
+ for (let chunkIdx = 0; chunkIdx < totalChunks; chunkIdx++) {
529
+ const chunkStart = chunkIdx * CHUNK_SIZE;
530
+ const chunkEnd = Math.min(chunkStart + CHUNK_SIZE, totalValid);
531
+ const chunkRows = cleanDataRows.slice(chunkStart, chunkEnd);
532
+
533
+ const partLabel = totalChunks > 1 ? ` (Part ${chunkIdx + 1} of ${totalChunks})` : '';
534
+ showStatus('dailyStatusMessage', `Updating metadata and writing ${chunkRows.length.toLocaleString()} rows into template${partLabel}...`, 'info');
535
+ await sleep(50);
536
+
537
+ let workbook = new ExcelJS.Workbook();
538
+ await workbook.xlsx.load(cachedDailyTemplateArrayBuffer);
539
+ const worksheet = workbook.worksheets[0];
540
+
541
+ // 3. Update Header Metadata directly in ExcelJS (100% same as existing)
542
+ if (project) worksheet.getCell('B4').value = ": " + project;
543
+ if (supplier) worksheet.getCell('B5').value = ": " + supplier;
544
+ if (lotNumber) worksheet.getCell('B6').value = ": " + lotNumber;
545
+ worksheet.getCell('B7').value = formatDateString(dateVal);
546
+
547
+ // Set font color to RED in Excel output (Restored!)
548
+ worksheet.getCell('B7').font = {
549
+ name: 'Calibri',
550
+ size: 11,
551
+ bold: true,
552
+ color: { argb: 'FFFF0000' }
553
  };
 
 
 
 
 
 
 
 
 
 
 
 
554
 
555
+ // 4. Extract template styles and borders from Row 12
556
+ const templateRow = worksheet.getRow(12);
557
+ const templateStyles = [];
558
  for (let c = 1; c <= 7; c++) {
559
+ const sampleCell = templateRow.getCell(c);
560
+ templateStyles[c] = {
561
+ font: sampleCell.font ? JSON.parse(JSON.stringify(sampleCell.font)) : undefined,
562
+ alignment: sampleCell.alignment ? JSON.parse(JSON.stringify(sampleCell.alignment)) : undefined,
563
+ border: sampleCell.border ? JSON.parse(JSON.stringify(sampleCell.border)) : {
564
+ top: { style: 'thin' }, left: { style: 'thin' }, bottom: { style: 'thin' }, right: { style: 'thin' }
565
+ },
566
+ numFmt: sampleCell.numFmt || undefined
567
+ };
568
+ }
569
+ const defaultBorder = {
570
+ top: { style: 'thin', color: { argb: 'FF000000' } },
571
+ left: { style: 'thin', color: { argb: 'FF000000' } },
572
+ bottom: { style: 'thin', color: { argb: 'FF000000' } },
573
+ right: { style: 'thin', color: { argb: 'FF000000' } }
574
+ };
575
 
576
+ // Pre-build reusable column styles for 1M+ speed & low RAM
577
+ const columnCellStyles = [];
578
+ for (let c = 1; c <= 7; c++) {
579
  const style = templateStyles[c];
580
+ columnCellStyles[c] = {
581
+ font: style && style.font ? style.font : undefined,
582
+ alignment: style && style.alignment ? style.alignment : undefined,
583
+ border: style && style.border ? style.border : defaultBorder,
584
+ numFmt: style ? style.numFmt : undefined
585
+ };
 
 
586
  }
587
 
588
+ let startRow = 12;
589
+ for (let r = 0; r < chunkRows.length; r++) {
590
+ const rowData = chunkRows[r];
591
+ const row = worksheet.getRow(startRow + r);
592
 
593
+ for (let c = 1; c <= 7; c++) {
594
+ const cell = row.getCell(c);
595
+ cell.value = rowData[c - 1];
596
+ cell.style = columnCellStyles[c];
597
+ }
598
+
599
+ row.commit();
600
+
601
+ if (r % 50000 === 0) {
602
+ const basePct = 55 + Math.floor((chunkIdx / totalChunks) * 40);
603
+ progressFill.style.width = `${basePct + Math.floor((r / chunkRows.length) * (40 / totalChunks))}%`;
604
+ await sleep(5);
605
+ }
606
  }
 
607
 
608
+ const partSuffix = totalChunks > 1 ? `_Part${chunkIdx + 1}` : '';
609
+ const outputFilename = `DAILY CLIENT MAC ADDRESS REPORT${lotSuffix}_${formattedDateForFile}${partSuffix}.xlsx`;
610
 
611
+ const buffer = await workbook.xlsx.writeBuffer();
612
+ downloadExcelBuffer(buffer, outputFilename);
 
613
 
614
+ workbook = null;
615
+ await sleep(200);
616
+ }
617
 
618
+ cleanDataRows = null;
 
 
 
 
 
 
 
 
 
619
 
620
  progressFill.style.width = '100%';
621
+ const splitNote = totalChunks > 1 ? ` Split into ${totalChunks} files (400k rows/file).` : '';
622
+ showStatus('dailyStatusMessage', `Success! Generated Daily Report using exact template (${skippedCount.toLocaleString()} blank/invalid rows skipped).${splitNote}`, 'success');
623
 
624
  } catch (err) {
625
  showStatus('dailyStatusMessage', 'Error during generation: ' + err.message, 'error');