Skip to content

Commit

Permalink
feat: better API for freeze try to be consistent with the Google Apps…
Browse files Browse the repository at this point in the history
… Script
  • Loading branch information
lumixraku committed Dec 16, 2024
1 parent 4d7eebb commit 470148c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 73 deletions.
6 changes: 4 additions & 2 deletions packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,9 @@ describe('Test FWorksheet', () => {
expect(freeze).toEqual({ startRow: -1, startColumn: 2, xSplit: 2, ySplit: 0 });

activeSheet?.cancelFreeze();
activeSheet?.freezeColumn(2, 3);
activeSheet?.setFrozenColumns(2, 3);
expect(activeSheet?.getFreeze()).toEqual({ startRow: -1, startColumn: 4, xSplit: 2, ySplit: 0 });
expect(activeSheet?.getFrozenColumnRange()).toEqual({ startColumn: 2, endColumn: 3 });
});

it('Worksheet setFrozenRows and getFrozenRows', () => {
Expand All @@ -532,8 +533,9 @@ describe('Test FWorksheet', () => {
expect(freeze).toEqual({ startRow: 3, startColumn: -1, xSplit: 0, ySplit: 3 });

activeSheet?.cancelFreeze();
activeSheet?.freezeRow(2, 3);
activeSheet?.setFrozenRows(2, 3);
expect(activeSheet?.getFreeze()).toEqual({ startRow: 4, startColumn: -1, xSplit: 0, ySplit: 2 });
expect(activeSheet?.getFrozenRowRange()).toEqual({ startRow: 2, endRow: 3 });
});

it('Worksheet combined frozen rows and columns', () => {
Expand Down
157 changes: 86 additions & 71 deletions packages/sheets/src/facade/f-worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,10 +1021,11 @@ export class FWorksheet extends FBase {
* @param freeze - the scrolling viewport start range and count of freezed rows and columns.
* that means if you want to freeze the first 3 rows and 2 columns, you should set freeze as { startRow: 3, startColumn: 2, xSplit: 2, ySplit: 3 }
*
* @deprecated use `freezeColumn` and `freezeRow` instead.
* @deprecated use `setFrozenRows` and `setFrozenColumns` instead.
* @returns True if the command was successful, false otherwise.
*/
setFreeze(freeze: IFreeze): boolean {
console.warn('setFreeze is deprecated, use setFrozenRows and setFrozenColumns instead');
return this._commandService.syncExecuteCommand(SetFrozenCommand.id, {
...freeze,
unitId: this._workbook.getUnitId(),
Expand Down Expand Up @@ -1059,27 +1060,95 @@ export class FWorksheet extends FBase {
* @param columns The number of columns to freeze.
* To unfreeze all columns, set this value to 0.
*/
setFrozenColumns(columns: number): void {
const currentFreeze = this.getFreeze();
this.setFreeze({
...currentFreeze,
startColumn: columns > 0 ? columns : -1,
xSplit: columns,
});
setFrozenColumns(columns: number): void;
/**
* Set freeze column, then the range from startColumn to endColumn will be fixed.
* e.g. setFrozenColumns(0, 2) will fix the column range from 0 to 2.
* e.g. setFrozenColumns(2, 3) will fix the column range from 2 to 3, And column from 0 to 1 will be invisible.
*
* @example
* ``` ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorkSheet = fWorkbook.getActiveSheet();
* // freeze the first too columns.
* fWorkSheet.setFrozenColumns(0, 2);
* ```
* @param startColumn
* @param endColumn
*/
setFrozenColumns(startColumn: number, endColumn: number): void;
setFrozenColumns(...args: [number] | [number, number]): void {
const freezeCfg = this.getFreeze();
if (arguments.length === 1) {
const columns = args[0];
this.setFreeze({
...freezeCfg,
startColumn: columns > 0 ? columns : -1,
xSplit: columns,
});
} else if (arguments.length === 2) {
let [startColumn = 0, endColumn = 0] = args;
if(startColumn > endColumn) {

Check failure on line 1091 in packages/sheets/src/facade/f-worksheet.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected space(s) after "if"
[startColumn, endColumn] = [endColumn, startColumn];
}
this._commandService.syncExecuteCommand(SetFrozenCommand.id, {
startColumn: endColumn + 1,
xSplit: endColumn - startColumn + 1,
startRow: freezeCfg.startRow,
ySplit: freezeCfg.ySplit,
unitId: this._workbook.getUnitId(),
subUnitId: this.getSheetId(),
});
}
}

/**
* Set the number of frozen rows.
* @param rows The number of rows to freeze.
* To unfreeze all rows, set this value to 0.
*/
setFrozenRows(rows: number): void {
const currentFreeze = this.getFreeze();
this.setFreeze({
...currentFreeze,
startRow: rows > 0 ? rows : -1,
ySplit: rows,
});
setFrozenRows(rows: number): void;

/**
* Set freeze row, then the range from startRow to endRow will be fixed.
* e.g. setFrozenRows(0, 2) will fix the row range from 0 to 2.
* e.g. setFrozenRows(2, 3) will fix the row range from 2 to 3, And row from 0 to 1 will be invisible.
* @param startRow
* @param endRow
*
* @example
* ``` ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorkSheet = fWorkbook.getActiveSheet();
* // freeze the first too rows.
* fWorkSheet.setFrozenRows(0, 2);
* ```
*/
setFrozenRows(startColumn: number, endColumn: number): void;
setFrozenRows(...args: [number] | [number, number]): void {
const freezeCfg = this.getFreeze();
if (arguments.length === 1) {
const rows = args[0];
this.setFreeze({
...freezeCfg,
startRow: rows > 0 ? rows : -1,
ySplit: rows,
});
} else if (arguments.length === 2) {
let [startRow = 0, endRow = 0] = args;
if(startRow > endRow) {

Check failure on line 1139 in packages/sheets/src/facade/f-worksheet.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected space(s) after "if"
[startRow, endRow] = [endRow, startRow];
}
this._commandService.syncExecuteCommand

Check failure on line 1142 in packages/sheets/src/facade/f-worksheet.ts

View workflow job for this annotation

GitHub Actions / eslint

Missing semicolon
this._commandService.syncExecuteCommand(SetFrozenCommand.id, {
startRow: endRow + 1,
ySplit: endRow - startRow + 1,
startColumn: freezeCfg.startColumn,
xSplit: freezeCfg.xSplit,
unitId: this._workbook.getUnitId(),
subUnitId: this.getSheetId(),
});
}
}

/**
Expand Down Expand Up @@ -1108,64 +1177,10 @@ export class FWorksheet extends FBase {
return freeze.startRow;
}

/**
* Set freeze column, then the range from startColumn to endColumn will be fixed.
* e.g. freezeColumn(0, 2) will fix the column range from 0 to 2.
* e.g. freezeColumn(2, 3) will fix the column range from 2 to 3, And column from 0 to 1 will be invisible.
*
* @example
* ``` ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorkSheet = fWorkbook.getActiveSheet();
* // freeze the first too columns.
* fWorkSheet.freezeColumn(0, 2);
* ```
* @param startColumn
* @param endColumn
*/
freezeColumn(startColumn: number, endColumn: number): void {
const freezeCfg = this.getFreeze();
this._commandService.syncExecuteCommand(SetFrozenCommand.id, {
startColumn: endColumn + 1,
xSplit: endColumn - startColumn + 1,
startRow: freezeCfg.startRow,
ySplit: freezeCfg.ySplit,
unitId: this._workbook.getUnitId(),
subUnitId: this.getSheetId(),
});
}

/**
* Set freeze row, then the range from startRow to endRow will be fixed.
* e.g. freezeRow(0, 2) will fix the row range from 0 to 2.
* e.g. freezeRow(2, 3) will fix the row range from 2 to 3, And row from 0 to 1 will be invisible.
* @param startRow
* @param endRow
*
* @example
* ``` ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorkSheet = fWorkbook.getActiveSheet();
* // freeze the first too rows.
* fWorkSheet.freezeRow(0, 2);
* ```
*/
freezeRow(startRow: number, endRow: number): void {
const freezeCfg = this.getFreeze();
this._commandService.syncExecuteCommand(SetFrozenCommand.id, {
startRow: endRow + 1,
ySplit: endRow - startRow + 1,
startColumn: freezeCfg.startColumn,
xSplit: freezeCfg.xSplit,
unitId: this._workbook.getUnitId(),
subUnitId: this.getSheetId(),
});
}

/**
* Get freezed rows.
*/
getRowFreezeStatus(): { startRow: number; endRow: number } {
getFrozenRowRange(): { startRow: number; endRow: number } {
const cfg = this._worksheet.getFreeze();
return {
startRow: cfg.startRow - cfg.ySplit,
Expand All @@ -1176,7 +1191,7 @@ export class FWorksheet extends FBase {
/**
* Get freezed columns
*/
getColumnFreezeStatus(): { startColumn: number; endColumn: number } {
getFrozenColumnRange(): { startColumn: number; endColumn: number } {
const cfg = this._worksheet.getFreeze();
return {
startColumn: cfg.startColumn - cfg.xSplit,
Expand Down

0 comments on commit 470148c

Please sign in to comment.