Tableau
Une table transforme une plage en un objet nommé qui peut être géré dans son ensemble. Elle conserve son propre style, son propre rang d'en-tête et ses propres filtres. Une fois créé, le Facade API peut le remodeler, le filtrer, le redimensionner ou le supprimer.
Mode plugin
Installation
pnpm add @univerjs/sheets-table @univerjs/sheets-table-uinpm install @univerjs/sheets-table @univerjs/sheets-table-uiyarn add @univerjs/sheets-table @univerjs/sheets-table-uibun add @univerjs/sheets-table @univerjs/sheets-table-uiUtilisation
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverSheetsTablePlugin } from '@univerjs/sheets-table'import { UniverSheetsTableUIPlugin } from '@univerjs/sheets-table-ui'import SheetsTableUIEnUS from '@univerjs/sheets-table-ui/locale/en-US'import '@univerjs/sheets-table-ui/lib/index.css'import '@univerjs/sheets-table/facade'const univer = new Univer({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( SheetsTableUIEnUS, ), },})univer.registerPlugin(UniverSheetsTablePlugin)univer.registerPlugin(UniverSheetsTableUIPlugin)Prise en charge mobile
Configurez d'abord le éditeur mobile. Dans la configuration en mode plugin sur cette page, remplacez l'enregistrement de bureau ci-dessous par son exportation mobile à partir du même paquet, avant d'ouvrir un document . Gardez les plugins modèles, styles, locaux et importations de Facade, et n'enregistrez pas les deux variantes.
| Paquet | Inscription de bureau | Inscription mobile |
|---|---|---|
@univerjs/sheets-table-ui | UniverSheetsTableUIPlugin | UniverSheetsTableMobileUIPlugin |
Mode prédéfini
Installation
pnpm add @univerjs/preset-sheets-tablenpm install @univerjs/preset-sheets-tableyarn add @univerjs/preset-sheets-tablebun add @univerjs/preset-sheets-tableUtilisation
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { UniverSheetsTablePreset } from '@univerjs/preset-sheets-table'import UniverPresetSheetsTableEnUS from '@univerjs/preset-sheets-table/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-table/lib/index.css'const { univerAPI } = createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetSheetsCoreEnUS, UniverPresetSheetsTableEnUS, ), }, presets: [ UniverSheetsCorePreset(), UniverSheetsTablePreset(), ],})API Facade
Consultez les définitions complètes des types dans FacadeAPI.
Importation
Note du mode greffon
Seul le mode plugin nécessite l'importation manuelle du paquet Facade. Le mode Preset inclut déjà le paquet Facade correspondant, donc aucune importation supplémentaire n'est nécessaire.
import '@univerjs/sheets-table/facade'Insérer un tableau
FWorkbook.addTableinsère une table dans une feuille de travail spécifique du cahier.FWorksheet.addTableinsère une table dans la feuille de travail.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Insert a table in the range B2:F11const 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)}Récupérer un tableau
FWorkbook.getTableListretourne toutes les tables du cahier.FWorksheet.getSubTableInfosretourne toutes les tables de la feuille de travail.
const fWorkbook = univerAPI.getActiveWorkbook()const tables = fWorkbook.getTableList()console.log('debugger tables', tables)FWorkbook.getTableInforetourne les informations de table par ID de table.FWorkbook.getTableInfoByNameretourne les informations de la table par nom de table.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Insert a table in the range B2:F11const fRange = fWorksheet.getRange('B2:F11')const success = await fWorksheet.addTable( 'name-1', fRange.getRange(), 'id-1', { tableStyleId: 'table-default-4', },)if (success) { const tableInfo = fWorkbook.getTableInfo('id-1') console.log('debugger tableInfo', tableInfo)}FWorksheet.getTableByCellretourne les informations de la table par position de cellule.
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:F11const 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))Définir un filtre
FWorkbook.setTableFilterdéfinit le filtre pour une table dans le cahier.FWorksheet.setTableFilterdéfinit le filtre pour une table dans la feuille de travail.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Insert a table in the range B2:F11const 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)}FWorksheet.resetFilterréinitialise le filtre pour une table dans la feuille de travail.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Insert a table in the range B2:F11const 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)}Modifier ou supprimer un tableau
FWorkbook.removeTablesupprime une table du cahier.FWorksheet.removeTablesupprime une table de la feuille de travail.
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')}FWorksheet.setTableRangedéplace ou redimensionne une table dans la feuille de travail.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Insert a table in the range B2:F11const 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)}FWorksheet.setTableNamerenomme une table dans la feuille de travail.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Insert a table in the range B2:F11const 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)}Ajouter un thème de tableau
FWorksheet.addTableThemeajoute un thème à une table dans la feuille de travail.
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Insert a table in the range B2:F11const 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)}Déplacer un tableau avec ses cellules
Si vous déplacez une plage de cellules qui contient une table entière, la table se déplace avec elle, soit dans la même feuille de travail, soit vers une autre feuille de travail dans le même cahier de travail. La plage doit inclure la ligne d'en-tête et toutes les lignes de table. La table conserve son identité, son nom, son style, ses métadonnées de colonnes et ses filtres, et les références structurées la suivent vers le nouvel emplacement. La destination doit être modifiable, à l'intérieur des limites de la feuille de travail et ne doit pas chevaucher une autre table.
Que pensez-vous de ce guide ?