# 図形とテキスト

- Human documentation: [https://docs.univer.ai/ja-JP/guides/slides/features/core/shapes-and-text](https://docs.univer.ai/ja-JP/guides/slides/features/core/shapes-and-text)

- Agent Markdown: [https://docs.univer.ai/ja-JP/guides/slides/features/core/shapes-and-text.md](https://docs.univer.ai/ja-JP/guides/slides/features/core/shapes-and-text.md)

- Requested language: `ja-JP`

- Content language: `ja-JP`

- Documentation version: `1.1.0`

- Source: [slides/features/core/shapes-and-text.ja-JP.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/slides/features/core/shapes-and-text.ja-JP.mdx)

---

Slides は共有 Shape エンジンを使って、編集可能な図形、コネクタ、図形内テキストを提供します。`FSlide.insertShape()` は Slides のコマンドパイプラインに接続された Shape Facade を返します。

## 図形を挿入する

```ts
const slide = univerAPI.getActivePresentation()?.getActiveSlide()

if (!slide) {
  throw new Error('No active slide')
}

const shape = slide.insertShape({
  shapeType: univerAPI.Enum.ShapeTypeEnum.RoundRect,
  transform: { left: 80, top: 64, width: 320, height: 120 },
  visible: true,
  selectable: true,
  shapeData: {
    fill: {
      fillType: univerAPI.Enum.ShapeFillEnum.SolidFill,
      color: '#dbeafe',
    },
  },
})
```

Shape ホストアダプターが利用できない場合や入力を作成できない場合、`insertShape()` は `null` を返します。図形を挿入する前に Slides 構成の Shape 依存関係を登録してください。

## リッチテキストを追加する

```ts
if (shape) {
  const richText = univerAPI.newRichText()
    .text('Quarterly ')
    .bold('review')

  shape.getText()
    .setRichText(richText)
    .setHorizontalAlign(univerAPI.Enum.HorizontalAlign.CENTER)
    .setVerticalAlign(univerAPI.Enum.VerticalAlign.MIDDLE)
}
```

図形の取得には `slide.getShape(id)` または `slide.getShapes()` を使用します。共通要素メソッドで位置、サイズ、表示、名前、説明を更新でき、`slide.deleteElement(shape)` で削除できます。

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

以下または前の例で `insertShape()` が返す図形を使用します。`getText()` はライブの [FShapeText](https://docs.univer.ai/ja-JP/reference/facade/shape-text.md) オブジェクトを返します。`getPlainText()` でテキストを読み取り、`setText()` で全文を置き換え、フォント、色、配置を設定できます。

```ts
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()` に渡します。ビルダーの変更だけでは反映されません。[文字スタイル](https://docs.univer.ai/ja-JP/reference/facade/shape-text.md#settextstyle)と[テキストボックスの余白・レイアウト](https://docs.univer.ai/ja-JP/reference/facade/shape-text.md#settextboxoptions)も参照してください。

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

## 図形の書式コピー

元の図形を選択し、書式コピーを有効にして対象の図形を選びます。塗りつぶし、線、効果、対応する文字書式をコピーし、対象の ID、形状、文字内容、数式の関連付けは保持します。書式のクリアは挿入時の既定値に戻します。SmartArt は対象外です。対応する図形 UI プラグインが自動登録します。
