1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-09 09:42:55 +08:00

feat: enable table customization and styles for paragraphs (#39)

This commit is contained in:
David Johnson
2024-09-24 11:15:22 -04:00
committed by GitHub
parent f03badc77c
commit 7782d22c85
6 changed files with 114 additions and 19 deletions

View File

@@ -20,10 +20,19 @@
package docx
import (
"reflect"
)
// AddTable add a new table to body by col*row
//
// unit: twips (1/20 point)
func (f *Docx) AddTable(row int, col int) *Table {
func (f *Docx) AddTable(
row int,
col int,
tableWidth int64,
borderColors *APITableBorderColors,
) *Table {
trs := make([]*WTableRow, row)
for i := 0; i < row; i++ {
cells := make([]*WTableCell, col)
@@ -40,16 +49,25 @@ func (f *Docx) AddTable(row int, col int) *Table {
TableCells: cells,
}
}
borderColors.applyDefault()
wTableWidth := &WTableWidth{Type: "auto"}
if tableWidth > 0 {
wTableWidth = &WTableWidth{W: tableWidth}
}
tbl := &Table{
TableProperties: &WTableProperties{
Width: &WTableWidth{Type: "auto"},
Width: wTableWidth,
TableBorders: &WTableBorders{
Top: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
Left: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
Bottom: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
Right: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
InsideH: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
InsideV: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
Top: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.Top},
Left: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.Left},
Bottom: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.Bottom},
Right: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.Right},
InsideH: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.InsideH},
InsideV: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.InsideV},
},
Look: &WTableLook{
Val: "0000",
@@ -65,7 +83,12 @@ func (f *Docx) AddTable(row int, col int) *Table {
// AddTableTwips add a new table to body by height and width
//
// unit: twips (1/20 point)
func (f *Docx) AddTableTwips(rowHeights []int64, colWidths []int64) *Table {
func (f *Docx) AddTableTwips(
rowHeights []int64,
colWidths []int64,
tableWidth int64,
borderColors *APITableBorderColors,
) *Table {
grids := make([]*WGridCol, len(colWidths))
trs := make([]*WTableRow, len(rowHeights))
for i, w := range colWidths {
@@ -95,16 +118,25 @@ func (f *Docx) AddTableTwips(rowHeights []int64, colWidths []int64) *Table {
}
}
}
borderColors.applyDefault()
wTableWidth := &WTableWidth{Type: "auto"}
if tableWidth > 0 {
wTableWidth = &WTableWidth{W: tableWidth}
}
tbl := &Table{
TableProperties: &WTableProperties{
Width: &WTableWidth{Type: "auto"},
Width: wTableWidth,
TableBorders: &WTableBorders{
Top: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
Left: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
Bottom: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
Right: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
InsideH: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
InsideV: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: "000000"},
Top: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.Top},
Left: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.Left},
Bottom: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.Bottom},
Right: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.Right},
InsideH: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.InsideH},
InsideV: &WTableBorder{Val: "single", Size: 4, Space: 0, Color: borderColors.InsideV},
},
Look: &WTableLook{
Val: "0000",
@@ -162,3 +194,39 @@ func (c *WTableCell) Shade(val, color, fill string) *WTableCell {
}
return c
}
//Replaces any index that is blank with "#000000"
func CheckBorderColors (borderColors [6]string) [6]string {
for i, _ := range borderColors{
if borderColors[i] == "" {
borderColors[i] = "#000000"
}
}
return borderColors
}
type APITableBorderColors struct {
Top string
Left string
Bottom string
Right string
InsideH string
InsideV string
}
func (tbc *APITableBorderColors) applyDefault() {
tbcR := reflect.ValueOf(tbc).Elem()
for i := 0; i < tbcR.NumField(); i++ {
if tbcR.Field(i).IsZero() {
tbcR.Field(i).SetString("#000000")
}
}
}