刘光辉
8 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
import { JnpfPrintDirectionEnum, JnpfPrintPaperSizeForType } from './define';
 
/**
 * 检查值是否是 null 或 undefined
 * @param value 要检查的值
 * @returns 如果值是 null 或 undefined,返回 true,否则返回 false
 */
export const isNullOrUndefined = (value: any) => {
  return value === null || value === undefined;
};
 
/**
 * 判断一个值是否为空对象
 * @param obj - 待检查的值
 * @returns 如果是空对象,返回 true;否则返回 false
 */
export const isEmptyObject = (obj: unknown): boolean => {
  return obj !== null && typeof obj === 'object' && !Array.isArray(obj) && Object.keys(obj).length === 0;
};
 
/**
 * 将指定值从数组中移除并将其添加到数组的末尾。
 * @param array - 要操作的数组。
 * @param  value - 要移动到数组末尾的值。
 * @returns - 操作后的数组(原数组被修改)。
 */
export const moveArrayValueToEnd = (array: any[], value: number | string) => {
  const index = array?.indexOf(value); // 找到目标值的索引
 
  if (index !== -1) {
    // 如果值存在于数组中
    const [item] = array.splice(index, 1); // 删除目标值
    array.push(item); // 添加到数组末尾
  }
 
  return array;
};
 
/**
 * 根据字母规则计算输入字符串的索引值。
 * @param input {string} - 输入的字母字符串(如 "A", "B", "AA", "AB" 等)。
 * @returns {number} - 对应的索引值。
 */
export const getIndexFromAlphabetRule = (input: string): null | number => {
  if (isNullOrUndefined(input)) {
    return null;
  }
 
  const base = 26; // 字母表的长度
  const charCodeA = 'A'.charCodeAt(0);
 
  let index = 0;
  for (let i = 0; i < input.length; i++) {
    index = index * base + (input.charCodeAt(i) - charCodeA + 1);
  }
 
  return index - 1; // 数组索引从 0 开始
};
 
/**
 * 根据索引值获取字母字符串。
 * @param index {number} - 输入的索引值(如 0, 1, 26, 27 等)。
 * @returns {string} - 对应的字母字符串(如 "A", "B", "AA", "AB" 等)。
 */
export const getAlphabetFromIndexRule = (index: number): string => {
  const base = 26; // 字母表的长度
  const charCodeA = 'A'.charCodeAt(0);
 
  let result = '';
  index += 1; // 转为从 1 开始的规则
 
  while (index > 0) {
    const remainder = (index - 1) % base;
    result = String.fromCharCode(charCodeA + remainder) + result;
    index = Math.floor((index - 1) / base);
  }
 
  return result;
};
 
/**
 * 根据类型和坐标获取箭头单元格数组
 * @param parentCellType - 父单元格类型
 * @param colName - 列名
 * @param rowName - 行名
 * @returns 单元格坐标数组
 */
// export const getSheetRelationCell = (parentCellType: string, colName?: string, rowName?: string): { row: number; col: number }[] => {
//   if (parentCellType === 'none' || !colName || !rowName) return [];
//
//   const col = getIndexFromAlphabetRule(colName);
//   const row = Number(rowName) - 1;
//
//   return [{ row, col }];
// };
 
/**
 * 修正工作表中的单元格数据。
 * 根据传入的单元格数据 (`cellData`) 和指定的行、列数量,判断是否需要修正数据。
 * 如果所有单元格数据异常(如未定义、空字符串、或无效值),则修正为默认配置;
 * 如果存在正常单元格,则返回原始数据。
 *
 */
