spreadsheet.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { cellText, isNumericLike, parseCsvTable, toSheetTable } from './spreadsheet';
  2. /**
  3. * Excel → HTML 表格转换。
  4. *
  5. * 背景:XlsxViewer 用 canvas 渲染,PC 端表格只占 410px(弹窗 1400px),
  6. * 且不能选文字、不能复制、不能 Ctrl+F。改为 HTML 表格。
  7. */
  8. describe('spreadsheet', () => {
  9. describe('cellText', () => {
  10. it('字符串原样', () => {
  11. expect(cellText('销售部')).toBe('销售部');
  12. });
  13. it('数字转字符串', () => {
  14. expect(cellText(20260023)).toBe('20260023');
  15. });
  16. it('布尔值', () => {
  17. expect(cellText(true)).toBe('TRUE');
  18. });
  19. it('🔴 ooxml 真实结构 {type:text,text}(曾显示 [object Object])', () => {
  20. expect(cellText({ type: 'text', text: '编号' })).toBe('编号');
  21. });
  22. it('{type:number,value}', () => {
  23. expect(cellText({ type: 'number', value: 1200 })).toBe('1200');
  24. });
  25. it('富文本 runs 拼接', () => {
  26. expect(cellText({ runs: [{ text: '总' }, { text: '计' }] })).toBe('总计');
  27. });
  28. it('公式单元格取值', () => {
  29. expect(cellText({ kind: 'formula', value: 42 })).toBe('42');
  30. });
  31. it('空值 → 空串', () => {
  32. expect(cellText(null)).toBe('');
  33. expect(cellText(undefined)).toBe('');
  34. });
  35. });
  36. describe('isNumericLike', () => {
  37. it('普通数字', () => {
  38. expect(isNumericLike('123')).toBeTrue();
  39. expect(isNumericLike('123.45')).toBeTrue();
  40. });
  41. it('千分位 / 百分号 / 负数', () => {
  42. expect(isNumericLike('1,234.56')).toBeTrue();
  43. expect(isNumericLike('12%')).toBeTrue();
  44. expect(isNumericLike('-5.5')).toBeTrue();
  45. });
  46. it('中文/文本不算数字', () => {
  47. expect(isNumericLike('销售部')).toBeFalse();
  48. expect(isNumericLike('SO-20260023')).toBeFalse();
  49. expect(isNumericLike('')).toBeFalse();
  50. });
  51. });
  52. describe('toSheetTable', () => {
  53. function row(index: number, cells: [number, unknown][]): { index: number; cells: { col: number; row: number; value: unknown }[] } {
  54. return { index, cells: cells.map(([col, value]) => ({ col, row: index, value })) };
  55. }
  56. it('🔴 按列号铺开,缺列补空(保证对齐)', () => {
  57. const t = toSheetTable('Sheet1', [row(0, [[0, 'A'], [2, 'C']])]);
  58. expect(t.rows[0].cells.map((c) => c.text)).toEqual(['A', '', 'C']);
  59. expect(t.colCount).toBe(3);
  60. });
  61. it('🔴 跳过完全空白的行(Excel 末尾常见大量空行)', () => {
  62. const t = toSheetTable('Sheet1', [
  63. row(0, [[0, '标题']]),
  64. row(1, []),
  65. row(2, [[0, '']]),
  66. row(3, [[0, '数据']]),
  67. ]);
  68. expect(t.rows.map((r) => r.cells[0].text)).toEqual(['标题', '数据']);
  69. });
  70. it('🔴 行号是 1 基(用户看到「第 1 行」)', () => {
  71. const t = toSheetTable('Sheet1', [row(0, [[0, 'a']]), row(1, [[0, 'b']])]);
  72. expect(t.rows.map((r) => r.index)).toEqual([1, 2]);
  73. });
  74. it('数字右对齐、文本左对齐', () => {
  75. const t = toSheetTable('Sheet1', [row(0, [[0, '123'], [1, '文本']])]);
  76. expect(t.rows[0].cells[0].align).toBe('right');
  77. expect(t.rows[0].cells[1].align).toBe('left');
  78. });
  79. it('后面的行比前面列多 → 前面的行补齐', () => {
  80. const t = toSheetTable('Sheet1', [
  81. row(0, [[0, 'a']]),
  82. row(1, [[0, 'b'], [1, 'c'], [2, 'd']]),
  83. ]);
  84. expect(t.rows[0].cells.length).toBe(3);
  85. });
  86. it('空表 → 空行数组', () => {
  87. const t = toSheetTable('Sheet1', []);
  88. expect(t.rows).toEqual([]);
  89. expect(t.colCount).toBe(0);
  90. });
  91. it('sheet 名保留', () => {
  92. expect(toSheetTable('销售明细', []).name).toBe('销售明细');
  93. });
  94. });
  95. describe('parseCsvTable', () => {
  96. it('基础行列 + 数字右对齐', () => {
  97. const t = parseCsvTable('pe', 'date,pe_all\n2017-10-20,20.06');
  98. expect(t.colCount).toBe(2);
  99. expect(t.rows.length).toBe(2);
  100. expect(t.rows[0].cells[0].text).toBe('date');
  101. expect(t.rows[1].cells[1].align).toBe('right');
  102. });
  103. it('引号包裹、引号内逗号与换行、"" 转义', () => {
  104. const t = parseCsvTable('s', 'a,b\n"x,y","line1\nline2"\n"say ""hi""",z');
  105. expect(t.rows[1].cells[0].text).toBe('x,y');
  106. expect(t.rows[1].cells[1].text).toBe('line1\nline2');
  107. expect(t.rows[2].cells[0].text).toBe('say "hi"');
  108. });
  109. it('去 BOM 与 CRLF;空行跳过', () => {
  110. const t = parseCsvTable('s', 'a,b\r\n1,2\r\n\r\n');
  111. expect(t.rows[0].cells[0].text).toBe('a');
  112. expect(t.rows.length).toBe(2);
  113. });
  114. });
  115. });