FWorkSheetTableMixin

GitHubEdit on GitHub
packages@univerjs/sheets-table

APIs

addTable

Add a table to the worksheet

Signature

addTable(tableName: string, rangeInfo: ITableRange, tableId?: string, options?: ITableOptions): Promise<boolean> | boolean

Parameters

  • tableName (string) — The table name
  • rangeInfo (ITableRange) — The table range information
  • tableId (string) — The table id
  • options (ITableOptions) — The table options

Returns

  • (Promise<boolean> | boolean) — Whether the table was added successfully

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()

// Insert a table in the range B2:F11
const fRange = fWorksheet.getRange('B2:F11')
const success = await fWorksheet.addTable(
  'name-1',
  fRange.getRange(),
  'id-1',
  {
    tableStyleId: 'table-default-4',
    columns: [
      { id: 'col-1', displayName: 'Column 1' },
    ],
    filters: [
      {
        filterType: univerAPI.Enum.TableColumnFilterTypeEnum.condition,
        filterInfo: {
          conditionType: univerAPI.Enum.TableConditionTypeEnum.Number,
          compareType: univerAPI.Enum.TableNumberCompareTypeEnum.GreaterThan,
          expectedValue: 2,
        },
      },
    ],
  },
)

if (success) {
  const tableInfo = fWorkbook.getTableInfo('id-1')
  console.log('debugger tableInfo', tableInfo)
}

addTableTheme

Add a theme to the table

Signature

addTableTheme(tableId: string, themeStyleJSON: IRangeThemeStyleJSON): Promise<boolean>

Parameters

  • tableId (string) — The table id
  • themeStyleJSON (IRangeThemeStyleJSON) — The theme style JSON

Returns

  • (Promise<boolean>) — Whether the theme was added successfully

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()

// Insert a table in the range B2:F11
const fRange = fWorksheet.getRange('B2:F11')
const success = await fWorksheet.addTable(
  'name-1',
  fRange.getRange(),
  'id-1',
  {
    tableStyleId: 'table-default-4',
  },
)

if (success) {
  await fWorksheet.addTableTheme('id-1', {
    name: 'table-custom-1',
    headerRowStyle: {
      bg: {
        rgb: '#145f82',
      },
    },
    firstRowStyle: {
      bg: {
        rgb: '#c0e4f5',
      },
    },
  })

  const tableInfo = fWorkbook.getTableInfo('id-1')
  console.log('debugger tableInfo', tableInfo)
}

getSubTableInfos

Get the list of tables in the worksheet

Signature

getSubTableInfos(): ITableInfoWithUnitId[]

Returns

  • (ITableInfoWithUnitId[]) — The list of tables

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()
const tables = fWorksheet.getSubTableInfos()
console.log('debugger tables', tables)

getTableByCell

Get the table information by cell position

Signature

getTableByCell(row: number, column: number): ITableInfoWithUnitId | undefined

Parameters

  • row (number) — The cell row index, starting from 0.
  • column (number) — The cell column index, starting from 0.

Returns

  • (ITableInfoWithUnitId | undefined) — The table information or undefined if not found

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()

const cellB2 = fWorksheet.getRange('B2')
const row = cellB2.getRow()
const column = cellB2.getColumn()
console.log('debugger tableInfo', fWorksheet.getTableByCell(row, column))

// Insert a table in the range B2:F11
const fRange = fWorksheet.getRange('B2:F11')
const success = await fWorksheet.addTable(
  'name-1',
  fRange.getRange(),
  'id-1',
  {
    tableStyleId: 'table-default-4',
  },
)
console.log('debugger tableInfo2', fWorksheet.getTableByCell(row, column))

removeTable

Remove a table from the worksheet

Signature

removeTable(tableId: string): Promise<boolean>

Parameters

  • tableId (string) — The table id

Returns

  • (Promise<boolean>) — Whether the table was removed successfully

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()
const tableInfo = fWorkbook.getTableInfo('id-1')
console.log('debugger tableInfo', tableInfo)

if (tableInfo) {
  // Remove the table with the specified id
  await fWorksheet.removeTable('id-1')
}

resetFilter

Reset the column filter of a table

Signature

resetFilter(tableId: string, column: number): Promise<boolean>

Parameters

  • tableId (string) — The table id
  • column (number) — The column index, starting from 0. For example, the first column is 0, the second column is 1, and so on.

