From be7f17fd2eac383fb0f541ba8291ce7d979bf9e4 Mon Sep 17 00:00:00 2001 From: Oliver Fuerst Date: Mon, 9 Dec 2024 12:26:05 +0000 Subject: [PATCH 1/2] add VerticalAlign method to run This allows defining subscript and superscript for a run. --- docx/run.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docx/run.go b/docx/run.go index cb37fe4..cc7577e 100644 --- a/docx/run.go +++ b/docx/run.go @@ -182,3 +182,15 @@ func (r *Run) Style(value string) *Run { r.getProp().Style = ctypes.NewRunStyle(value) return r } + +// VerticalAlign sets the vertical alignment for the run text. +// +// Parameter: A value from the stypes.VerticalAlignRun type indicating the desired vertical alignment. One of: +// +// VerticalAlignRunBaseline, VerticalAlignRunSuperscript, VerticalAlignRunSubscript +// +// Returns: The modified Run instance with the updated vertical alignment. +func (r *Run) VerticalAlign(value stypes.VerticalAlignRun) *Run { + r.getProp().VertAlign = ctypes.NewGenSingleStrVal(value) + return r +} From c178d5fe9bbb538346c6ae776d5d050e4bf0b8bd Mon Sep 17 00:00:00 2001 From: Oliver Fuerst Date: Mon, 9 Dec 2024 12:26:56 +0000 Subject: [PATCH 2/2] add new Table property functions --- docx/table.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/docx/table.go b/docx/table.go index 7ac1d9e..6e62c5d 100644 --- a/docx/table.go +++ b/docx/table.go @@ -19,6 +19,23 @@ func (t *Table) unmarshalXML(d *xml.Decoder, start xml.StartElement) error { return t.ct.UnmarshalXML(d, start) } +func (t *Table) Width(v int, u stypes.TableWidth) *Table { + w := ctypes.TableWidth{ + Width: &v, + WidthType: &u, + } + t.ct.TableProp.Width = &w + return t +} + +func (t *Table) Grid(widths ...uint64) *Table { + for _, w := range widths { + col := ctypes.Column{Width: &w} + t.ct.Grid.Col = append(t.ct.Grid.Col, col) + } + return t +} + func NewTable(root *RootDoc) *Table { return &Table{ root: root, @@ -196,3 +213,37 @@ func (c *Cell) AddEmptyPara() *Paragraph { return p } + +// ColSpan sets the number of columns a cell should span across in a table. +func (c *Cell) ColSpan(cols int) *Cell { + if c.ct.Property != nil { + c.ct.Property.GridSpan = &ctypes.DecimalNum{Val: cols} + } + return c +} + +// RowSpan sets the cell to span vertically in a table, indicating it is part of a vertically merged group of cells. +func (c *Cell) RowSpan() *Cell { + if c.ct.Property != nil { + vMerge := ctypes.AnnotationVMergeRest + c.ct.Property.CellMerge = &ctypes.CellMerge{ + VMerge: &vMerge, + } + } + return c +} + +// VerticalAlign sets the vertical alignment of a cell based on the provided string: "top", "center", "middle", or "bottom". +func (c *Cell) VerticalAlign(valign string) *Cell { + if c.ct.Property != nil { + switch valign { + case "top": + c.ct.Property.VAlign = ctypes.NewGenSingleStrVal(stypes.VerticalJcTop) + case "center", "middle": + c.ct.Property.VAlign = ctypes.NewGenSingleStrVal(stypes.VerticalJcCenter) + case "bottom": + c.ct.Property.VAlign = ctypes.NewGenSingleStrVal(stypes.VerticalJcBottom) + } + } + return c +}