刘光辉
11 小时以前 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
import { Disposable, ICellData, IUniverInstanceService, UniverInstanceType, Workbook } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
import { SheetSkeletonManagerService } from '@univerjs/sheets-ui';
 
export class JnpfSheetsCellService extends Disposable {
  constructor(
    @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
    @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
  ) {
    super();
  }
 
  // 获取所有单元格数据
  public getAllCellData() {
    const sheet = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet();
    return sheet.getCellMatrix().getMatrix();
  }
 
  // 获取指定单元格数据
  public getCellData(col: number, row: number) {
    const sheet = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet();
    const cellMatrix = sheet.getCellMatrix().getMatrix();
 
    return cellMatrix?.[row]?.[col] ?? null; // 如果未找到匹配的单元格,返回 null
  }
 
  // 获取当前单元格的自定义属性
  public getCurrentSheetCellsCustom() {
    const sheet = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet();
    const cellMatrix = sheet?.getCellMatrix()?.getMatrix() ?? {};
 
    const result: { [row: string]: { [col: string]: any } } = {};
    Object.entries(cellMatrix)?.forEach(([row, colObj]) => {
      if (colObj && typeof colObj === 'object') {
        const rowData: { [col: string]: any } = {};
 
        Object.entries(colObj)?.forEach(([col, value]: any) => {
          if (value?.custom) {
            rowData[col] = { ...value };
          }
        });
 
        if (Object.keys(rowData)?.length) {
          result[row] = rowData;
        }
      }
    });
 
    return result;
  }
 
  // 获取激活单元格的尺寸信息
  public getTargetCellSize(col: number, row: number): null | { cellHeight: number; cellWidth: number } {
    // 获取当前的工作簿实例
    const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
    if (!workbook) {
      return null;
    }
 
    const unitId = workbook.getUnitId();
 
    // 获取当前激活的工作表
    const worksheet = workbook.getActiveSheet();
    if (!worksheet) {
      return null;
    }
 
    const subUnitId = worksheet.getSheetId();
 
    // 获取对应的骨架渲染服务
    const skeleton = this._renderManagerService?.getRenderById(unitId)?.with(SheetSkeletonManagerService)?.getUnitSkeleton(unitId, subUnitId)?.skeleton as any;
 
    if (!skeleton) {
      return null;
    }
 
    // 获取指定单元格的位置信息
    const cellInfo = skeleton.getCellWithCoordByIndex(row, col);
    if (!cellInfo) {
      return null;
    }
 
    // 解构单元格信息
    const { endX, endY, isMergedMainCell, mergeInfo, startX, startY } = cellInfo;
 
    let cellWidth: number;
    let cellHeight: number;
 
    if (isMergedMainCell && mergeInfo) {
      cellWidth = mergeInfo.endX - mergeInfo.startX;
      cellHeight = mergeInfo.endY - mergeInfo.startY;
    } else {
      cellWidth = endX - startX;
      cellHeight = endY - startY;
    }
 
    return {
      cellHeight: Math.floor(cellHeight) - 2,
      cellWidth: Math.floor(cellWidth) - 2,
    };
  }
 
  // 刷新所有单元格的视图
  public refreshRangeCellsView(range: any) {
    if (!range) {
      return;
    }
 
    const originalValue = range?.getValue() ?? '';
    range?.setValue(originalValue);
  }
 
  // 设置单元格数据
  public setCellData(col: number, row: number, cellData: ICellData) {
    const sheet = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet();
    const cellMatrix = sheet.getCellMatrix();
 
    try {
      cellMatrix.setValue(row, col, cellData);
    } catch (error) {
      console.warn(error);
    }
  }
}