形状

形状(Shapes)可以直接绘制在工作表上,用于流程图、注释标记、提示框和可视化说明。

插件模式

安装

Shell
pnpm add @univerjs-pro/engine-formula @univerjs/drawing @univerjs/drawing-ui @univerjs/sheets-drawing @univerjs/sheets-drawing-ui @univerjs-pro/sheets-shape @univerjs-pro/sheets-shape-ui

形状还依赖 @univerjs-pro/engine-formula 中的 UniverProFormulaEnginePlugin。请将基础初始化代码中原有的 UniverFormulaEnginePlugin 导入和注册替换为该插件。两种公式引擎不能同时注册,否则插入形状时可能因依赖缺失而报错。

形状插件依赖绘图插件,因此在注册形状插件之前,需要先注册 UniverDrawingPlugin、UniverSheetsDrawingPlugin、UniverDrawingUIPlugin 和 UniverSheetsDrawingUIPlugin。

使用

TypeScript
import { UniverProFormulaEnginePlugin } from '@univerjs-pro/engine-formula'import { UniverSheetsShapePlugin } from '@univerjs-pro/sheets-shape'import { UniverSheetsShapeUIPlugin } from '@univerjs-pro/sheets-shape-ui'import SheetsShapeUIZhCN from '@univerjs-pro/sheets-shape-ui/locale/zh-CN'import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverDrawingPlugin } from '@univerjs/drawing'import { UniverDrawingUIPlugin } from '@univerjs/drawing-ui'import DrawingUIZhCN from '@univerjs/drawing-ui/locale/zh-CN'import { UniverSheetsDrawingPlugin } from '@univerjs/sheets-drawing'import { UniverSheetsDrawingUIPlugin } from '@univerjs/sheets-drawing-ui'import SheetsDrawingUIZhCN from '@univerjs/sheets-drawing-ui/locale/zh-CN'import '@univerjs-pro/sheets-shape/facade'import '@univerjs/drawing-ui/lib/index.css'import '@univerjs/sheets-drawing-ui/lib/index.css'import '@univerjs-pro/sheets-shape-ui/lib/index.css'const univer = new Univer({  locale: LocaleType.ZH_CN,  locales: {    [LocaleType.ZH_CN]: mergeLocales(      DrawingUIZhCN,       SheetsDrawingUIZhCN,       SheetsShapeUIZhCN,     ),  },})univer.registerPlugin(UniverProFormulaEnginePlugin)univer.registerPlugin(UniverDrawingPlugin)univer.registerPlugin(UniverDrawingUIPlugin)univer.registerPlugin(UniverSheetsDrawingPlugin)univer.registerPlugin(UniverSheetsDrawingUIPlugin)univer.registerPlugin(UniverSheetsShapePlugin)univer.registerPlugin(UniverSheetsShapeUIPlugin)

如果持有 Univer 商业许可证,客户端配置见在客户端使用许可证。

移动端适配

请先配置移动端编辑器。在本文插件模式的初始化代码中,将下表中的桌面端注册替换为同一个包导出的移动端插件,并且必须在打开文档前完成替换。模型插件、样式、语言包和 Facade 导入保持不变。不要同时注册两个版本。

来源包桌面端注册移动端注册
@univerjs/drawing-uiUniverDrawingUIPluginUniverDrawingMobileUIPlugin
@univerjs/sheets-drawing-uiUniverSheetsDrawingUIPluginUniverSheetsDrawingMobileUIPlugin
@univerjs-pro/sheets-shape-uiUniverSheetsShapeUIPluginUniverSheetsShapeMobileUIPlugin

预设模式

形状功能包含在 @univerjs/preset-sheets-advanced 预设中。该预设已注册 UniverProFormulaEnginePlugin,无需手动替换公式引擎。

安装

UniverSheetsAdvancedPreset 来自 @univerjs/preset-sheets-advanced,运行时依赖 UniverSheetsDrawingPreset 预设,因此需要先安装 @univerjs/preset-sheets-drawing。

Shell
pnpm add @univerjs/preset-sheets-drawing @univerjs/preset-sheets-advanced

使用

TypeScript
import { UniverSheetsAdvancedPreset } from '@univerjs/preset-sheets-advanced'import UniverPresetSheetsAdvancedZhCN from '@univerjs/preset-sheets-advanced/locales/zh-CN'import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreZhCN from '@univerjs/preset-sheets-core/locales/zh-CN'import { UniverSheetsDrawingPreset } from '@univerjs/preset-sheets-drawing'import UniverPresetSheetsDrawingZhCN from '@univerjs/preset-sheets-drawing/locales/zh-CN'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-drawing/lib/index.css'import '@univerjs/preset-sheets-advanced/lib/index.css'const { univerAPI } = createUniver({  locale: LocaleType.ZH_CN,  locales: {    [LocaleType.ZH_CN]: mergeLocales(      UniverPresetSheetsCoreZhCN,      UniverPresetSheetsDrawingZhCN,       UniverPresetSheetsAdvancedZhCN,     ),  },  presets: [    UniverSheetsCorePreset(),    UniverSheetsDrawingPreset(),     UniverSheetsAdvancedPreset(),   ],})