export const correctSheetCellData = (cellData: any = {}, rowCount: number, columnCount: number, isFloatDom: boolean = false) => {
  // 单元格纠正配置
  const correctCellDataConfig = {
    t: 1,
    v: ' ',
  };
 
  // 如果 cellData 是空对象,直接返回默认初始化的单元格数据
  if (isEmptyObject(cellData)) {
    return {
      0: {
        0: correctCellDataConfig,
      },
    };
  }
 
  let totalCells = 0;
  let abnormalCells = 0;
  // 统计单元格数据中正常与异常的数量
  for (const rowKey in cellData) {
    const rowValue = cellData[rowKey] ?? {};
    for (const colKey in rowValue) {
      const { s, t, v, p } = rowValue[colKey] ?? {};
 
      // 判断异常单元格条件
      if (isNullOrUndefined(s) && !p && (v === undefined || (v === '' && t === 1) || (v === 0 && (t === 2 || t === 3)))) {
        abnormalCells++;
      }
 
      totalCells++;
    }
  }
 
  // 如果存在正常单元格,直接返回原始数据
  if (totalCells > abnormalCells) {
    return cellData;
  }
 
  // 否则需要修正数据
  for (let i = 0; i < rowCount; i++) {
    for (let j = 0; j < columnCount; j++) {
      const { custom, t, v } = cellData?.[i]?.[j] ?? {};
 
      // 判断是否需要修正(根据 isFloatDom 区分)
      const needsCorrection = isFloatDom
        ? custom === undefined && (v === undefined || (v === '' && t === 1))
        : custom === undefined && (v === undefined || (v === '' && t === 1) || (v === 0 && (t === 2 || t === 3)));
 
      // 如果发现第一个需要修正的单元格,修正并结束所有循环
      if (needsCorrection) {
        if (!cellData[i]) {
          cellData[i] = {};
        }
        cellData[i][j] = correctCellDataConfig;
 
        return cellData; // 修正完成后直接返回
      }
    }
  }
 
  // 排查不出来问题,只能返回了
  return cellData;
};
 
/**
 * 将 Base64 编码的字符串转换为 File 对象
 * @param base64String Base64 字符串(必须以 `data:` 开头)
 * @param fileName 生成的文件名
 * @param mimeType 文件 MIME 类型(如 "image/png", "application/pdf")
 * @returns 返回一个 Promise,解析后得到 File 对象
 */
export function base64ToFile(base64String: string, fileName: string, mimeType: string): Promise<File> {
  return fetch(base64String)
    .then(res => res.blob()) // 将 Base64 转换为 Blob
    .then(blob => new File([blob], fileName, { type: mimeType })); // 生成 File 对象
}
 
/**
 * 计算旋转后的边界框尺寸
 * @param {number} width 原始宽度
 * @param {number} height 原始高度
 * @param {number} angleDegrees 旋转角度(0-360,单位:度)
 * @returns {{ rotatedWidth: number; rotatedHeight: number }} 旋转后的宽度和高度
 */
export function rotatedBoundingBox(width: number, height: number, angleDegrees: number): { rotatedHeight: number; rotatedWidth: number } {
  const angle = (angleDegrees * Math.PI) / 180; // 角度转换为弧度
  const rotatedWidth = Math.abs(width * Math.cos(angle)) + Math.abs(height * Math.sin(angle));
  const rotatedHeight = Math.abs(width * Math.sin(angle)) + Math.abs(height * Math.cos(angle));
  return { rotatedHeight, rotatedWidth };
}
 
/**
 * 计算插入行和列后父格的位置
 * @param range - 选中的区域,包含起始行、列(`startRow`、`startColumn`)以及结束行、列(`endRow`、`endColumn`)
 * @param axis - 操作方向,`'row'` 表示插入行,`'col'` 表示插入列
 * @param offset - 插入的行数或列数,决定偏移量
 * @param custom - 当前单元格的数据,包含自定义父格信息(如 `topParentCellType` 和 `leftParentCellType`)
 */
