bep40 commited on
Commit
246f690
·
verified ·
1 Parent(s): cb9d8cb

Upload excel-fix-100px.js

Browse files
Files changed (1) hide show
  1. excel-fix-100px.js +149 -0
excel-fix-100px.js ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * V.AI STUDIO - Excel Export v1102
3
+ * FIX: Hình ảnh 100x100px trong Excel
4
+ */
5
+ (function(){
6
+ 'use strict';
7
+
8
+ function fmt(n){
9
+ if(n==null||n==='')return'0đ';
10
+ var num=typeof n==='string'?parseFloat(n.replace(/[^\d.-]/g,'')):Number(n);
11
+ if(isNaN(num))return'0đ';
12
+ return num.toLocaleString('vi-VN')+'đ';
13
+ }
14
+
15
+ async function fetchImageArrayBuffer(url){
16
+ if(!url||!url.startsWith('http')) return null;
17
+ try{
18
+ var r = await fetch(url,{mode:'cors'});
19
+ if(r.ok) return await r.arrayBuffer();
20
+ }catch(e){}
21
+ try{
22
+ var r2 = await fetch('https://api.allorigins.win/raw?url=' + encodeURIComponent(url));
23
+ if(r2.ok) return await r2.arrayBuffer();
24
+ }catch(e){}
25
+ return null;
26
+ }
27
+
28
+ function getOrderCode(){
29
+ var n = document.getElementById('quoteName')?.value || '';
30
+ var h = window.location.hash;
31
+ var today = new Date(), dd=today.getDate().toString().padStart(2,'0'), mm=(today.getMonth()+1).toString().padStart(2,'0'), yy=today.getFullYear().toString().slice(-2);
32
+ var ini='';
33
+ if(n && n.trim()){
34
+ n.trim().split(/\s+/).forEach(function(w){
35
+ if(w && /[A-Z]/.test(w.charAt(0).toUpperCase())) ini += w.charAt(0).toUpperCase();
36
+ });
37
+ }
38
+ return 'VAS'+(ini||'X')+dd+mm+yy;
39
+ }
40
+
41
+ window.exportExcel = async function(){
42
+ var cart = window.cart || [];
43
+ if(!cart.length){alert('Giỏ trống!');return;}
44
+
45
+ if(typeof ExcelJS === 'undefined'){
46
+ var s = document.createElement('script');
47
+ s.src = 'https://cdn.jsdelivr.net/npm/exceljs@4.4.0/dist/exceljs.min.js';
48
+ document.head.appendChild(s);
49
+ await new Promise(function(r){s.onload=r});
50
+ }
51
+
52
+ try{
53
+ var code = getOrderCode();
54
+ var wb = new ExcelJS.Workbook(), ws = wb.addWorksheet('Báo giá');
55
+
56
+ // Column 2 width 15 for 100px image
57
+ ws.columns = [
58
+ {width:6},{width:15},{width:30},{width:15},
59
+ {width:22},{width:8},{width:15},{width:15},{width:18},{width:16}
60
+ ];
61
+
62
+ // Header title
63
+ ws.mergeCells('A1:J1');
64
+ ws.getCell('A1').value = 'BẢNG BÁO GIÁ';
65
+ ws.getCell('A1').font = {bold:true, size:18, color:{argb:'FFDB9815'}};
66
+ ws.getCell('A1').alignment = {horizontal:'center'};
67
+ ws.getRow(1).height = 30;
68
+
69
+ // Table header
70
+ var hr = ws.getRow(3);
71
+ ['STT','Hình','Tên sản phẩm','Mã SP','Thông tin','SL','Đơn giá','Giá CK','Thành tiền','Ghi chú'].forEach(function(h,i){
72
+ var c = hr.getCell(i+1);
73
+ c.value = h;
74
+ c.font = {bold:true, color:{argb:'FFFFFFFF'}, size:9};
75
+ c.fill = {type:'pattern', pattern:'solid', fgColor:{argb:'FF003F62'}};
76
+ c.alignment = {horizontal:'center', vertical:'middle'};
77
+ });
78
+ hr.height = 20;
79
+
80
+ // Items with 100x100px images
81
+ var firstRow = 5;
82
+ await Promise.all(cart.map(async function(c, i){
83
+ var p = window.D && D[c.idx] ? D[c.idx] : null;
84
+ var row = ws.getRow(firstRow + i);
85
+ row.height = 120; // 100px + padding
86
+
87
+ var bgColor = i % 2 === 0 ? 'FFF8FAFC' : 'FFFFFFFF';
88
+
89
+ row.getCell(1).value = i+1;
90
+ row.getCell(1).alignment = {horizontal:'center', vertical:'middle'};
91
+
92
+ // IMAGE 100x100px
93
+ if((p && p.image) || c.image){
94
+ var imgUrl = p?.image || c.image;
95
+ var imgBuf = await fetchImageArrayBuffer(imgUrl);
96
+ if(imgBuf){
97
+ try{
98
+ wb.addImage(wb.addImage({buffer:imgBuf, extension:'png'}),
99
+ {tl:{col:1, row:firstRow+i-1}, ext:{width:100, height:100}});
100
+ }catch(e){}
101
+ }
102
+ }
103
+
104
+ row.getCell(3).value = c.name || (p?p.name||p.n:'') || '';
105
+ row.getCell(3).font = {bold:true, size:9};
106
+ row.getCell(4).value = c.sku || (p?p.sku||p.model:'') || '';
107
+ row.getCell(6).value = Number(c.qty||1);
108
+ row.getCell(7).value = Number(c.priceNum||(p?p.priceNum:0)||0);
109
+ row.getCell(7).numFmt = '#,##0"đ"';
110
+ row.getCell(8).value = Number(c.priceNum||(p?p.priceNum:0)||0);
111
+ row.getCell(8).numFmt = '#,##0"đ"';
112
+ row.getCell(9).value = Number(c.priceNum||(p?p.priceNum:0)||0) * Number(c.qty||1);
113
+ row.getCell(9).numFmt = '#,##0"đ"';
114
+
115
+ for(var ci=1; ci<=10; ci++){
116
+ row.getCell(ci).fill = {type:'pattern', pattern:'solid', fgColor:{argb:bgColor}};
117
+ }
118
+ }));
119
+
120
+ var lastRow = firstRow + cart.length - 1;
121
+ // Total row
122
+ ws.getCell('A'+(lastRow+2)).value = 'TỔNG CỘNG';
123
+ ws.getCell('A'+(lastRow+2)).font = {bold:true, size:13, color:{argb:'FFFFFFFF'}};
124
+ ws.getCell('A'+(lastRow+2)).fill = {type:'pattern', pattern:'solid', fgColor:{argb:'FF003F62'}};
125
+ ws.getCell('I'+(lastRow+2)).value = {formula:'SUM(I'+firstRow+':I'+lastRow+')'};
126
+ ws.getCell('I'+(lastRow+2)).numFmt = '#,##0"đ"';
127
+ ws.getCell('I'+(lastRow+2)).font = {bold:true, size:14, color:{argb:'FFF0B840'}};
128
+
129
+ wb.calcProperties.fullCalcOnLoad = true;
130
+ var buf = await wb.xlsx.writeBuffer();
131
+ var blob = new Blob([buf], {type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
132
+ var url = URL.createObjectURL(blob);
133
+ var a = document.createElement('a');
134
+ a.href = url;
135
+ a.download = code + '.xlsx';
136
+ document.body.appendChild(a);
137
+ a.click();
138
+ setTimeout(function(){ document.body.removeChild(a); URL.revokeObjectURL(url); }, 30000);
139
+
140
+ console.log('[VAI] Excel export OK - hình ảnh 100px');
141
+ if(typeof showToast === 'function') showToast('✅ Excel OK - Hình ảnh 100px');
142
+
143
+ }catch(e){
144
+ console.error('[VAI] Excel error:', e);
145
+ alert('Lỗi: '+e.message);
146
+ }
147
+ };
148
+
149
+ console.log('[VAI v1102] Excel fix loaded - 100px images ready');