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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
flush
jerryb98141513146
diff --git a/packages/core/src/Shared/ObjectArray.ts b/packages/core/src/Shared/ObjectArray.tsindex 9bc29bc0f1..3f88e78a94 100644--- a/packages/core/src/Shared/ObjectArray.ts+++ b/packages/core/src/Shared/ObjectArray.ts@@ -1,4 +1,3 @@-import { Tools } from './Tools';import { Nullable } from './Types';/**@@ -28,6 +27,23 @@ export type ObjectArrayType<T> = ObjectArray<T> | ObjectArrayPrimitiveType<T>;const define = <T>(value?: T): value is T => value !== undefined && value !== null;+const likeArr = (value: object): number => {+ let keys: string[] = Object.keys(value);+ let regexp = /^\d+$/;+ let maxKey = 0;+ for (let i = 0; i < keys.length; i++) {+ let key = keys[i];+ if (!regexp.test(key)) {+ return -1;+ }+ let seq = parseInt(key) + 1;+ if (seq > maxKey) {+ maxKey = seq;+ }+ }+ return maxKey;+};+/*** Arrays in object form and provide an array-like API*@@ -39,32 +55,42 @@ export class ObjectArray<T> {private _length: number;constructor();- constructor(array: object);+ constructor(array: ObjectArrayPrimitiveType<T>);constructor(size: number);- constructor(array: object, size: number);+ constructor(array: ObjectArrayPrimitiveType<T>, size: number);constructor(...argument: any) {- if (Tools.hasLength(argument, 1)) {- if (Tools.isObject(argument[0])) {- this._array = argument[0] as { [index: number]: T };- this._length = ObjectArray.getMaxLength(this._array);- return;- }- if (Tools.isNumber(argument[0])) {+ switch (argument.length) {+ case 0: {this._array = {};- this._length = argument[0];+ this._length = 0;return;}- }- if (Tools.hasLength(argument, 2)) {- const array = argument[0];- const size = argument[1];- this._array = array;- this._length = size;- return;- }- if (Tools.hasLength(argument, 0)) {- this._array = {};- this._length = 0;+ case 1: {+ if (typeof argument[0] === 'number') {+ this._array = {};+ this._length = argument[0];+ return;+ }+ const length = likeArr(argument[0]);+ if (length > -1) {+ this._array = argument[0];+ this._length = length;+ return;+ }+ throw new Error(+ `create object array error ${JSON.stringify(argument[0])}`+ );+ }+ case 2: {+ if (likeArr(argument[0]) > -1) {+ this._array = argument[0];+ this._length = argument[1];+ return;+ }+ throw new Error(+ `create object array error ${JSON.stringify(argument[0])}`+ );+ }}}@@ -201,7 +227,7 @@ export class ObjectArray<T> {}toJSON(): ObjectArrayPrimitiveType<T> {- return { ...this._array, length: this._length };+ return this._array;}toArray(): T[] {diff --git a/packages/core/src/Shared/ObjectMatrix.ts b/packages/core/src/Shared/ObjectMatrix.tsindex 13700ef676..455e8c8db3 100644--- a/packages/core/src/Shared/ObjectMatrix.ts+++ b/packages/core/src/Shared/ObjectMatrix.ts@@ -28,6 +28,12 @@ export class ObjectMatrix<T> {this._option = new ObjectArray<ObjectArrayPrimitiveType<T>>(matrix);}+ static MakeObjectMatrixSize<T>(size: number): ObjectMatrix<T> {+ return new ObjectMatrix({+ [size - 1]: {},+ });+ }+getMatrix() {return this._matrix;}diff --git a/packages/core/src/Sheets/Domain/Worksheet.ts b/packages/core/src/Sheets/Domain/Worksheet.tsindex ce538b7d1c..f179eaee2d 100644--- a/packages/core/src/Sheets/Domain/Worksheet.ts+++ b/packages/core/src/Sheets/Domain/Worksheet.ts@@ -54,7 +54,7 @@ import {IStyleData,IWorksheetConfig,} from '../../Interfaces';-import { Nullable, ObjectMatrix, Tools, ObjectArray, Tuples } from '../../Shared';+import { Nullable, ObjectMatrix, Tools, Tuples } from '../../Shared';import { ColumnManager } from './ColumnManager';import { Merges } from './Merges';import { Range } from './Range';@@ -586,7 +586,7 @@ export class Worksheet {actionName: InsertRowDataAction.NAME,sheetId: _sheetId,rowIndex,- rowData: new ObjectMatrix<ICellData>(new ObjectArray(numRows)).toJSON(),+ rowData: ObjectMatrix.MakeObjectMatrixSize<ICellData>(numRows).toJSON(),};const insertRow: IInsertRowActionData = {actionName: InsertRowAction.NAME,@@ -636,7 +636,7 @@ export class Worksheet {actionName: InsertRowDataAction.NAME,sheetId: _sheetId,rowIndex,- rowData: new ObjectMatrix<ICellData>(new ObjectArray(numRows)).toJSON(),+ rowData: ObjectMatrix.MakeObjectMatrixSize<ICellData>(numRows).toJSON(),};const insertRow = {actionName: InsertRowAction.NAME,@@ -691,7 +691,7 @@ export class Worksheet {actionName: InsertRowDataAction.NAME,sheetId: _sheetId,rowIndex,- rowData: new ObjectMatrix<ICellData>(new ObjectArray(numRows)).toJSON(),+ rowData: ObjectMatrix.MakeObjectMatrixSize<ICellData>(numRows).toJSON(),};const insertRow = {actionName: InsertRowAction.NAME,diff --git a/packages/core/test/Shared/ObjectMatrix.test.ts b/packages/core/test/Shared/ObjectMatrix.test.tsindex 491bef4905..d4b2cd8f2e 100644--- a/packages/core/test/Shared/ObjectMatrix.test.ts+++ b/packages/core/test/Shared/ObjectMatrix.test.ts@@ -100,7 +100,7 @@ test('Test ObjectMatrix insertRow', () => {});test('Test ObjectMatrix forEach', () => {- const matrix = new ObjectMatrix<ObjectArray<number>>();+ const matrix = new ObjectMatrix();matrix.insertRow(0, new ObjectArray({ 0: 0, 1: 1 }));matrix.insertRow(1, new ObjectArray({ 0: 0, 1: 1 }));matrix.insertRow(2, new ObjectArray({ 0: 0, 1: 1 }));@@ -109,7 +109,7 @@ test('Test ObjectMatrix forEach', () => {test('Test ObjectMatrix BigData', () => {console.time();- const matrix = new ObjectMatrix<ObjectArray<number>>();+ const matrix = new ObjectMatrix();for (let i = 0; i < 100000; i++) {matrix.insertRow(i, new ObjectArray());}