群組
分組功能也稱大綱或維度大綱,用於將工作表中的行或列歸入可摺疊的分組,便於整理和彙總大型資料集。
簡介
在 Univer Sheets 中,分組功能支援以下操作:
- 對行或列分組:將行或列按層級歸組,以整理資料。
- 摺疊和展開分組:單擊即可顯示或隱藏分組中的行或列。
- 巢狀分組:資料層次較深時,可建立多層分組。
分組通常用於以下場景:
- 彙總詳細資料:將明細行歸入彙總行下方,使檢視更清晰。
- 整理大型資料集:將相關的列或行放在一起,便於查詢和瀏覽。
- 製作可摺疊報表:允許使用者按需展開或收起報表的某一部分。
外掛模式
安裝
Shell
pnpm add @univerjs-pro/sheets-outline @univerjs-pro/sheets-outline-uiShell
npm install @univerjs-pro/sheets-outline @univerjs-pro/sheets-outline-uiShell
yarn add @univerjs-pro/sheets-outline @univerjs-pro/sheets-outline-uiShell
bun add @univerjs-pro/sheets-outline @univerjs-pro/sheets-outline-ui使用
TypeScript
import { UniverSheetsOutlinePlugin } from '@univerjs-pro/sheets-outline'import { UniverSheetsOutlineUIPlugin } from '@univerjs-pro/sheets-outline-ui'import SheetsOutlineUIZhTW from '@univerjs-pro/sheets-outline-ui/locale/zh-TW'import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import '@univerjs-pro/sheets-outline/facade'import '@univerjs-pro/sheets-outline-ui/lib/index.css'const univer = new Univer({ locale: LocaleType.ZH_TW, locales: { [LocaleType.ZH_TW]: mergeLocales( SheetsOutlineUIZhTW, ), },})univer.registerPlugin(UniverSheetsOutlinePlugin)univer.registerPlugin(UniverSheetsOutlineUIPlugin)如已取得 Univer 商業許可證,配置方式見在客戶端中使用許可證。
行動裝置支援
請先配置行動版編輯器。在本頁外掛模式的初始化程式碼中,將下列桌面版註冊替換為同一包匯出的行動版外掛,並且必須在開啟文件之前完成替換。模型外掛、樣式、語言套件和 Facade 匯入均需保留,不要同時註冊兩個版本。
| 套件 | 桌面版註冊 | 行動版註冊 |
|---|---|---|
@univerjs-pro/sheets-outline-ui | UniverSheetsOutlineUIPlugin | UniverSheetsOutlineMobileUIPlugin |
預設模式
群組功能包含在 @univerjs/preset-sheets-advanced 預設中。
安裝
Shell
pnpm add @univerjs/preset-sheets-advancedShell
npm install @univerjs/preset-sheets-advancedShell
yarn add @univerjs/preset-sheets-advancedShell
bun add @univerjs/preset-sheets-advanced使用
TypeScript
import { UniverSheetsAdvancedPreset } from '@univerjs/preset-sheets-advanced'import UniverPresetSheetsAdvancedZhTW from '@univerjs/preset-sheets-advanced/locales/zh-TW'import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreZhTW from '@univerjs/preset-sheets-core/locales/zh-TW'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-advanced/lib/index.css'const { univerAPI } = createUniver({ locale: LocaleType.ZH_TW, locales: { [LocaleType.ZH_TW]: mergeLocales( UniverPresetSheetsCoreZhTW, UniverPresetSheetsAdvancedZhTW, ), }, presets: [ UniverSheetsCorePreset(), UniverSheetsAdvancedPreset(), ],})如已取得 Univer 商業許可證,配置方式見在客戶端中使用許可證。
Facade API
完整的 Facade API 型別定義見 FacadeAPI。
導入
外掛模式提示
只有外掛模式需要手動引入 Facade 包。預設模式已包含對應的 Facade 包,無需額外匯入。
TypeScript
import '@univerjs-pro/sheets-outline/facade'建立列群組
使用 Facade API 建立行分組:
TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// 將第 2 到 6 列群組(從 0 開始索引:startRow=1, numRows=5)fWorksheet.addRowOutline(1, 5)建立欄群組
列分組的建立方式相同:
TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// 將第 B 到 E 欄群組(從 0 開始索引:startColumn=1, numColumns=4)fWorksheet.addColumnOutline(1, 4)摺疊和展開群組
分組也可以在程式碼中摺疊或展開:
TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const rowOutlines = fWorksheet.getDimensionOutlines(univerAPI.Enum.DimensionOutlineAxis.ROW)if (rowOutlines.length > 0) { // 摺疊第一個列群組 fWorksheet.setDimensionOutlineCollapsed(rowOutlines[0].id, true) // 稍後展開 fWorksheet.setDimensionOutlineCollapsed(rowOutlines[0].id, false)}移除群組
按分組 ID 移除指定的分組:
TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const rowOutlines = fWorksheet.getDimensionOutlines(univerAPI.Enum.DimensionOutlineAxis.ROW)if (rowOutlines.length > 0) { fWorksheet.removeDimensionOutline(rowOutlines[0].id)}清除範圍內的群組
清除指定範圍內的所有分組:
TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// 移除第 2 到 10 列範圍內完全包含的所有列群組fWorksheet.clearDimensionOutlines(univerAPI.Enum.DimensionOutlineAxis.ROW, 1, 9)查詢群組
讀取工作表上已有的分組:
TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// 取得所有列群組const rowOutlines = fWorksheet.getDimensionOutlines(univerAPI.Enum.DimensionOutlineAxis.ROW)rowOutlines.forEach((outline) => { console.log(outline.id, outline.start, outline.end, outline.collapsed)})// 取得所有群組(包括列和欄)const allOutlines = fWorksheet.getDimensionOutlines()群組資料結構
一個分組包含以下屬性:
| 屬性 | 類型 | 說明 |
|---|---|---|
id | string | 分組的唯一標識 |
unitId | string | 工作簿的 Unit ID |
subUnitId | string | 工作表的 sheet ID |
axis | DimensionOutlineAxis | 分組所在的軸(row 或 column) |
start | number | 從 0 開始的起始索引(含) |
end | number | 從 0 開始的結束索引(含) |
collapsed | boolean | 群組目前是否摺疊 |
驗證與錯誤
建立分組時會執行以下校驗:
- 無效範圍:範圍必須有效,即非負、數量不為零。
- 超出邊界:範圍不能超出工作表邊界。
- 交叉分組:新分組不能和已有分組交叉。
- 最大深度:分組有巢狀深度上限。
取消分組時傳入的範圍必須完整包含該分組的範圍,否則校驗不透過。
校驗不透過時命令不會執行,錯誤會透過 SheetsOutlineErrorService 發出。
你覺得這篇文檔如何?