如果持有 Univer 商业许可证,客户端配置见在客户端使用许可证。

Facade API

完整的 Facade API 类型定义见 FacadeAPI。

引入

插件模式说明

只有插件模式需要手动导入 Facade 包。预设模式已内置对应的 Facade 包,无需额外导入。

TypeScript
import '@univerjs-pro/sheets-shape/facade'

插入基础形状

FWorksheet.insertShape 接收标准的形状创建数据,并返回一个可直接操作的 Facade 实例。

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const rectShape = fWorksheet.insertShape({  shapeType: univerAPI.Enum.ShapeTypeEnum.Rect,  transform: { left: 120, top: 80, width: 240, height: 120 },  shapeData: {    fill: {      fillType: univerAPI.Enum.ShapeFillEnum.SolidFill,      color: '#e6f4ff',    },    stroke: {      lineStrokeType: univerAPI.Enum.ShapeLineTypeEnum.SolidLine,      color: '#1677ff',      width: 2,    },  },})if (!rectShape) throw new Error('Shape could not be inserted.')

编辑形状文字

以下示例沿用 insertShape() 返回的形状对象,可以是上一个示例创建的形状,也可以是下方代码片段中使用的形状。getText() 返回实时的 FShapeText 对象:getPlainText() 读取纯文本,setText() 替换全部文字,字体、颜色和对齐方式均由对应的方法设置。

TypeScript
if (!rectShape) throw new Error('Shape is not available')const shapeText = rectShape.getText()console.log(shapeText.getPlainText())shapeText  .setText('Quarterly review')  .setFontSize(18)  .setColor('#2563eb')  .setBold(true)  .setHorizontalAlign(univerAPI.Enum.HorizontalAlign.CENTER)  .setVerticalAlign(univerAPI.Enum.VerticalAlign.MIDDLE)

setText() 会整体替换已有文字;同一个形状需要多种样式时,使用 setRichText()。要编辑现有的富文本,请先调用 getRichText()?.copy(),修改构建器后再传给 setRichText()。该构建器是脱离文档的副本,仅修改它不会自动写回文档。其他选项见文字样式和文本框内边距与布局。

TypeScript
const richText = univerAPI.newRichText()  .text('Quarterly ')  .bold('review')shapeText.setRichText(richText)

插入并连接连接线形状

使用 FWorksheet.insertShape 插入连接线,再通过返回的 FConnectorShape Facade 绑定两端。

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const leftShape = fWorksheet.insertShape({  shapeType: univerAPI.Enum.ShapeTypeEnum.RoundRect,  transform: { left: 80, top: 80, width: 180, height: 100 },})const rightShape = fWorksheet.insertShape({  shapeType: univerAPI.Enum.ShapeTypeEnum.Ellipse,  transform: { left: 420, top: 200, width: 180, height: 100 },})const connectorShape = fWorksheet.insertShape({  shapeType: univerAPI.Enum.ShapeTypeEnum.BentConnector3,  transform: { left: 240, top: 130, width: 240, height: 120 },})if (!leftShape || !rightShape || !connectorShape) {  throw new Error('Shapes could not be inserted.')}const startSite = leftShape.getConnectionSites()[0]const rightSites = rightShape.getConnectionSites()const endSite = rightSites[2] ?? rightSites[0]if (!startSite || !endSite) throw new Error('Connection site not found.')connectorShape  .bindStart(leftShape.getId(), startSite.index)  .bindEnd(rightShape.getId(), endSite.index)  .setEndArrow(univerAPI.Enum.ShapeArrowTypeEnum.Arrow)

更新和删除形状

更新和删除均通过形状的 Facade 实例完成。

TypeScript
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const firstShape = fWorksheet.getShapes().find(shape => !shape.isConnectorShape())if (firstShape) {  firstShape    .setStrokeColor('#ff4d4f')    .setStrokeWidth(3)    .setSolidFill('#fff1f0')  // 如有需要,可在更新后删除该形状  const removed = firstShape.remove()  console.log(removed)}

形状格式刷

选中源形状,启用格式刷,再选择目标形状。填充、描边、效果和受支持的文字格式会复制到目标形状,目标形状自身的标识、几何信息、文字内容和公式绑定保持不变。清除格式后恢复到插入时的默认样式。SmartArt 不参与此操作。对应的形状 UI 插件会自动注册这一行为。

你觉得这篇文档如何?

© 2026 DreamNum Co., Ltd.