spreadsheet.ts
5.13 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
// 电子表格(xlsx/xls)解析为 x-data-spreadsheet 可渲染的数据结构。
// 纯计算逻辑,从 app/page.tsx 抽出。
import * as XLSX from "xlsx";
import type { SheetData } from "x-data-spreadsheet";
export const SPREADSHEET_MAX_ROWS = 5000;
export const SPREADSHEET_MAX_CELLS = 200000;
export const SPREADSHEET_MAX_FILE_BYTES = 50 * 1024 * 1024;
const FONT_UNIT_PX = 7;
const CELL_PADDING_PX = 16;
const MIN_COL_WIDTH = 50;
const MAX_AUTO_COL_WIDTH = 320;
const DEFAULT_ROW_HEIGHT = 25;
const ROW_LINE_HEIGHT = 20;
const colWidthFromExcel = (col?: { wch?: number; wpx?: number }) => {
if (!col) return undefined;
if (typeof col.wpx === "number") return col.wpx;
if (typeof col.wch === "number") return Math.round(col.wch * 7 + 5);
return undefined;
};
const isWideChar = (code: number) => (
(code >= 0x1100 && code <= 0x115f)
|| (code >= 0x2e80 && code <= 0xa4cf && code !== 0x303f)
|| (code >= 0xac00 && code <= 0xd7a3)
|| (code >= 0xf900 && code <= 0xfaff)
|| (code >= 0xff00 && code <= 0xff60)
|| (code >= 0xffe0 && code <= 0xffe6)
);
const textDisplayUnits = (text: string) => {
let units = 0;
for (const ch of text) {
units += isWideChar(ch.codePointAt(0) || 0) ? 2 : 1;
}
return units;
};
export const workbookToSpreadsheetData = (
workbook: import("xlsx").WorkBook
): { sheets: SheetData[]; truncated: boolean } => {
let truncated = false;
const sheets: SheetData[] = workbook.SheetNames.map((name) => {
const worksheet = workbook.Sheets[name];
const ref = worksheet["!ref"];
const rows: Record<number, { cells: Record<number, { text: string; style?: number }>; height?: number }> & { len?: number } = {};
const cols: Record<number, { width: number }> & { len?: number } = {};
const colMaxUnits: Record<number, number> = {};
if (ref) {
const range = XLSX.utils.decode_range(ref);
let maxRow = range.e.r;
const totalCols = range.e.c - range.s.c + 1;
const rowLimit = Math.max(
0,
Math.min(maxRow - range.s.r, SPREADSHEET_MAX_ROWS - 1, Math.floor(SPREADSHEET_MAX_CELLS / Math.max(totalCols, 1)) - 1)
);
if (range.e.r - range.s.r > rowLimit) {
truncated = true;
maxRow = range.s.r + rowLimit;
}
for (let r = range.s.r; r <= maxRow; r++) {
const cells: Record<number, { text: string; style?: number }> = {};
let hasValue = false;
for (let c = range.s.c; c <= range.e.c; c++) {
const cellAddr = XLSX.utils.encode_cell({ r, c });
const cell = worksheet[cellAddr];
if (cell && cell.v !== undefined && cell.v !== null) {
const text = String(cell.v);
cells[c - range.s.c] = { text };
hasValue = true;
const col = c - range.s.c;
const lineUnits = Math.max(...text.split("\n").map(textDisplayUnits));
colMaxUnits[col] = Math.max(colMaxUnits[col] || 0, lineUnits);
}
}
if (hasValue) {
rows[r - range.s.r] = { cells };
}
}
rows.len = maxRow - range.s.r + 1;
cols.len = totalCols;
}
const excelCols = (worksheet["!cols"] || []) as Array<{ wch?: number; wpx?: number } | undefined>;
for (let c = 0; c < (cols.len || 0); c++) {
const autoWidth = Math.min(
MAX_AUTO_COL_WIDTH,
Math.max(MIN_COL_WIDTH, (colMaxUnits[c] || 0) * FONT_UNIT_PX + CELL_PADDING_PX)
);
const storedWidth = colWidthFromExcel(excelCols[c]);
const width = Math.max(autoWidth, storedWidth || 0);
if (width) {
cols[c] = { width };
}
}
const wrapStyleIndex = 0;
Object.keys(rows).forEach((rowKey) => {
if (rowKey === "len") return;
const row = rows[Number(rowKey)];
let neededHeight = DEFAULT_ROW_HEIGHT;
Object.keys(row.cells).forEach((colKey) => {
const c = Number(colKey);
const cellData = row.cells[c];
const colWidth = cols[c]?.width || MIN_COL_WIDTH;
const capacityUnits = Math.max(1, Math.floor((colWidth - CELL_PADDING_PX) / FONT_UNIT_PX));
const segments = cellData.text.split("\n");
const totalLines = segments.reduce(
(sum, seg) => sum + Math.max(1, Math.ceil(textDisplayUnits(seg) / capacityUnits)),
0
);
if (totalLines > 1) {
cellData.style = wrapStyleIndex;
neededHeight = Math.max(neededHeight, totalLines * ROW_LINE_HEIGHT + 8);
}
});
if (neededHeight > DEFAULT_ROW_HEIGHT) {
row.height = neededHeight;
}
});
const merges = ((worksheet["!merges"] || []) as Array<{ s: { r: number; c: number }; e: { r: number; c: number } }>)
.filter((m) => ref && m.e.r < (XLSX.utils.decode_range(ref).s.r + (rows.len || 0)))
.map((m) => {
const base = ref ? XLSX.utils.decode_range(ref) : { s: { r: 0, c: 0 } };
return `${XLSX.utils.encode_cell({ r: m.s.r - base.s.r, c: m.s.c - base.s.c })}:${XLSX.utils.encode_cell({ r: m.e.r - base.s.r, c: m.e.c - base.s.c })}`;
});
return {
name,
rows,
cols,
merges,
styles: [{ textwrap: true, valign: "top" }],
} as SheetData;
});
return { sheets, truncated };
};