| 123456789101112131415161718192021222324252627282930313233343536373839 |
- import {
- Paragraph,
- Table,
- TableRow,
- TableCell,
- } from "docx"
- export function convertDomTableToDocxTable(tableEl: HTMLTableElement): Table {
- const rows = Array.from(tableEl.rows).map((htmlRow) => {
- const cells = Array.from(htmlRow.cells).map((htmlCell) => {
- const text = htmlCell.textContent?.trim() || ""
- return new TableCell({
- children: [new Paragraph(text)],
- columnSpan: htmlCell.colSpan > 1 ? htmlCell.colSpan : undefined,
- rowSpan: htmlCell.rowSpan > 1 ? htmlCell.rowSpan : undefined,
- })
- })
- return new TableRow({ children: cells })
- })
- return new Table({ rows })
- }
- const handleTable = (el: Element): Table | undefined => {
- const tag = el.tagName.toLowerCase()
- if (tag === "table") {
- if (el instanceof HTMLTableElement) {
- return convertDomTableToDocxTable(el)
- }
- }
- }
- export default {
- handleTable,
- config: {} // 无配置
- }
|