Freeze

GitHubEdit on GitHub

Freezing uses the top-left corner of the current selection as the freeze point. Therefore, if the current selection is cell A1, freezing will not be effective.

IWorksheetData.freeze can be used to configure the freeze state of a worksheet. Its interface definition is as follows:

interface IFreeze {
  /**
   * The number of frozen columns
   */
  xSplit: number
  /**
   * The number of frozen rows
   */
  ySplit: number
  /**
   * The starting row that can be scrolled (the starting row of the main view area)
   */
  startRow: number
  /**
   * The starting column that can be scrolled (the starting column of the main view area)
   */
  startColumn: number
}

For example, if I want to freeze column B and row 2, I can configure it like this:

{
  "xSplit": 1, // Freeze 1 column, which is column B
  "ySplit": 1, // Freeze 1 row, which is row 2
  "startRow": 2, // The starting row that can be scrolled is row 3
  "startColumn": 2 // The starting column that can be scrolled is column C
}

If you want to implement custom freeze functionality, such as freezing the first row and first column, you can refer to:

Facade API

The FWorksheet class provides several methods to manage the freeze state of rows and columns in a worksheet. These methods allow you to set, get, and cancel frozen panes.

Set Freeze

const worksheet = univerAPI.getActiveWorkbook().getActiveSheet()
worksheet.setFreeze({
  xSplit: 1, // Freeze 1 column, which is column B
  ySplit: 1, // Freeze 1 row, which is row 2
  startRow: 2, // The starting row that can be scrolled is row 3
  startColumn: 2, // The starting column that can be scrolled is column C
})
// console.log('当前冻结状态:', worksheet.getFreeze())
console.log('The current freeze state:', worksheet.getFreeze())

Set Frozen Columns

const worksheet = univerAPI.getActiveWorkbook().getActiveSheet()

// Set freeze for columns A-B
worksheet.setFrozenColumns(2)

Set Frozen Rows

const worksheet = univerAPI.getActiveWorkbook().getActiveSheet()

// Set freeze for the first 3 rows
worksheet.setFrozenRows(3)

Get Freeze State

const worksheet = univerAPI.getActiveWorkbook().getActiveSheet()

const freezeState = worksheet.getFreeze()

Get Frozen Columns

const worksheet = univerAPI.getActiveWorkbook().getActiveSheet()

const frozenColumns = worksheet.getFrozenColumns()

Get Frozen Rows

const worksheet = univerAPI.getActiveWorkbook().getActiveSheet()

const frozenRows = worksheet.getFrozenRows()

Cancel Freeze

const worksheet = univerAPI.getActiveWorkbook().getActiveSheet()

// Cancel all freezes
worksheet.cancelFreeze()

How is this guide?