-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcel-worker.js
More file actions
68 lines (60 loc) · 1.9 KB
/
Copy pathexcel-worker.js
File metadata and controls
68 lines (60 loc) · 1.9 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
'use strict';
importScripts('xlsx.full.min.js');
function parseTime(val) {
if (!val && val !== 0) return null;
if (val instanceof Date) return isNaN(val) ? null : val;
if (typeof val === 'number') {
const d = new Date(Math.round((val - 25569) * 86400000));
return isNaN(d) ? null : d;
}
if (typeof val === 'string') {
const cleaned = val.trim().replace(/\//g, '-');
const d = new Date(cleaned);
return isNaN(d) ? null : d;
}
return null;
}
function countChars(text) {
if (!text) return 0;
return String(text).replace(/\s/g, '').length;
}
function parseExcelBuffer(buffer, fileName) {
const data = new Uint8Array(buffer);
const wb = XLSX.read(data, { type: 'array', cellDates: true, dense: false });
const ws = wb.Sheets[wb.SheetNames[0]];
const rows = XLSX.utils.sheet_to_json(ws, { header: 1, defval: '', raw: true });
const info = rows[1] || [];
const session = {
id: fileName,
filename: fileName,
wxid: String(info[1] || '').trim(),
nickname: String(info[3] || '').trim(),
remark: String(info[5] || '').trim(),
messages: [],
};
for (let i = 4; i < rows.length; i++) {
const row = rows[i];
if (!row || row.every(c => c === '' || c == null)) continue;
const content = String(row[7] || '');
session.messages.push({
seq: row[0],
time: parseTime(row[1]),
senderNickname: String(row[2] || '').trim(),
senderWxid: String(row[3] || '').trim(),
senderRemark: String(row[4] || '').trim(),
senderRole: String(row[5] || '').trim(),
msgType: String(row[6] || '').trim(),
content,
_charCount: countChars(content),
});
}
return session;
}
self.onmessage = e => {
const { fileName, buffer } = e.data || {};
try {
self.postMessage({ ok: true, session: parseExcelBuffer(buffer, fileName) });
} catch (err) {
self.postMessage({ ok: false, error: err?.message || String(err) });
}
};