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(formula): select and slice for array (#757)

Univeraaa478a0ece3
diff --git a/packages/engine-formula/src/engine/utils/compare.ts b/packages/engine-formula/src/engine/utils/compare.ts
index b5e39ee870..2a01e18e74 100644
--- a/packages/engine-formula/src/engine/utils/compare.ts
+++ b/packages/engine-formula/src/engine/utils/compare.ts
@@ -15,5 +15,5 @@
*/
export function getCompare() {
- return new Intl.Collator('zh', { numeric: true }).compare;
+ return new Intl.Collator(undefined, { numeric: true }).compare;
}
diff --git a/packages/engine-formula/src/engine/value-object/__tests__/array-value-object.spec.ts b/packages/engine-formula/src/engine/value-object/__tests__/array-value-object.spec.ts
new file mode 100644
index 0000000000..acdc48cc14
--- /dev/null
+++ b/packages/engine-formula/src/engine/value-object/__tests__/array-value-object.spec.ts
@@ -0,0 +1,127 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* eslint-disable no-magic-numbers */
+import { describe, expect, it } from 'vitest';
+
+import { ArrayValueObject, transformToValueObject } from '../array-value-object';
+
+describe('arrayValueObject test', () => {
+ const originArrayValueObject = new ArrayValueObject({
+ calculateValueList: transformToValueObject([
+ [1, 2, 3, 4, 5],
+ [6, 7, 8, 9, 10],
+ [11, 12, 13, 14, 15],
+ ]),
+ rowCount: 3,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+
+ describe('slice', () => {
+ it('row==null;column=2,,', () => {
+ expect(originArrayValueObject.slice(null, [2]).toValue()).toStrictEqual([
+ [3, 4, 5],
+ [8, 9, 10],
+ [13, 14, 15],
+ ]);
+ });
+
+ it('row==2,,;column=2,,', () => {
+ expect(originArrayValueObject.slice([2], [2]).toValue()).toStrictEqual([[13, 14, 15]]);
+ });
+
+ it('row==,,2;column=2,,', () => {
+ expect(originArrayValueObject.slice([undefined, undefined, 2], [2]).toValue()).toStrictEqual([
+ [3, 4, 5],
+ [13, 14, 15],
+ ]);
+ });
+
+ it('row==1,,;column=null', () => {
+ expect(originArrayValueObject.slice([1]).toValue()).toStrictEqual([
+ [6, 7, 8, 9, 10],
+ [11, 12, 13, 14, 15],
+ ]);
+ });
+
+ it('row==0,1,;column=,,2', () => {
+ expect(originArrayValueObject.slice([0, 1], [undefined, undefined, 2]).toValue()).toStrictEqual([
+ [1, 3, 5],
+ [6, 8, 10],
+ ]);
+ });
+ });
+
+ describe('pick', () => {
+ it('normal', () => {
+ const pickArrayValueObject = new ArrayValueObject({
+ calculateValueList: transformToValueObject([
+ [true, false, false, true, false],
+ [true, false, true, false, false],
+ [true, false, true, false, false],
+ ]),
+ rowCount: 3,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+
+ expect(originArrayValueObject.pick(pickArrayValueObject).toValue()).toStrictEqual([[1, 4, 6, 8, 11, 13]]);
+ });
+
+ it('not boolean', () => {
+ const pickArrayValueObject = new ArrayValueObject({
+ calculateValueList: transformToValueObject([
+ [true, false, false, 1, false],
+ [true, false, 1, false, false],
+ [true, false, 1, false, false],
+ ]),
+ rowCount: 3,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+
+ expect(originArrayValueObject.pick(pickArrayValueObject).toValue()).toStrictEqual([[1, 6, 11]]);
+ });
+
+ it('pick and sum', () => {
+ const pickArrayValueObject = new ArrayValueObject({
+ calculateValueList: transformToValueObject([
+ [true, false, false, true, false],
+ [true, false, true, false, false],
+ [true, false, true, false, false],
+ ]),
+ rowCount: 3,
+ columnCount: 5,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+
+ expect(originArrayValueObject.pick(pickArrayValueObject).sum().getValue()).toStrictEqual(43);
+ });
+ });
+});
diff --git a/packages/engine-formula/src/engine/value-object/array-value-object.ts b/packages/engine-formula/src/engine/value-object/array-value-object.ts
index 5297b222ae..17d106c65d 100644
--- a/packages/engine-formula/src/engine/value-object/array-value-object.ts
+++ b/packages/engine-formula/src/engine/value-object/array-value-object.ts
@@ -54,6 +54,50 @@ export function fromObjectToString(array: IArrayValueObject) {
return '';
}
+export function transformToValueObject(array: Array<Array<number | string | boolean>> = []) {
+ const arrayValueList: CalculateValueType[][] = [];
+
+ for (let r = 0; r < array.length; r++) {
+ const row = array[r];
+
+ if (arrayValueList[r] == null) {
+ arrayValueList[r] = [];
+ }
+
+ for (let c = 0; c < row.length; c++) {
+ const cell = row[c];
+
+ arrayValueList[r][c] = ValueObjectFactory.create(cell);
+ }
+ }
+
+ return arrayValueList;
+}
+
+export function transformToValue(array: CalculateValueType[][] = []) {
+ const arrayValueList: Array<Array<string | number | boolean>> = [];
+
+ for (let r = 0; r < array.length; r++) {
+ const row = array[r];
+
+ if (arrayValueList[r] == null) {
+ arrayValueList[r] = [];
+ }
+
+ for (let c = 0; c < row.length; c++) {
+ const cell = row[c];
+
+ if (cell.isErrorObject()) {
+ arrayValueList[r][c] = (cell as ErrorValueObject).getErrorType();
+ } else {
+ arrayValueList[r][c] = (cell as BaseValueObject).getValue();
+ }
+ }
+ }
+
+ return arrayValueList;
+}
+
export class ArrayValueObject extends BaseValueObject {
private _value: CalculateValueType[][] = [];
@@ -147,6 +191,17 @@ export class ArrayValueObject extends BaseValueObject {
return true;
}
+ get(row: number, column: number) {
+ return this._value[row][column];
+ }
+
+ set(row: number, column: number, value: CalculateValueType) {
+ if (row >= this._rowCount || column >= this._columnCount) {
+ throw new Error('Exceeding array bounds.');
+ }
+ this._value[row][column] = value;
+ }
+
getRangePosition() {
const startRow = 0;
const rowCount = this.getRowCount();
@@ -189,10 +244,105 @@ export class ArrayValueObject extends BaseValueObject {
/**
* Referring to matrix calculations,
- * extract the matching values from a true/false matrix based on parameters and store them in a one-dimensional array.
+ * extract the matching values from a true/false matrix based on parameters and store them in a two-dimensional array.
+ * implement x[x<10]
+ * https://numpy.org/doc/stable/user/basics.indexing.html
* @param takeArray
*/
- take(takeArray: ArrayValueObject) {}
+ pick(takeArray: ArrayValueObject) {
+ const takeArrayRowCount = takeArray.getRowCount();
+ const takeArrayColumnCount = takeArray.getColumnCount();
+
+ if (takeArrayRowCount !== this._rowCount || takeArrayRowCount !== this._rowCount) {
+ return this._createNewArray([[new NumberValueObject(0)]], 1, 1);
+ }
+
+ const newValue: CalculateValueType[][] = [];
+
+ newValue[0] = [];
+
+ for (let r = 0; r < takeArrayRowCount; r++) {
+ for (let c = 0; c < takeArrayColumnCount; c++) {
+ const takeCell = takeArray.get(r, c);
+ const value = this.get(r, c);
+ if (takeCell.isErrorObject()) {
+ continue;
+ }
+
+ if ((takeCell as BaseValueObject).getValue() === true) {
+ newValue[0].push(value);
+ }
+ }
+ }
+
+ return this._createNewArray(newValue, 1, newValue[0].length);
+ }
+
+ /**
+ * Flatten a 2D array.
+ * https://numpy.org/doc/stable/reference/generated/numpy.chararray.flatten.html#numpy.chararray.flatten
+ */
+ flatten() {
+ const newValue: CalculateValueType[][] = [];
+ newValue[0] = [];
+ for (let r = 0; r < this._rowCount; r++) {
+ for (let c = 0; c < this._rowCount; c++) {
+ const value = this.get(r, c);
+
+ newValue[0].push(value);
+ }
+ }
+ return this._createNewArray(newValue, 1, newValue[0].length);
+ }
+
+ /**
+ * I'm looking to perform slicing operations on 2D arrays, similar to the functionality provided by NumPy.
+ * https://numpy.org/doc/stable/user/basics.indexing.html
+ * @rowParam start:stop:step
+ * @columnParam start:stop:step
+ * @param takeArray
+ */
+ slice(rowParam: Nullable<Array<Nullable<number>>>, columnParam: Nullable<Array<Nullable<number>>>) {
+ let rowStart = 0;
+ let rowStop = this._rowCount - 1;
+ let rowStep = 1;
+
+ let columnStart = 0;
+ let columnStop = this._columnCount - 1;
+ let columnStep = 1;
+
+ if (rowParam != null) {
+ rowStart = rowParam[0] || 0;
+ rowStop = rowParam[1] || this._rowCount - 1;
+ rowStep = rowParam[2] || 1;
+ }
+
+ if (columnParam != null) {
+ columnStart = columnParam[0] || 0;
+ columnStop = columnParam[1] || this._columnCount - 1;
+ columnStep = columnParam[2] || 1;
+ }
+
+ const result: CalculateValueType[][] = [];
+
+ const array = this._value;
+
+ let result_row_index = 0;
+ let result_column_index = 0;
+ for (let r = rowStart; r <= rowStop; r += rowStep) {
+ result_column_index = 0;
+ if (result[result_row_index] == null) {
+ result[result_row_index] = [];
+ }
+ for (let c = columnStart; c <= columnStop; c += columnStep) {
+ result[result_row_index][result_column_index] = array[r][c];
+ result_column_index++;
+ }
+ result_row_index++;
+ }
+
+ return this._createNewArray(result, result.length, result[0].length);
+ }
sum() {
let accumulatorAll: CalculateValueType = new NumberValueObject(0);
@@ -414,6 +564,10 @@ export class ArrayValueObject extends BaseValueObject {
return this._createNewArray(result, rowCount, columnCount);
}
+ toValue() {
+ return transformToValue(this._value);
+ }
+
private _sort(index: number) {
const compare = getCompare();
return (a: CalculateValueType[], b: CalculateValueType[]) => {