# 表格

> 通过 Facade API 插入、调整大小、设置样式并编辑结构化 PDF 表格。

- Human documentation: [https://docs.univer.ai/zh-CN/guides/pdfs/features/core/tables](https://docs.univer.ai/zh-CN/guides/pdfs/features/core/tables)

- Agent Markdown: [https://docs.univer.ai/zh-CN/guides/pdfs/features/core/tables.md](https://docs.univer.ai/zh-CN/guides/pdfs/features/core/tables.md)

- Requested language: `zh-CN`

- Content language: `zh-CN`

- Documentation version: `1.0.0-rc.0`

- Source: [pdfs/features/core/tables.zh-CN.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/pdfs/features/core/tables.zh-CN.mdx)

---

`FPdfTable` 维护结构化网格，其中包含稳定的单元格包装器，并为每个单元格保存一个可编辑文本 story。

## 插入与编辑表格

```ts
const table = page.insertTable({
  rowCount: 2,
  columnCount: 2,
  cellTexts: ['Owner', 'Status', 'Docs', 'Ready'],
  left: 36,
  top: 36,
  width: 360,
  height: 108,
})

table.resize(3, 2)
table.getCell(2, 0).setText('Legal')
table.getCell(2, 1).setText('Review')
```

行数和列数必须是正安全整数，且表格不得超过 10,000 个单元格。调整大小时会按位置保留重叠的单元格。

## 应用表格主题

```ts
const [preset] = univerAPI.getPdfTableThemePresets()

if (preset) {
  table.setTheme({
    styleId: preset.id,
    options: { firstRow: true },
  })
}
```

`getPdfTableThemePresets()` 返回分离的 `{ id, group }` 描述符。请将预设的 `id` 用作 `styleId`，不要把描述符直接传给 `setTheme()`。

## 单元格样式

```ts
table.getCell(0, 0).setStyle({
  fill: { color: '#eef3ff' },
  fontColor: '#1d4ed8',
  horizontalAlignment: univerAPI.Enum.HorizontalAlign.CENTER,
  verticalAlign: univerAPI.Enum.PdfTableCellVerticalAlign.MIDDLE,
})
```

`FPdfTableCell.setStyle()` 接受容器字段，以及作用于整个单元格的 `fontColor` 和 `horizontalAlignment`。通过 `univerAPI.Enum` 使用 `PdfTableCellVerticalAlign`。

表格方法包括 `getRowCount()`、`getColumnCount()`、`getCell()`、`resize()`、`getTheme()` 和 `setTheme()`。单元格方法包括 `getText()`、`setText()`、`getStyle()` 和 `setStyle()`。