export function getParentCellPosWhenInsert(range: any, axis: 'col' | 'row', offset: number, custom: any) {
  const { startColumn = 0, startRow = 0 } = range ?? {};
 
  let {
    leftParentCellCustomColName,
    leftParentCellCustomRowName,
    leftParentCellType,
    topParentCellCustomColName,
    topParentCellCustomRowName,
    topParentCellType,
  } = custom ?? {};
 
  // 上父格自定义
  if (topParentCellType === 'custom' && !isNullOrUndefined(topParentCellCustomColName) && !isNullOrUndefined(topParentCellCustomRowName)) {
    // 行操作
    if (axis === 'row') {
      const targetTopColIndex = Number(topParentCellCustomColName) - 1;
      if (startRow <= targetTopColIndex) {
        topParentCellCustomColName = (targetTopColIndex + offset + 1).toString();
      }
    }
 
    // 列操作
    if (axis === 'col') {
      const targetTopRowIndex = getIndexFromAlphabetRule(topParentCellCustomRowName);
      if (targetTopRowIndex !== null && startColumn <= targetTopRowIndex) {
        topParentCellCustomRowName = getAlphabetFromIndexRule(targetTopRowIndex + offset);
      }
    }
  }
 
  // 左父格自定义
  if (leftParentCellType === 'custom' && !isNullOrUndefined(leftParentCellCustomColName) && !isNullOrUndefined(leftParentCellCustomRowName)) {
    // 行操作
    if (axis === 'row') {
      const targetLeftColIndex = Number(leftParentCellCustomColName) - 1;
      if (startRow <= targetLeftColIndex) {
        leftParentCellCustomColName = (targetLeftColIndex + offset + 1).toString();
      }
    }
 
    // 列操作
    if (axis === 'col') {
      const targetLeftRowIndex = getIndexFromAlphabetRule(leftParentCellCustomRowName);
      if (targetLeftRowIndex !== null && startColumn <= targetLeftRowIndex) {
        leftParentCellCustomRowName = getAlphabetFromIndexRule(targetLeftRowIndex + offset);
      }
    }
  }
 
  return {
    ...custom,
    leftParentCellCustomColName,
    leftParentCellCustomRowName,
    topParentCellCustomColName,
    topParentCellCustomRowName,
  };
}
 
/**
 * 计算删除行和列后父格的位置
 * @param range - 选中的区域,包含起始行、列(`startRow`、`startColumn`)以及结束行、列(`endRow`、`endColumn`)
 * @param axis - 操作方向,`'row'` 表示插入行,`'col'` 表示插入列
 * @param offset - 插入的行数或列数,决定偏移量
 * @param custom - 当前单元格的数据,包含自定义父格信息(如 `topParentCellType` 和 `leftParentCellType`)
 */
export function getParentCellPosWhenDelete(range: any, axis: 'col' | 'row', offset: number, custom: any) {
  const { endColumn = 0, endRow = 0, startColumn = 0, startRow = 0 } = range ?? {};
 
  let {
    leftParentCellCustomColName,
    leftParentCellCustomRowName,
    leftParentCellType,
    topParentCellCustomColName,
    topParentCellCustomRowName,
    topParentCellType,
  } = custom ?? {};
 
  // 上父格自定义
  if (topParentCellType === 'custom' && !isNullOrUndefined(topParentCellCustomColName) && !isNullOrUndefined(topParentCellCustomRowName)) {
    // 行操作
    if (axis === 'row') {
      const targetTopColIndex = Number(topParentCellCustomColName) - 1;
 
      if (startRow <= targetTopColIndex && endRow >= targetTopColIndex) {
        topParentCellCustomColName = null;
        topParentCellCustomRowName = null;
      } else if (startRow <= targetTopColIndex) {
        topParentCellCustomColName = (targetTopColIndex - offset + 1).toString();
      }
    }
 
    // 列操作
    if (axis === 'col') {
      const targetTopRowIndex = getIndexFromAlphabetRule(topParentCellCustomRowName);
      if (targetTopRowIndex !== null) {
        if (startColumn <= targetTopRowIndex && endColumn >= targetTopRowIndex) {
          topParentCellCustomColName = null;
          topParentCellCustomRowName = null;
        } else if (startColumn <= targetTopRowIndex) {
          topParentCellCustomRowName = getAlphabetFromIndexRule(targetTopRowIndex - offset);
        }
      }
    }
  }
 
  // 左父格自定义
  if (leftParentCellType === 'custom' && !isNullOrUndefined(leftParentCellCustomColName) && !isNullOrUndefined(leftParentCellCustomRowName)) {
    // 行操作
    if (axis === 'row') {
      const targetLeftColIndex = Number(leftParentCellCustomColName) - 1;
 
      if (startRow <= targetLeftColIndex && endRow >= targetLeftColIndex) {
        leftParentCellCustomColName = null;
        leftParentCellCustomRowName = null;
      } else if (startRow <= targetLeftColIndex) {
        leftParentCellCustomColName = (targetLeftColIndex - offset + 1).toString();
      }
    }
 
    // 列操作
    if (axis === 'col') {
      const targetLeftRowIndex = getIndexFromAlphabetRule(leftParentCellCustomRowName);
      if (targetLeftRowIndex !== null) {
        if (startColumn <= targetLeftRowIndex && endColumn >= targetLeftRowIndex) {
          leftParentCellCustomColName = null;
          leftParentCellCustomRowName = null;
        } else if (startColumn <= targetLeftRowIndex) {
          leftParentCellCustomRowName = getAlphabetFromIndexRule(targetLeftRowIndex - offset);
        }
      }
    }
  }
 
  return {
    ...custom,
    leftParentCellCustomColName,
    leftParentCellCustomRowName,
    topParentCellCustomColName,
    topParentCellCustomRowName,
  };
}
 
