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(facade): add FRange.getValues api (#3344)

#FF00001021e300b6b7
diff --git a/packages/facade/src/apis/sheets/__tests__/f-range.spec.ts b/packages/facade/src/apis/sheets/__tests__/f-range.spec.ts
index a696e033e2..dd17783668 100644
--- a/packages/facade/src/apis/sheets/__tests__/f-range.spec.ts
+++ b/packages/facade/src/apis/sheets/__tests__/f-range.spec.ts
@@ -18,7 +18,7 @@
import type { ICellData, Injector, IRange, IStyleData, Nullable } from '@univerjs/core';
import { DataValidationType, HorizontalAlign, ICommandService, IUniverInstanceService, VerticalAlign, WrapStrategy } from '@univerjs/core';
-import { SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand } from '@univerjs/sheets';
+import { AddWorksheetMergeCommand, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand } from '@univerjs/sheets';
import { beforeEach, describe, expect, it } from 'vitest';
import { FormulaDataModel } from '@univerjs/engine-formula';
@@ -58,6 +58,7 @@ describe('Test FRange', () => {
commandService.registerCommand(SetHorizontalTextAlignCommand);
commandService.registerCommand(SetTextWrapCommand);
commandService.registerCommand(AddSheetDataValidationCommand);
+ commandService.registerCommand(AddWorksheetMergeCommand);
getValueByPosition = (
startRow: number,
@@ -210,6 +211,60 @@ describe('Test FRange', () => {
expect(getStyleByPosition(3, 4, 3, 4)?.bg?.rgb).toBe('red');
});
+ it('Range getValues', () => {
+ const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();
+
+ // Set different types of values
+ activeSheet?.getRange(0, 0, 4, 4)?.setValues([
+ [1, 'text', true, ''],
+ [2.5, '', false, ''],
+ ['', '=SUM(A1:A2)', '', 0],
+ ['', '', '', ''],
+ ]);
+
+ // Test getting values for the entire range
+ const range1 = activeSheet?.getRange(0, 0, 4, 4);
+ const values1 = range1?.getValues();
+ expect(values1).toEqual([
+ [1, 'text', 1, ''],
+ [2.5, '', 0, ''],
+ ['', null, '', 0],
+ ['', '', '', ''],
+ ]);
+
+ // Test getting values for a partial range
+ const range2 = activeSheet?.getRange(1, 1, 2, 2);
+ const values2 = range2?.getValues();
+ expect(values2).toEqual([
+ ['', 0],
+ [null, ''],
+ ]);
+
+ // Test getting value for a single cell
+ const range3 = activeSheet?.getRange(0, 0, 1, 1);
+ const values3 = range3?.getValues();
+ expect(values3).toEqual([[1]]);
+
+ // Test getting values for an empty area
+ const range4 = activeSheet?.getRange(5, 5, 2, 2);
+ const values4 = range4?.getValues();
+ expect(values4).toEqual([
+ [null, null],
+ [null, null],
+ ]);
+
+ // Test getting values for a range containing merged cells
+ activeSheet?.getRange(6, 0, 2, 2)?.merge();
+ activeSheet?.getRange(6, 0)?.setValue('Merged');
+ const range5 = activeSheet?.getRange(6, 0, 3, 3);
+ const values5 = range5?.getValues();
+ expect(values5).toEqual([
+ ['Merged', null, null],
+ [null, null, null],
+ [null, null, null],
+ ]);
+ });
+
it('Range getCellData', () => {
const activeSheet = univerAPI.getActiveWorkbook()!.getActiveSheet();
activeSheet?.getRange(0, 0)?.setValue(1);
diff --git a/packages/facade/src/apis/sheets/f-range.ts b/packages/facade/src/apis/sheets/f-range.ts
index 1cd9016c79..f4447912db 100644
--- a/packages/facade/src/apis/sheets/f-range.ts
+++ b/packages/facade/src/apis/sheets/f-range.ts
@@ -214,10 +214,35 @@ export class FRange {
return null;
}
+ /**
+ * Returns the value of the cell at the start of this range.
+ * @returns The value of the cell.
+ */
getValue(): CellValue | null {
return this._worksheet.getCell(this._range.startRow, this._range.startColumn)?.v ?? null;
}
+ /**
+ * Returns the rectangular grid of values for this range.
+ * Returns a two-dimensional array of values, indexed by row, then by column.
+ * @returns A two-dimensional array of values.
+ */
+ getValues(): Nullable<CellValue>[][] {
+ const { startRow, endRow, startColumn, endColumn } = this._range;
+ const range: Array<Array<Nullable<CellValue>>> = [];
+
+ for (let r = startRow; r <= endRow; r++) {
+ const row: Array<Nullable<CellValue>> = [];
+
+ for (let c = startColumn; c <= endColumn; c++) {
+ row.push(this._worksheet.getCell(r, c)?.v ?? null);
+ }
+
+ range.push(row);
+ }
+ return range;
+ }
+
/**
* Returns the formulas (A1 notation) for the cells in the range. Entries in the 2D array are empty strings for cells with no formula.
* @returns A two-dimensional array of formulas in string format.