1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-04 23:30:25 +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

@@ -69,3 +69,11 @@ func (p *Paragraph) AddPageBreaks() *Run {
p.Children = append(p.Children, run)
return run
}
func (p *Paragraph) AddStyle(val string) *Paragraph {
if p.Properties == nil {
p.Properties = &ParagraphProperties{}
}
p.Properties.Style = &Style{Val: val}
return p
}

View File

@@ -83,6 +83,15 @@ func (r *Run) Highlight(val string) *Run {
return r
}
func (r *Run) Strike(val bool) *Run {
trueFalseStr := "false"
if val {
trueFalseStr = "true"
}
r.RunProperties.Strike = &Strike{Val: trueFalseStr}
return r
}
// AddTab add a tab in front of the run
func (r *Run) AddTab() *Run {
r.Children = append(r.Children, &Tab{})

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")
}
}
}

View File

@@ -29,12 +29,22 @@ import (
)
func TestTableStructure(t *testing.T) {
borderColors := &APITableBorderColors{
"#ff0000",
"#ff0000",
"#ff0000",
"#ff0000",
"#ff0000",
"",
}
w := New().WithDefaultTheme()
// add new paragraph
para1 := w.AddParagraph()
// add text
para1.AddText("table")
tab1 := w.AddTable(4, 3).Justification("center")
tab1 := w.AddTable(4, 3, 1000, borderColors).Justification("center")
tab1.TableProperties.Position = &WTablePositioningProperties{LeftFromText: 2333}
para2 := tab1.TableRows[3].Justification("center").TableCells[2].Shade("clear", "auto", "E7E6E6").AddParagraph()
r, err := para2.AddAnchorDrawingFrom("testdata/fumiama.JPG")

2
xml/default/word/fontTable.xml Executable file → Normal file
View File

@@ -38,4 +38,4 @@
<w:pitch w:val="variable"/>
<w:sig w:usb0="E00002FF" w:usb1="400004FF" w:usb2="00000000" w:usb3="00000000" w:csb0="0000019F" w:csb1="00000000"/>
</w:font>
</w:fonts>
</w:fonts>

View File

@@ -461,4 +461,4 @@
<w:sz w:val="24"/>
</w:rPr>
</w:style>
</w:styles>
</w:styles>