/**
 * 计算移动行和列后父格的位置
 * @param sourceRange - 原选中的区域,包含起始行、列(`startRow`、`startColumn`)以及结束行、列(`endRow`、`endColumn`)
 * @param targetRange - 后影响的区域,包含起始行、列(`startRow`、`startColumn`)以及结束行、列(`endRow`、`endColumn`)
 * @param axis - 操作方向,`'row'` 表示插入行,`'col'` 表示插入列
 * @param offset - 插入的行数或列数,决定偏移量
 * @param involved - 涉及的行或列的数量
 * @param custom - 当前单元格的数据,包含自定义父格信息(如 `topParentCellType` 和 `leftParentCellType`)
 */
export function getParentCellPosWhenMove(sourceRange: any, targetRange: any, axis: 'col' | 'row', offset: number, involved: number, custom: any) {
  let {
    leftParentCellCustomColName,
    leftParentCellCustomRowName,
    leftParentCellType,
    topParentCellCustomColName,
    topParentCellCustomRowName,
    topParentCellType,
  } = custom ?? {};
 
  // 上父格自定义
  if (topParentCellType === 'custom' && !isNullOrUndefined(topParentCellCustomColName) && !isNullOrUndefined(topParentCellCustomRowName)) {
    // 行操作
    if (axis === 'row') {
      const targetTopColIndex = Number(topParentCellCustomColName) - 1;
 
      if (sourceRange.startRow <= targetTopColIndex && targetTopColIndex <= sourceRange.endRow) {
        topParentCellCustomColName = (targetTopColIndex + offset + 1).toString();
      } else if (
        (sourceRange.startRow < targetTopColIndex && targetTopColIndex < targetRange.startRow) ||
        (sourceRange.startRow > targetTopColIndex && targetTopColIndex >= targetRange.startRow)
      ) {
        const moveValue = offset > 0 ? involved : -involved;
        topParentCellCustomColName = (targetTopColIndex - moveValue + 1).toString();
      }
    }
 
    // 列操作
    if (axis === 'col') {
      const targetTopRowIndex = getIndexFromAlphabetRule(topParentCellCustomRowName);
      if (targetTopRowIndex !== null) {
        if (sourceRange.startColumn <= targetTopRowIndex && targetTopRowIndex <= sourceRange.endColumn) {
          topParentCellCustomRowName = getAlphabetFromIndexRule(targetTopRowIndex + offset);
        } else if (
          (sourceRange.startColumn < targetTopRowIndex && targetTopRowIndex < targetRange.startColumn) ||
          (sourceRange.startColumn > targetTopRowIndex && targetTopRowIndex >= targetRange.startColumn)
        ) {
          const moveValue = offset > 0 ? involved : -involved;
          topParentCellCustomRowName = getAlphabetFromIndexRule(targetTopRowIndex - moveValue);
        }
      }
    }
  }
 
  // 左父格自定义
  if (leftParentCellType === 'custom' && !isNullOrUndefined(leftParentCellCustomColName) && !isNullOrUndefined(leftParentCellCustomRowName)) {
    // 行操作
    if (axis === 'row') {
      const targetLeftColIndex = Number(leftParentCellCustomColName) - 1;
 
      if (sourceRange.startRow <= targetLeftColIndex && targetLeftColIndex <= sourceRange.endRow) {
        leftParentCellCustomColName = (targetLeftColIndex + offset + 1).toString();
      } else if (
        (sourceRange.startRow < targetLeftColIndex && targetLeftColIndex < targetRange.startRow) ||
        (sourceRange.startRow > targetLeftColIndex && targetLeftColIndex >= targetRange.startRow)
      ) {
        const moveValue = offset > 0 ? involved : -involved;
        leftParentCellCustomColName = (targetLeftColIndex - moveValue + 1).toString();
      }
    }
 
    // 列操作
    if (axis === 'col') {
      const targetLeftRowIndex = getIndexFromAlphabetRule(leftParentCellCustomRowName);
      if (targetLeftRowIndex !== null) {
        if (sourceRange.startColumn <= targetLeftRowIndex && targetLeftRowIndex <= sourceRange.endColumn) {
          leftParentCellCustomRowName = getAlphabetFromIndexRule(targetLeftRowIndex + offset);
        } else if (
          (sourceRange.startColumn < targetLeftRowIndex && targetLeftRowIndex < targetRange.startColumn) ||
          (sourceRange.startColumn > targetLeftRowIndex && targetLeftRowIndex >= targetRange.startColumn)
        ) {
          const moveValue = offset > 0 ? involved : -involved;
          leftParentCellCustomRowName = getAlphabetFromIndexRule(targetLeftRowIndex - moveValue);
        }
      }
    }
  }
 
  return {
    ...custom,
    leftParentCellCustomColName,
    leftParentCellCustomRowName,
    topParentCellCustomColName,
    topParentCellCustomRowName,
  };
}
 
