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: add getSlice method (#717)

Wenzhao Hu4c1d087515f3
diff --git a/packages/core/src/shared/object-matrix.ts b/packages/core/src/shared/object-matrix.ts
index f15f3036c5..49397d5d11 100644
--- a/packages/core/src/shared/object-matrix.ts
+++ b/packages/core/src/shared/object-matrix.ts
@@ -17,6 +17,7 @@
import type { IRange } from '../types/interfaces/i-range';
import type { ObjectArrayPrimitiveType, PredicateFunction } from './object-array';
import { ObjectArray } from './object-array';
+import { Tools } from './tools';
import type { Nullable } from './types';
/**
@@ -259,7 +260,17 @@ export class ObjectMatrix<T> {
});
}
- getFragments(startRow: number, endRow: number, startColumn: number, endColumn: number): ObjectMatrix<T> {
+ /**
+ * Return a fragment of the original data matrix. Note that the returned matrix's row matrix would start from
+ * 0 not `startRow`. Neither does its column matrix. If you want to get the original matrix, use `getSlice`.
+ *
+ * @param startRow
+ * @param endRow
+ * @param startColumn
+ * @param endColumn
+ * @returns
+ */
+ getFragment(startRow: number, endRow: number, startColumn: number, endColumn: number): ObjectMatrix<T> {
const objectMatrix = new ObjectMatrix<T>();
for (let r = startRow; r <= endRow; r++) {
const row = new ObjectArray<T>();
@@ -272,6 +283,30 @@ export class ObjectMatrix<T> {
return objectMatrix;
}
+ /**
+ * Return a slice of the original data matrix. Note that the returned matrix's row matrix would start from
+ * `startRow` not 0, and the same does its column index. You may be looking for `getFragment` if you want
+ * both of the indexes start from 0.
+ *
+ * @param startRow
+ * @param endRow
+ * @param startColumn
+ * @param endColumn
+ * @returns
+ */
+ getSlice(startRow: number, endRow: number, startColumn: number, endColumn: number): ObjectMatrix<T> {
+ const objectMatrix = new ObjectMatrix<T>();
+ for (let r = startRow; r <= endRow; r++) {
+ for (let c = startColumn; c <= endColumn; c++) {
+ const value = this.getValue(r, c);
+ if (value) {
+ objectMatrix.setValue(r, c, Tools.deepClone(value));
+ }
+ }
+ }
+ return objectMatrix;
+ }
+
getSizeOf(): number {
return this._option.getSizeOf();
}
diff --git a/packages/core/src/sheets/range.ts b/packages/core/src/sheets/range.ts
index 17c949be0b..f5dd5cab17 100644
--- a/packages/core/src/sheets/range.ts
+++ b/packages/core/src/sheets/range.ts
@@ -290,7 +290,7 @@ export class Range {
const { startRow, endRow, startColumn, endColumn } = this._range;
// get object values from sheet matrix, or use this.getMatrix() create a new matrix then this.getMatrix().getData()
- const values = this._worksheet.getCellMatrix().getFragments(startRow, endRow, startColumn, endColumn).getData();
+ const values = this._worksheet.getCellMatrix().getFragment(startRow, endRow, startColumn, endColumn).getData();
if (options.isIncludeStyle) {
const style = this._deps.getStyles();