Returns

  • (Promise<boolean>) — Whether the table filter was reset successfully

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()

// Insert a table in the range B2:F11
const fRange = fWorksheet.getRange('B2:F11')
const success = await fWorksheet.addTable(
  'name-1',
  fRange.getRange(),
  'id-1',
  {
    tableStyleId: 'table-default-4',
  },
)

if (success) {
  // Set the filter for the second column
  await fWorksheet.setTableFilter('id-1', 1, {
    filterType: univerAPI.Enum.TableColumnFilterTypeEnum.condition,
    filterInfo: {
      conditionType: univerAPI.Enum.TableConditionTypeEnum.Number,
      compareType: univerAPI.Enum.TableNumberCompareTypeEnum.GreaterThan,
      expectedValue: 10,
    },
  })

  // Reset the filter for the second column after 3 seconds
  setTimeout(async () => {
    await fWorksheet.resetFilter('id-1', 1)
  }, 3000)

  const tableInfo = fWorkbook.getTableInfo('id-1')
  console.log('debugger tableInfo', tableInfo)
}

setTableFilter

Set the filter for a table column

Signature

setTableFilter(tableId: string, column: number, filter: ITableFilterItem): Promise<boolean>

Parameters

  • tableId (string) — The table id
  • column (number) — The table column index, starting from 0. For example, the first column is 0, the second column is 1, and so on.
  • filter (ITableFilterItem) — The filter item

Returns

  • (Promise<boolean>) — Whether the table filter was set successfully

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()

// Insert a table in the range B2:F11
const fRange = fWorksheet.getRange('B2:F11')
const success = await fWorksheet.addTable(
  'name-1',
  fRange.getRange(),
  'id-1',
  {
    tableStyleId: 'table-default-4',
  },
)

if (success) {
  // Set the filter for the second column
  await fWorksheet.setTableFilter('id-1', 1, {
    filterType: univerAPI.Enum.TableColumnFilterTypeEnum.condition,
    filterInfo: {
      conditionType: univerAPI.Enum.TableConditionTypeEnum.Number,
      compareType: univerAPI.Enum.TableNumberCompareTypeEnum.GreaterThan,
      expectedValue: 10,
    },
  })

  const tableInfo = fWorkbook.getTableInfo('id-1')
  console.log('debugger tableInfo', tableInfo)
}

setTableName

Set the name of a table

Signature

setTableName(tableId: string, tableName: string): Promise<boolean> | boolean

Parameters

  • tableId (string) — The table id
  • tableName (string) — The new table name

Returns

  • (Promise<boolean> | boolean) — Whether the table name was set successfully

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()

// Insert a table in the range B2:F11
const fRange = fWorksheet.getRange('B2:F11')
const success = await fWorksheet.addTable(
  'name-1',
  fRange.getRange(),
  'id-1',
  {
    tableStyleId: 'table-default-4',
  },
)

if (success) {
  // Update the table name after 3 seconds
  setTimeout(async () => {
    await fWorksheet.setTableName('id-1', 'new-name')

    const tableInfo = fWorkbook.getTableInfo('id-1')
    console.log('debugger tableInfo', tableInfo)
  }, 3000)
}

setTableRange

Set the range of a table

Signature

setTableRange(tableId: string, rangeInfo: ITableRange): Promise<boolean>

Parameters

  • tableId (string) — The table id
  • rangeInfo (ITableRange) — The new range information

Returns

  • (Promise<boolean>) — Whether the table range was set successfully

Examples

const fWorkbook = univerAPI.getActiveWorkbook()
const fWorksheet = fWorkbook.getActiveSheet()

// Insert a table in the range B2:F11
const fRange = fWorksheet.getRange('B2:F11')
const success = await fWorksheet.addTable(
  'name-1',
  fRange.getRange(),
  'id-1',
  {
    tableStyleId: 'table-default-4',
  },
)

if (success) {
  // Update the table range to B2:F21 after 3 seconds
  setTimeout(async () => {
    const newRange = fWorksheet.getRange('B2:F21')
    await fWorksheet.setTableRange('id-1', newRange.getRange())

    const tableInfo = fWorkbook.getTableInfo('id-1')
    console.log('debugger tableInfo', tableInfo)
  }, 3000)
}