Skip to content

Commit 9506373

Browse files
authored
add cells merge (#148)
* add cell merge #0 实现表格单元格合并 * Update unittest.yaml * Update unittest.yaml
1 parent 58f876a commit 9506373

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

core/parser.go

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,20 +388,76 @@ func (p *Parser) ParseDocxBlockTable(t *lark.DocxBlockTable) string {
388388
// - First row as header
389389
// - Ignore cell merging
390390
var rows [][]string
391+
mergeInfoMap := map[int64]map[int64]*lark.DocxBlockTablePropertyMergeInfo{}
392+
393+
// 构建单元格合并信息的映射
394+
if t.Property.MergeInfo != nil {
395+
for i, merge := range t.Property.MergeInfo {
396+
rowIndex := int64(i) / t.Property.ColumnSize
397+
colIndex := int64(i) % t.Property.ColumnSize
398+
if _, exists := mergeInfoMap[int64(rowIndex)]; !exists {
399+
mergeInfoMap[int64(rowIndex)] = map[int64]*lark.DocxBlockTablePropertyMergeInfo{}
400+
}
401+
mergeInfoMap[rowIndex][colIndex] = merge
402+
}
403+
}
391404
for i, blockId := range t.Cells {
392405
block := p.blockMap[blockId]
393406
cellContent := p.ParseDocxBlock(block, 0)
394407
cellContent = strings.ReplaceAll(cellContent, "\n", "")
395408
rowIndex := int64(i) / t.Property.ColumnSize
396-
if len(rows) < int(rowIndex)+1 {
409+
colIndex := int64(i) % t.Property.ColumnSize
410+
// 初始化行
411+
for len(rows) <= int(rowIndex) {
397412
rows = append(rows, []string{})
398413
}
399-
rows[rowIndex] = append(rows[rowIndex], cellContent)
414+
for len(rows[rowIndex]) <= int(colIndex) {
415+
rows[rowIndex] = append(rows[rowIndex], "")
416+
}
417+
// 设置单元格内容
418+
rows[rowIndex][colIndex] = cellContent
400419
}
401420

421+
// 渲染为 HTML 表格
402422
buf := new(strings.Builder)
403-
buf.WriteString(renderMarkdownTable(rows))
404-
buf.WriteString("\n")
423+
buf.WriteString("<table>\n")
424+
425+
// 跟踪已经处理过的合并单元格
426+
processedCells := map[string]bool{}
427+
428+
// 构建 HTML 表格内容
429+
for rowIndex, row := range rows {
430+
buf.WriteString("<tr>\n")
431+
for colIndex, cellContent := range row {
432+
cellKey := fmt.Sprintf("%d-%d", rowIndex, colIndex)
433+
434+
// 跳过已处理的单元格
435+
if processedCells[cellKey] {
436+
continue
437+
}
438+
439+
mergeInfo := mergeInfoMap[int64(rowIndex)][int64(colIndex)]
440+
if mergeInfo != nil {
441+
// 合并单元格
442+
buf.WriteString(fmt.Sprintf(
443+
`<td rowspan="%d" colspan="%d">%s</td>`,
444+
mergeInfo.RowSpan, mergeInfo.ColSpan, cellContent,
445+
))
446+
// 标记合并范围内的所有单元格为已处理
447+
for r := rowIndex; r < rowIndex+int(mergeInfo.RowSpan); r++ {
448+
for c := colIndex; c < colIndex+int(mergeInfo.ColSpan); c++ {
449+
processedCells[fmt.Sprintf("%d-%d", r, c)] = true
450+
}
451+
}
452+
} else {
453+
// 普通单元格
454+
buf.WriteString(fmt.Sprintf("<td>%s</td>", cellContent))
455+
}
456+
}
457+
buf.WriteString("</tr>\n")
458+
}
459+
buf.WriteString("</table>\n")
460+
405461
return buf.String()
406462
}
407463

0 commit comments

Comments
 (0)