From da6ab6afb3c9ab62166f936102e09d63d32dd2c0 Mon Sep 17 00:00:00 2001 From: DdGnoybab <107293558+DdGnoybab@users.noreply.github.com> Date: Tue, 19 Nov 2024 14:46:29 +0800 Subject: [PATCH 1/3] add cell merge #0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现表格单元格合并 --- core/parser.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/core/parser.go b/core/parser.go index 6e9099d..794cbfa 100644 --- a/core/parser.go +++ b/core/parser.go @@ -388,20 +388,76 @@ func (p *Parser) ParseDocxBlockTable(t *lark.DocxBlockTable) string { // - First row as header // - Ignore cell merging var rows [][]string + mergeInfoMap := map[int64]map[int64]*lark.DocxBlockTablePropertyMergeInfo{} + + // 构建单元格合并信息的映射 + if t.Property.MergeInfo != nil { + for i, merge := range t.Property.MergeInfo { + rowIndex := int64(i) / t.Property.ColumnSize + colIndex := int64(i) % t.Property.ColumnSize + if _, exists := mergeInfoMap[int64(rowIndex)]; !exists { + mergeInfoMap[int64(rowIndex)] = map[int64]*lark.DocxBlockTablePropertyMergeInfo{} + } + mergeInfoMap[rowIndex][colIndex] = merge + } + } for i, blockId := range t.Cells { block := p.blockMap[blockId] cellContent := p.ParseDocxBlock(block, 0) cellContent = strings.ReplaceAll(cellContent, "\n", "") rowIndex := int64(i) / t.Property.ColumnSize - if len(rows) < int(rowIndex)+1 { + colIndex := int64(i) % t.Property.ColumnSize + // 初始化行 + for len(rows) <= int(rowIndex) { rows = append(rows, []string{}) } - rows[rowIndex] = append(rows[rowIndex], cellContent) + for len(rows[rowIndex]) <= int(colIndex) { + rows[rowIndex] = append(rows[rowIndex], "") + } + // 设置单元格内容 + rows[rowIndex][colIndex] = cellContent } + // 渲染为 HTML 表格 buf := new(strings.Builder) - buf.WriteString(renderMarkdownTable(rows)) - buf.WriteString("\n") + buf.WriteString("\n") + + // 跟踪已经处理过的合并单元格 + processedCells := map[string]bool{} + + // 构建 HTML 表格内容 + for rowIndex, row := range rows { + buf.WriteString("\n") + for colIndex, cellContent := range row { + cellKey := fmt.Sprintf("%d-%d", rowIndex, colIndex) + + // 跳过已处理的单元格 + if processedCells[cellKey] { + continue + } + + mergeInfo := mergeInfoMap[int64(rowIndex)][int64(colIndex)] + if mergeInfo != nil { + // 合并单元格 + buf.WriteString(fmt.Sprintf( + ``, + mergeInfo.RowSpan, mergeInfo.ColSpan, cellContent, + )) + // 标记合并范围内的所有单元格为已处理 + for r := rowIndex; r < rowIndex+int(mergeInfo.RowSpan); r++ { + for c := colIndex; c < colIndex+int(mergeInfo.ColSpan); c++ { + processedCells[fmt.Sprintf("%d-%d", r, c)] = true + } + } + } else { + // 普通单元格 + buf.WriteString(fmt.Sprintf("", cellContent)) + } + } + buf.WriteString("\n") + } + buf.WriteString("
%s%s
\n") + return buf.String() } From d1a176db9b0e9739305b423106930f636a092a31 Mon Sep 17 00:00:00 2001 From: DdGnoybab <107293558+DdGnoybab@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:19:12 +0800 Subject: [PATCH 2/3] Update unittest.yaml --- .github/workflows/unittest.yaml | 62 ++++++++++++++++----------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/.github/workflows/unittest.yaml b/.github/workflows/unittest.yaml index 64d931e..623f260 100644 --- a/.github/workflows/unittest.yaml +++ b/.github/workflows/unittest.yaml @@ -1,34 +1,34 @@ -name: unittest +# name: unittest -on: - pull_request: - types: [opened, reopened, synchronize] - branches: - - main - workflow_dispatch: +# on: +# pull_request: +# types: [opened, reopened, synchronize] +# branches: +# - main +# workflow_dispatch: -jobs: - build: - runs-on: ubuntu-latest - steps: - - name: Checkout the code - uses: actions/checkout@v2 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.21 - - name: Check format - run: test -z $(gofmt -l .) - - name: Run testing - env: - FEISHU_APP_ID: ${{ secrets.FEISHU_APP_ID }} - FEISHU_APP_SECRET: ${{ secrets.FEISHU_APP_SECRET }} - run: | - touch .env - go mod download - go test -v ./... - - name: Build cmd tool - run: make - - name: Build docker image - run: make image +# jobs: +# build: +# runs-on: ubuntu-latest +# steps: +# - name: Checkout the code +# uses: actions/checkout@v2 +# - name: Set up Go +# uses: actions/setup-go@v4 +# with: +# go-version: 1.21 +# - name: Check format +# run: test -z $(gofmt -l .) +# - name: Run testing +# env: +# FEISHU_APP_ID: ${{ secrets.FEISHU_APP_ID }} +# FEISHU_APP_SECRET: ${{ secrets.FEISHU_APP_SECRET }} +# run: | +# touch .env +# go mod download +# go test -v ./... +# - name: Build cmd tool +# run: make +# - name: Build docker image +# run: make image From aeaf4b81d383ae4f8b5dc3d51b66ac9bf28f3bf9 Mon Sep 17 00:00:00 2001 From: DdGnoybab <107293558+DdGnoybab@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:22:07 +0800 Subject: [PATCH 3/3] Update unittest.yaml --- .github/workflows/unittest.yaml | 62 ++++++++++++++++----------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/.github/workflows/unittest.yaml b/.github/workflows/unittest.yaml index 623f260..64d931e 100644 --- a/.github/workflows/unittest.yaml +++ b/.github/workflows/unittest.yaml @@ -1,34 +1,34 @@ -# name: unittest +name: unittest -# on: -# pull_request: -# types: [opened, reopened, synchronize] -# branches: -# - main -# workflow_dispatch: +on: + pull_request: + types: [opened, reopened, synchronize] + branches: + - main + workflow_dispatch: -# jobs: -# build: -# runs-on: ubuntu-latest -# steps: -# - name: Checkout the code -# uses: actions/checkout@v2 -# - name: Set up Go -# uses: actions/setup-go@v4 -# with: -# go-version: 1.21 -# - name: Check format -# run: test -z $(gofmt -l .) -# - name: Run testing -# env: -# FEISHU_APP_ID: ${{ secrets.FEISHU_APP_ID }} -# FEISHU_APP_SECRET: ${{ secrets.FEISHU_APP_SECRET }} -# run: | -# touch .env -# go mod download -# go test -v ./... -# - name: Build cmd tool -# run: make -# - name: Build docker image -# run: make image +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout the code + uses: actions/checkout@v2 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: 1.21 + - name: Check format + run: test -z $(gofmt -l .) + - name: Run testing + env: + FEISHU_APP_ID: ${{ secrets.FEISHU_APP_ID }} + FEISHU_APP_SECRET: ${{ secrets.FEISHU_APP_SECRET }} + run: | + touch .env + go mod download + go test -v ./... + - name: Build cmd tool + run: make + - name: Build docker image + run: make image