/**
 * 计算移动单元格后父格的位置
 * @param {object} fromRange - 被移动的单元格的范围,包含起始和结束行列坐标
 * @param {object} offset - 行列偏移量,包含纵向(offsetRow)和横向(offsetCol)的偏移
 * @param {object} custom - 当前单元格的自定义信息,包含上父格和左父格的信息
 */
export function getParentCellPosWhenMoveCell(fromRange: any, offset: any, custom: any) {
  const { offsetCol, offsetRow } = offset ?? {};
  let {
    leftParentCellCustomColName,
    leftParentCellCustomRowName,
    leftParentCellType,
    topParentCellCustomColName,
    topParentCellCustomRowName,
    topParentCellType,
  } = custom ?? {};
 
  // 上父格自定义
  if (topParentCellType === 'custom' && !isNullOrUndefined(topParentCellCustomColName) && !isNullOrUndefined(topParentCellCustomRowName)) {
    const targetTopColIndex = Number(topParentCellCustomColName) - 1;
    const targetTopRowIndex = getIndexFromAlphabetRule(topParentCellCustomRowName);
 
    if (targetTopRowIndex !== null) {
      const isLinchpin =
        fromRange.startRow <= targetTopColIndex &&
        targetTopColIndex <= fromRange.endRow &&
        fromRange.startColumn <= targetTopRowIndex &&
        targetTopRowIndex <= fromRange.endColumn; // 当事单元格
 
      if (
        offsetRow !== 0 && // 纵向操作
        isLinchpin
      ) {
        topParentCellCustomColName = (targetTopColIndex + offsetRow + 1).toString();
      }
 
      if (
        offsetCol !== 0 && // 横向操作
        isLinchpin
      ) {
        topParentCellCustomRowName = getAlphabetFromIndexRule(targetTopRowIndex + offsetCol);
      }
    }
  }
 
  // 左父格自定义
  if (leftParentCellType === 'custom' && !isNullOrUndefined(leftParentCellCustomColName) && !isNullOrUndefined(leftParentCellCustomRowName)) {
    const targetLeftColIndex = Number(leftParentCellCustomColName) - 1;
    const targetLeftRowIndex = getIndexFromAlphabetRule(leftParentCellCustomRowName);
 
    if (targetLeftRowIndex !== null) {
      const isLinchpin =
        fromRange.startRow <= targetLeftColIndex &&
        targetLeftColIndex <= fromRange.endRow &&
        fromRange.startColumn <= targetLeftRowIndex &&
        targetLeftRowIndex <= fromRange.endColumn; // 当事单元格
 
      if (
        offsetRow !== 0 && // 纵向操作
        isLinchpin
      ) {
        leftParentCellCustomColName = (targetLeftColIndex + offsetRow + 1).toString();
      }
 
      if (
        offsetCol !== 0 && // 横向操作
        isLinchpin
      ) {
        leftParentCellCustomRowName = getAlphabetFromIndexRule(targetLeftRowIndex + offsetCol);
      }
    }
  }
 
  return {
    ...custom,
    leftParentCellCustomColName,
    leftParentCellCustomRowName,
    topParentCellCustomColName,
    topParentCellCustomRowName,
  };
}
 
