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
fix(sheets): expand data region to full contiguous range (#7893)
wpxp1234566783093401f7
diff --git a/packages/sheets/src/facade/__tests__/f-range.spec.ts b/packages/sheets/src/facade/__tests__/f-range.spec.tsindex 84cd668111..00f5d9bfd9 100644--- a/packages/sheets/src/facade/__tests__/f-range.spec.ts+++ b/packages/sheets/src/facade/__tests__/f-range.spec.ts@@ -14,8 +14,17 @@* limitations under the License.*/-import type { ICellData, IDocumentData, Injector, IStyleData, Nullable, RichTextValue, Workbook } from '@univerjs/core';+import type {+ ICellData,+ IDocumentData,+ Injector,+ IStyleData,+ Nullable,+ RichTextValue,+ Workbook,+} from '@univerjs/core';import type { FUniver } from '@univerjs/core/facade';+import type { FWorksheet } from '../f-worksheet';import {AbsoluteRefType,Dimension,@@ -907,6 +916,96 @@ describe('Test FRange', () => {]));});+ describe('getDataRegion', () => {+ let worksheet: FWorksheet;++ beforeEach(() => {+ worksheet = univerAPI.createWorkbook({+ id: 'data-region',+ sheetOrder: ['data'],+ sheets: {+ data: { id: 'data', name: 'data', rowCount: 8, columnCount: 8 },+ },+ }).getActiveSheet();+ });++ it.each([+ { dataRange: 'A1:A4', cells: ['A1', 'A2', 'A3', 'A4'] },+ { dataRange: 'A1:D1', cells: ['A1', 'B1', 'C1', 'D1'] },+ ])('expands every cell in $dataRange to the whole list', ({ dataRange, cells }) => {+ worksheet.getRange(dataRange).setValue(1);++ for (const cell of cells) {+ const range = worksheet.getRange(cell);+ expect(range.getDataRegion().getA1Notation()).toBe(dataRange);+ expect(range.getA1Notation()).toBe(cell);+ }+ });++ it('expands rectangles while respecting the requested dimension', () => {+ worksheet.getRange('A1:F6').setValue(1);++ expect(worksheet.getRange('C3').getDataRegion().getA1Notation()).toBe('A1:F6');+ expect(worksheet.getRange('C3:D4').getDataRegion().getA1Notation()).toBe('A1:F6');+ expect(worksheet.getRange('C3:D4').getDataRegion(Dimension.ROWS).getA1Notation()).toBe('C1:D6');+ expect(worksheet.getRange('C3:D4').getDataRegion(Dimension.COLUMNS).getA1Notation()).toBe('A3:F4');+ });++ it('rechecks boundaries exposed by expansion in another direction', () => {+ worksheet.getRange('B4:D4').setValue(1);+ worksheet.getRange('D2:D3').setValue(1);+ worksheet.getRange('A2:C2').setValue(1);+ worksheet.getRange('A1').setValue(1);++ expect(worksheet.getRange('B4').getDataRegion().getA1Notation()).toBe('A1:D4');+ });++ it('counts zero, false, formulas and rich text as data and stops at empty boundaries', () => {+ worksheet.getRange('A1:A4').setValues([+ [{ v: 0 }],+ [{ v: false }],+ [{ f: '=1' }],+ [{ p: { id: 'rich-text', documentStyle: {}, body: { dataStream: 'text\r\n' } } }],+ ]);+ worksheet.getRange('A5:B5').setValues([[{ v: '' }, {}]]);+ worksheet.getRange('A5:B5').setFontWeight('bold');+ worksheet.getRange('A6:B6').setValue('separate');+ worksheet.getRange('C1:C4').setValue('separate');++ expect(worksheet.getRange('A1').getDataRegion().getA1Notation()).toBe('A1:A4');+ expect(worksheet.getRange('A4').getDataRegion().getA1Notation()).toBe('A1:A4');+ });++ it('expands an empty starting cell when adjacent cells contain data', () => {+ worksheet.getRange('C2').setValue(1);+ worksheet.getRange('B3').setValue(1);+ worksheet.getRange('D3').setValue(1);+ worksheet.getRange('C4').setValue(1);++ expect(worksheet.getRange('C3').getDataRegion().getA1Notation()).toBe('B2:D4');+ expect(worksheet.getRange('C3').getDataRegion(Dimension.ROWS).getA1Notation()).toBe('C2:C4');+ expect(worksheet.getRange('C3').getDataRegion(Dimension.COLUMNS).getA1Notation()).toBe('B3:D3');+ });++ it('keeps isolated ranges and ignores diagonal-only neighbors', () => {+ worksheet.getRange('A1').setValue(1);+ worksheet.getRange('C3').setValue(1);+ worksheet.getRange('F6').setValue(1);++ expect(worksheet.getRange('A1').getDataRegion().getA1Notation()).toBe('A1');+ expect(worksheet.getRange('B2').getDataRegion().getA1Notation()).toBe('B2');+ expect(worksheet.getRange('D4:E5').getDataRegion().getA1Notation()).toBe('D4:E5');+ });++ it('stays within worksheet bounds when data reaches every edge', () => {+ worksheet.getRange('A1:H8').setValue(1);++ expect(worksheet.getRange('D4').getDataRegion().getA1Notation()).toBe('A1:H8');+ expect(worksheet.getRange('A1').getDataRegion(Dimension.ROWS).getA1Notation()).toBe('A1:A8');+ expect(worksheet.getRange('H8').getDataRegion(Dimension.COLUMNS).getA1Notation()).toBe('A8:H8');+ });+ });+it('Range supports an analyst editing a formatted data island', () => {const workbook = univerAPI.getActiveWorkbook()!;const activeSheet = workbook.getActiveSheet();@@ -982,9 +1081,9 @@ describe('Test FRange', () => {activeSheet.getRange('A3').setValue('Neighbor');activeSheet.getRange('E3').setValue('Calculated');activeSheet.getRange('C5').setValue('Footer');- expect(activeSheet.getRange('C3').getDataRegion().getA1Notation()).toBe('B2:D4');- expect(activeSheet.getRange('C3').getDataRegion(Dimension.ROWS).getA1Notation()).toBe('C2:C4');- expect(activeSheet.getRange('C3').getDataRegion(Dimension.COLUMNS).getA1Notation()).toBe('B3:D3');+ expect(activeSheet.getRange('C3').getDataRegion().getA1Notation()).toBe('A1:E5');+ expect(activeSheet.getRange('C3').getDataRegion(Dimension.ROWS).getA1Notation()).toBe('C2:C5');+ expect(activeSheet.getRange('C3').getDataRegion(Dimension.COLUMNS).getA1Notation()).toBe('A3:E3');const offset = report.offset(1, 1, 2);expect(offset.getA1Notation()).toBe('C3:E4');diff --git a/packages/sheets/src/facade/f-range.ts b/packages/sheets/src/facade/f-range.tsindex f1d9693e3f..b87670a281 100644--- a/packages/sheets/src/facade/f-range.ts+++ b/packages/sheets/src/facade/f-range.ts@@ -2635,6 +2635,7 @@ export class FRange extends FBaseInitialable {* Returns a copy of the range expanded `Direction.UP` and `Direction.DOWN` if the specified dimension is `Dimension.ROWS`, or `Direction.NEXT` and `Direction.PREVIOUS` if the dimension is `Dimension.COLUMNS`.* The expansion of the range is based on detecting data next to the range that is organized like a table.* The expanded range covers all adjacent cells with data in them along the specified dimension including the table boundaries.+ * Cells connected only diagonally do not cause the range to expand.* If the original range is surrounded by empty cells along the specified dimension, the range itself is returned.* @param {Dimension} [dimension] - The dimension along which to expand the range. If not provided, the range will be expanded in both dimensions.* @returns {FRange} The range's data region or a range covering each column or each row spanned by the original range.@@ -2655,7 +2656,7 @@ export class FRange extends FBaseInitialable {* fWorksheet.getRange('D3').setValue(100);* fWorksheet.getRange('C4').setValue(100);*- * // Get C3 data region along the rows dimension, the range will be C2:D4+ * // Get C3 data region along the rows dimension, the range will be C2:C4* const range = fWorksheet.getRange('C3').getDataRegion(univerAPI.Enum.Dimension.ROWS);* console.log(range.getA1Notation()); // C2:C4*@@ -2669,78 +2670,57 @@ export class FRange extends FBaseInitialable {* ```*/getDataRegion(dimension?: Dimension): FRange {- const { startRow, startColumn, endRow, endColumn } = this._range;+ let { startRow, startColumn, endRow, endColumn } = this._range;const maxRows = this._worksheet.getMaxRows();const maxColumns = this._worksheet.getMaxColumns();const cellMatrix = this._worksheet.getCellMatrix();-- // If the original range is surrounded by empty cells along the specified dimension, the range itself is returned.- let newStartRow = startRow;- let newStartColumn = startColumn;- let newEndRow = endRow;- let newEndColumn = endColumn;-- // Four directions or dimension rows- if (dimension !== Dimension.COLUMNS) {- let topRowHasValue = false;- let bottomRowHasValue = false;-+ const rowHasData = (row: number): boolean => {for (let c = startColumn; c <= endColumn; c++) {- if (startRow > 0 && !isNullCell(cellMatrix.getValue(startRow - 1, c))) {- topRowHasValue = true;+ if (!isNullCell(cellMatrix.getValue(row, c))) {+ return true;}-- if (endRow < maxRows - 1 && !isNullCell(cellMatrix.getValue(endRow + 1, c))) {- bottomRowHasValue = true;- }-- if (topRowHasValue && bottomRowHasValue) {- break;+ }+ return false;+ };+ const columnHasData = (column: number): boolean => {+ for (let r = startRow; r <= endRow; r++) {+ if (!isNullCell(cellMatrix.getValue(r, column))) {+ return true;}}+ return false;+ };- if (topRowHasValue) {- newStartRow = startRow - 1;- }+ let expanded = true;+ while (expanded) {+ expanded = false;- if (bottomRowHasValue) {- newEndRow = endRow + 1;+ while (dimension !== Dimension.COLUMNS && startRow > 0 && rowHasData(startRow - 1)) {+ startRow--;+ expanded = true;}- }-- // Four directions or dimension columns- if (dimension !== Dimension.ROWS) {- let leftColumnHasValue = false;- let rightColumnHasValue = false;-- for (let r = startRow; r <= endRow; r++) {- if (startColumn > 0 && !isNullCell(cellMatrix.getValue(r, startColumn - 1))) {- leftColumnHasValue = true;- }-- if (endColumn < maxColumns - 1 && !isNullCell(cellMatrix.getValue(r, endColumn + 1))) {- rightColumnHasValue = true;- }- if (leftColumnHasValue && rightColumnHasValue) {- break;- }+ while (dimension !== Dimension.COLUMNS && endRow < maxRows - 1 && rowHasData(endRow + 1)) {+ endRow++;+ expanded = true;}- if (leftColumnHasValue) {- newStartColumn = startColumn - 1;+ while (dimension !== Dimension.ROWS && startColumn > 0 && columnHasData(startColumn - 1)) {+ startColumn--;+ expanded = true;}- if (rightColumnHasValue) {- newEndColumn = endColumn + 1;+ while (dimension !== Dimension.ROWS && endColumn < maxColumns - 1 && columnHasData(endColumn + 1)) {+ endColumn++;+ expanded = true;}}return this._injector.createInstance(FRange, this._workbook, this._worksheet, {- startRow: newStartRow,- startColumn: newStartColumn,- endRow: newEndRow,- endColumn: newEndColumn,+ startRow,+ startColumn,+ endRow,+ endColumn,});}