要素

要素は、ボードの可視コンテンツです:テキスト、画像、形状、およびコネクタ。 FBoardファサードを介して行われた変更は、通常のコマンドパスに従います。そうしない、やり直し、コラボレーションはそれらにも適用されます。

要素を読み取る

アクティブなページを読む前にアクティブなボードを入手してください。 安定したID、タイプ、バインド、および注文のみが必要な場合は、フルの内部スナップショットに応じて記述子を使用します。

TypeScript
const board = univerAPI.getActiveBoard()if (!board) throw new Error('No active board')const elements = board.describeElements()const shapes = board.findElements({  elementType: univerAPI.Enum.BoardElementType.Shape,})

図形とテキストを挿入する

低レベルの要素のスナップショットを作成する代わりに、セマンティックインサートメソッドを使用します。 insertShapeでは、transformはジオメトリを運び、返されたライブシェイプハンドルはテキストとスタイルのAPIを公開します。

TypeScript
const shape = board.insertShape({  shapeType: univerAPI.Enum.ShapeTypeEnum.RoundRect,  transform: { left: 80, top: 80, width: 180, height: 100 },  name: 'Review card',})if (!shape) throw new Error('Cannot insert shape')shape.getText().setText('Review')shape.setRotation(6)

insertText はスタンドアローンテキストを追加し、insertImageは画像を追加します。複数の要素を 1 つのコマンドで追加する必要がある場合は、バッチバリアントを使用します。

図形のテキストを編集する

insertShape() が返した形状を下例、または前のセクションで得られる形状を使用してください。 getText()は、ライブ FShapeText(ファシェプテキスト)ハンドルを返します。 getPlainText()を呼び出して、テキスト、setText()がすべてを交換し、フォント、色、アライメントメソッドをフォーマットします。

TypeScript
if (!shape) throw new Error('Shape is not available')const shapeText = shape.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)

要素を接続する

コネクターは、既存の要素 ID に添付できます。 スタイル、ラベル、またはエンドポイントが後で変更されると、返されたコネクタまたは ID を保持します。

TypeScript
const target = board.insertShape({  shapeType: univerAPI.Enum.ShapeTypeEnum.Rect,  transform: { left: 360, top: 80, width: 180, height: 100 },})if (!target) throw new Error('Cannot insert target')const connector = board.insertConnector({  fromElementId: shape.getId(),  toElementId: target.getId(),  style: { endMarker: { type: 'filledTriangle', size: 'md' } },})if (!connector) throw new Error('Cannot insert connector')

要素を変形・配置する

変更しなければならないプロパティのみを渡します。 注文、アライメント、配布、および自動レイアウト方法は、要素 ID も受け付けます。

TypeScript
if (!board.setElementTransform(shape.getId(), { left: 120, top: 140, rotation: 12 })) {  throw new Error('Cannot transform shape')}if (!board.bringElementsToFront([shape.getId()])) {  throw new Error('Cannot bring shape to front')}

要素を削除する

removeElementは単一の要素を取除き、removeElements はバッチ原子を取除きます。 どちらでも、コマンドが成功したかどうかを返します。

TypeScript
if (!board.removeElements([connector.id, target.getId()])) {  throw new Error('Cannot remove elements')}

組み立てられたグループおよびスイムレーンのために、コンテナ&スイムレーンを続いて下さい。板正面の参照およびボード要素データモデルはより多くの細部を提供します。

このガイドはいかがですか?

© 2026 DreamNum Co., Ltd.