Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(sheet): matrix test (#3027)

VicKun8f15ea4f409c
diff --git a/packages/core/src/shared/__tests__/object-matrix.spec.ts b/packages/core/src/shared/__tests__/object-matrix.spec.ts
index 2127ab4caa..0b07bb9482 100644
--- a/packages/core/src/shared/__tests__/object-matrix.spec.ts
+++ b/packages/core/src/shared/__tests__/object-matrix.spec.ts
@@ -108,4 +108,80 @@ describe('test ObjectMatrix', () => {
2: { 1: '121', 2: '313' },
});
});
+
+ it('test getDataRange', () => {
+ const matrix = new ObjectMatrix({});
+
+ matrix.setValue(3, 3, '444');
+ matrix.setValue(2000, 2000, '555');
+ const range = matrix.getDataRange();
+
+ const range1 = matrix.getStartEndScope();
+
+ // the getDataRange and getStartEndScope should be the same
+ expect(range.startRow).toBe(3);
+ expect(range.startColumn).toBe(3);
+ expect(range.endRow).toBe(2000);
+ expect(range.endColumn).toBe(2000);
+
+ expect(range1.startRow).toBe(3);
+ expect(range1.startColumn).toBe(3);
+ expect(range1.endRow).toBe(2000);
+ expect(range1.endColumn).toBe(2000);
+ });
+
+ it('test getDataRange compare with getStartEndScope', () => {
+ const matrix = new ObjectMatrix({});
+
+ for (let i = 0; i < 100; i++) {
+ const row = Math.floor(Math.random() * 10000);
+ const col = Math.floor(Math.random() * 10000);
+ matrix.setValue(row, col, '444');
+ }
+
+ const range = matrix.getDataRange();
+ const range1 = matrix.getStartEndScope();
+
+ expect(range.startRow).toBe(range1.startRow);
+ expect(range.startColumn).toBe(range1.startColumn);
+ expect(range.endRow).toBe(range1.endRow);
+ expect(range.endColumn).toBe(range1.endColumn);
+ });
+ it('test getDataRange bigger', () => {
+ const matrix = new ObjectMatrix({});
+
+ for (let i = 0; i < 100; i++) {
+ matrix.setValue(i, i, '444');
+ }
+ matrix.setValue(3, 3, '444');
+ matrix.setValue(2000, 2000, '555');
+
+ // console.time('getDataRange');
+ const range = matrix.getDataRange();
+ // console.timeEnd('getDataRange');
+
+ // console.time('getStartEndScope');
+ const range1 = matrix.getStartEndScope();
+ // console.timeEnd('getStartEndScope');
+
+ //compare the time
+ // 100:
+ // getDataRange: 0.763ms
+ // getStartEndScope: 0.43ms
+ // 1000:
+ // getDataRange: 14.686ms
+ // getStartEndScope: 8.129ms
+ // 10000:
+ // getDataRange: 38.232ms
+ // getStartEndScope: 21.041ms
+ // 100000:
+ // getDataRange: 46.832ms
+ // getStartEndScope: 23.482ms
+
+ // the getDataRange and getStartEndScope should be the same
+ expect(range.startRow).toBe(range1.startRow);
+ expect(range.startColumn).toBe(range1.startColumn);
+ expect(range.endRow).toBe(range1.endRow);
+ expect(range.endColumn).toBe(range1.endColumn);
+ });
});
diff --git a/packages/core/src/shared/object-matrix.ts b/packages/core/src/shared/object-matrix.ts
index f509945a8f..c52a30ea5c 100644
--- a/packages/core/src/shared/object-matrix.ts
+++ b/packages/core/src/shared/object-matrix.ts
@@ -596,6 +596,34 @@ export class ObjectMatrix<T> {
return objectMatrix.getData();
}
+ /**
+ * the function can only be used in all the row and column are positive integer
+ * @description the positive integer in V8 Object is stored in a fast memory space and it is sorted when we get the keys
+ * @returns {IRange} the start and end scope of the matrix
+ */
+ getStartEndScope(): IRange {
+ let startRow = Infinity;
+ let endRow = -Infinity;
+ let startColumn = Infinity;
+ let endColumn = -Infinity;
+
+ const rows = Object.keys(this._matrix);
+ if (rows.length > 0) {
+ startRow = +rows[0];
+ endRow = +rows[rows.length - 1];
+ }
+
+ for (const row of rows) {
+ const columns = Object.keys(this._matrix[row as unknown as number]);
+ if (columns.length > 0) {
+ startColumn = Math.min(startColumn, +columns[0]);
+ endColumn = Math.max(endColumn, +columns[columns.length - 1]);
+ }
+ }
+
+ return { startRow, endRow, startColumn, endColumn };
+ }
+
getDataRange(): IRange {
let startRow = 0;
let startColumn = 0;