| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import { cellText, isNumericLike, parseCsvTable, toSheetTable } from './spreadsheet';
- /**
- * Excel → HTML 表格转换。
- *
- * 背景:XlsxViewer 用 canvas 渲染,PC 端表格只占 410px(弹窗 1400px),
- * 且不能选文字、不能复制、不能 Ctrl+F。改为 HTML 表格。
- */
- describe('spreadsheet', () => {
- describe('cellText', () => {
- it('字符串原样', () => {
- expect(cellText('销售部')).toBe('销售部');
- });
- it('数字转字符串', () => {
- expect(cellText(20260023)).toBe('20260023');
- });
- it('布尔值', () => {
- expect(cellText(true)).toBe('TRUE');
- });
- it('🔴 ooxml 真实结构 {type:text,text}(曾显示 [object Object])', () => {
- expect(cellText({ type: 'text', text: '编号' })).toBe('编号');
- });
- it('{type:number,value}', () => {
- expect(cellText({ type: 'number', value: 1200 })).toBe('1200');
- });
- it('富文本 runs 拼接', () => {
- expect(cellText({ runs: [{ text: '总' }, { text: '计' }] })).toBe('总计');
- });
- it('公式单元格取值', () => {
- expect(cellText({ kind: 'formula', value: 42 })).toBe('42');
- });
- it('空值 → 空串', () => {
- expect(cellText(null)).toBe('');
- expect(cellText(undefined)).toBe('');
- });
- });
- describe('isNumericLike', () => {
- it('普通数字', () => {
- expect(isNumericLike('123')).toBeTrue();
- expect(isNumericLike('123.45')).toBeTrue();
- });
- it('千分位 / 百分号 / 负数', () => {
- expect(isNumericLike('1,234.56')).toBeTrue();
- expect(isNumericLike('12%')).toBeTrue();
- expect(isNumericLike('-5.5')).toBeTrue();
- });
- it('中文/文本不算数字', () => {
- expect(isNumericLike('销售部')).toBeFalse();
- expect(isNumericLike('SO-20260023')).toBeFalse();
- expect(isNumericLike('')).toBeFalse();
- });
- });
- describe('toSheetTable', () => {
- function row(index: number, cells: [number, unknown][]): { index: number; cells: { col: number; row: number; value: unknown }[] } {
- return { index, cells: cells.map(([col, value]) => ({ col, row: index, value })) };
- }
- it('🔴 按列号铺开,缺列补空(保证对齐)', () => {
- const t = toSheetTable('Sheet1', [row(0, [[0, 'A'], [2, 'C']])]);
- expect(t.rows[0].cells.map((c) => c.text)).toEqual(['A', '', 'C']);
- expect(t.colCount).toBe(3);
- });
- it('🔴 跳过完全空白的行(Excel 末尾常见大量空行)', () => {
- const t = toSheetTable('Sheet1', [
- row(0, [[0, '标题']]),
- row(1, []),
- row(2, [[0, '']]),
- row(3, [[0, '数据']]),
- ]);
- expect(t.rows.map((r) => r.cells[0].text)).toEqual(['标题', '数据']);
- });
- it('🔴 行号是 1 基(用户看到「第 1 行」)', () => {
- const t = toSheetTable('Sheet1', [row(0, [[0, 'a']]), row(1, [[0, 'b']])]);
- expect(t.rows.map((r) => r.index)).toEqual([1, 2]);
- });
- it('数字右对齐、文本左对齐', () => {
- const t = toSheetTable('Sheet1', [row(0, [[0, '123'], [1, '文本']])]);
- expect(t.rows[0].cells[0].align).toBe('right');
- expect(t.rows[0].cells[1].align).toBe('left');
- });
- it('后面的行比前面列多 → 前面的行补齐', () => {
- const t = toSheetTable('Sheet1', [
- row(0, [[0, 'a']]),
- row(1, [[0, 'b'], [1, 'c'], [2, 'd']]),
- ]);
- expect(t.rows[0].cells.length).toBe(3);
- });
- it('空表 → 空行数组', () => {
- const t = toSheetTable('Sheet1', []);
- expect(t.rows).toEqual([]);
- expect(t.colCount).toBe(0);
- });
- it('sheet 名保留', () => {
- expect(toSheetTable('销售明细', []).name).toBe('销售明细');
- });
- });
- describe('parseCsvTable', () => {
- it('基础行列 + 数字右对齐', () => {
- const t = parseCsvTable('pe', 'date,pe_all\n2017-10-20,20.06');
- expect(t.colCount).toBe(2);
- expect(t.rows.length).toBe(2);
- expect(t.rows[0].cells[0].text).toBe('date');
- expect(t.rows[1].cells[1].align).toBe('right');
- });
- it('引号包裹、引号内逗号与换行、"" 转义', () => {
- const t = parseCsvTable('s', 'a,b\n"x,y","line1\nline2"\n"say ""hi""",z');
- expect(t.rows[1].cells[0].text).toBe('x,y');
- expect(t.rows[1].cells[1].text).toBe('line1\nline2');
- expect(t.rows[2].cells[0].text).toBe('say "hi"');
- });
- it('去 BOM 与 CRLF;空行跳过', () => {
- const t = parseCsvTable('s', 'a,b\r\n1,2\r\n\r\n');
- expect(t.rows[0].cells[0].text).toBe('a');
- expect(t.rows.length).toBe(2);
- });
- });
- });
|