/**
 * 计算清楚单元格后父格的位置
 * @param ranges - 当前选择的单元格范围列表,每个范围包含起始和结束行列
 *  @param custom - 父格的自定义信息,包括行列名称和类型
 */
export function getParentCellPosWhenClearCell(ranges: any, custom: any) {
  let {
    leftParentCellCustomColName,
    leftParentCellCustomRowName,
    leftParentCellType,
    topParentCellCustomColName,
    topParentCellCustomRowName,
    topParentCellType,
  } = custom ?? {};
 
  // 上父格自定义
  if (topParentCellType === 'custom' && !isNullOrUndefined(topParentCellCustomColName) && !isNullOrUndefined(topParentCellCustomRowName)) {
    const targetTopColIndex = Number(topParentCellCustomColName) - 1;
    const targetTopRowIndex = getIndexFromAlphabetRule(topParentCellCustomRowName);
 
    if (targetTopRowIndex !== null) {
      const isLinchpin = ranges.some(({ endColumn, endRow, startColumn, startRow }: any) => {
        return startRow <= targetTopColIndex && targetTopColIndex <= endRow && startColumn <= targetTopRowIndex && targetTopRowIndex <= endColumn;
      });
 
      if (isLinchpin) {
        topParentCellCustomColName = null;
        topParentCellCustomRowName = null;
      }
    }
  }
 
  // 左父格自定义
  if (leftParentCellType === 'custom' && !isNullOrUndefined(leftParentCellCustomColName) && !isNullOrUndefined(leftParentCellCustomRowName)) {
    const targetLeftColIndex = Number(leftParentCellCustomColName) - 1;
    const targetLeftRowIndex = getIndexFromAlphabetRule(leftParentCellCustomRowName);
 
    if (targetLeftRowIndex !== null) {
      const isLinchpin = ranges.some(({ endColumn, endRow, startColumn, startRow }: any) => {
        return startRow <= targetLeftColIndex && targetLeftColIndex <= endRow && startColumn <= targetLeftRowIndex && targetLeftRowIndex <= endColumn;
      });
 
      if (isLinchpin) {
        leftParentCellCustomColName = null;
        leftParentCellCustomRowName = null;
      }
    }
  }
 
  return {
    ...custom,
    leftParentCellCustomColName,
    leftParentCellCustomRowName,
    topParentCellCustomColName,
    topParentCellCustomRowName,
  };
}
 
/**
 * 翻译小时
 * @param hour
 */
function to12HourFormat(hour: any) {
  const h = hour % 12 || 12; // 0 => 12, 13 => 1, 14 => 2 ...
  return h.toString().padStart(2, '0'); // 补零,例如 2 => '02'
}
 
/**
 * 翻译页眉页脚文本
 */
/**
 * 翻译页眉页脚文本
 */
export function translateHeaderFooterText(orientation: string, printConfig: any): string {
  if (!printConfig?.customHeaderFooterValue?.[orientation]) return '';
 
  let targetValue = printConfig?.customHeaderFooterValue[orientation];
 
  // 日期和时间的预计算,减少 slice 调用
  const dateTime = printConfig?.currentDateTime || '';
  const dateA = dateTime.slice(0, 10); // YYYY-MM-DD
  const dateB = dateTime.slice(5, 10); // YYYY-MM
  const dateC = `${dateTime.slice(5, 7)}/${dateTime.slice(8, 10)}/${dateTime.slice(0, 4)}`; // MM/DD/YYYY
  const dateD = `${dateTime.slice(5, 7)}/${dateTime.slice(8, 10)}`; // MM/DD
  const timeA = dateTime.slice(11, 19); // HH:mm:ss
  const timeB = dateTime.slice(11, 16); // HH:mm
 
  const hourFormat = to12HourFormat(dateTime.slice(11, 13));
  const hour = Number(dateTime.slice(11, 13));
 
  const timeC = `${hour < 13 ? 'AM' : 'PM'} ${hourFormat}:${dateTime.slice(14, 19)}`; // HH:mm:ss
  const timeD = `${hour < 13 ? 'AM' : 'PM'} ${hourFormat}:${dateTime.slice(14, 16)}`; // HH:mm:ss
 
  // 依次替换占位符,保持原有业务逻辑
  targetValue = targetValue
    .replace('@ReportName', printConfig.workbookTitleText)
    .replace('@SheetName', printConfig.workSheetTitleText)
    .replace('@TotalPages', printConfig.bookTotalPage)
    .replace('@PageNumbers', printConfig.bookPageNumber)
    .replace('@SheetTotalPages', printConfig.sheetTotalPage)
    .replace('@SheetPageNumbers', printConfig.sheetPageNumber)
    .replace('@DateA', dateA)
    .replace('@DateB', dateB)
    .replace('@DateC', dateC)
    .replace('@DateD', dateD)
    .replace('@TimeA', timeA)
    .replace('@TimeB', timeB)
    .replace('@TimeC', timeC)
    .replace('@TimeD', timeD);
 
  return targetValue;
}
 
/**
 * 获取打印页面的样式
 * @param paperType - 纸张类型,对应 `JnpfPrintPaperSizeForType` 中的键值
 * @param direction - 打印方向,取值为 `JnpfPrintDirectionEnum.portrait`(纵向)或 `JnpfPrintDirectionEnum.landscape`(横向)
 * @returns 一个包含打印样式的 `<style>` 元素
 */
export function getPrintPageStyle(paperType: string, direction: string) {
  const { h, w } = JnpfPrintPaperSizeForType[paperType as keyof typeof JnpfPrintPaperSizeForType] ?? {};
  const width = direction === JnpfPrintDirectionEnum?.portrait ? w : h;
  const height = direction === JnpfPrintDirectionEnum?.portrait ? h : w;
  const style = `
@page {
    size: ${width}px ${height}px;
}
@page {
    margin: 0;
    visibility: hidden;
}
@media print {
    body > * {
        display: none!important;
    }
    #jnpfReportPrint, #jnpfReportPrint * {
        display: block!important;
        height: fit-content;
        overflow: visible;
        top: 0;
        width: fit-content;
    }
    #jnpfReportPrint .printContainer {
        page-break-after: always!important;
        height: ${height}px;
        width: ${width}px;
        position: relative;
    }
}`;
  const $style = document.createElement('style');
  $style.innerHTML = style;
  $style.className = 'jnpfPrintCss';
  return $style;
}