From d186678dbb7b76708e1ab64d27cdda080d143c51 Mon Sep 17 00:00:00 2001 From: Todd Date: Sat, 2 Sep 2017 15:27:53 -0500 Subject: [PATCH] spreadsheet: merge spreadsheet/style package into spreadsheet --- _examples/spreadsheet/simple/simple.xlsx | Bin 8983 -> 8983 bytes _examples/spreadsheet/wrapped-text/main.go | 30 ++++++++++++++++++ .../spreadsheet/wrapped-text/wrapped.xlsx | Bin 0 -> 7553 bytes spreadsheet/{styles => }/borders.go | 2 +- spreadsheet/cell.go | 3 +- spreadsheet/cell_test.go | 2 +- spreadsheet/{styles => }/cellstyle.go | 30 +++++++++++++++--- spreadsheet/{styles => }/fills.go | 2 +- spreadsheet/{styles => }/font.go | 9 +++--- spreadsheet/{styles => }/patternfill.go | 2 +- spreadsheet/{styles => }/stylesheet.go | 8 ++--- spreadsheet/{styles => }/stylesheet_test.go | 14 ++++---- spreadsheet/{styles => }/testdata/styles.xml | 0 spreadsheet/workbook.go | 7 ++-- spreadsheet/workbook_test.go | 8 ++--- 15 files changed, 83 insertions(+), 34 deletions(-) create mode 100644 _examples/spreadsheet/wrapped-text/main.go create mode 100644 _examples/spreadsheet/wrapped-text/wrapped.xlsx rename spreadsheet/{styles => }/borders.go (97%) rename spreadsheet/{styles => }/cellstyle.go (72%) rename spreadsheet/{styles => }/fills.go (97%) rename spreadsheet/{styles => }/font.go (91%) rename spreadsheet/{styles => }/patternfill.go (98%) rename spreadsheet/{styles => }/stylesheet.go (96%) rename spreadsheet/{styles => }/stylesheet_test.go (83%) rename spreadsheet/{styles => }/testdata/styles.xml (100%) diff --git a/_examples/spreadsheet/simple/simple.xlsx b/_examples/spreadsheet/simple/simple.xlsx index dffb6668dfe09f91d8c65ec9baab495ca5394c67..58c741e30cea3069dda573fbed6aa349c48135ef 100644 GIT binary patch delta 148 zcmbR4HrAMxGc}CY6Povskw>foMgJHH;woJm*$0-Ns!87XQaP zl?5a|Rp>NWeUijFu(*))9uR->1qFK$C8%f)qP!GsKvajKEr>d2Fho>HjK-wWG4UWfy|Y9Ey+2j&yt+5JAPo*SkkD5XPI2kK5xjhVaIFSN+bpb7y@)W2S%& z^ZKi6Y(TR}lS>mAj@2BaAu8sTkG-@EE58-_O?R)0E97F%3;LO(acH#_?dLUXvwk0V(f;TSy+Vg=Qy6 zBwI=)xPsK2+4&(GRO)MB$DZehNw5K}3YC6B0N;dLzm zMea8}D&Tf_a(dt#8!I#!z%(jQ2Q1bp^zaR~^^YIhSby%wsl6Y5di`*#h37^ix;_u+ zdJCb%M_5n`qw0q)rYwlAQ@FGUy1Gh`O)7^>_q;M*i;y7}WgiaxHa^$#__Qm)l#c$4 zkoCxwu;WpWkt($7OwErZ>O>Ayjt<9C0y+ zD@M6xVtbLOxKO1N{cRd?DgxFHlGq<6323+KyB${HVap~}{pEJYG#1aRel=6MwZ060 z{3SimYT?;ztruNu6;8jfxq`HdJStu0x7t+rGzW~eS&+_SvqxeX1|`chlaa|2!@y?| z!wf+Aop#4BOdJN9$+*f{fm_5^iHwhvA~{oHJSic8}Om<-Qmv%LUjt3kuUMw@ht zv|)a0O(i8Z#4sl*CPFqCu*$x0mA z338aO*_L8}^Lt5ub<(x@MR`YX6VvmqW4b{)u(84$TLp@MD3foR(2y5%~@a#)$j@Uq(AxSL!=5|saojI8dN-2}>WPq%}xI=;KAsI`WZbDYo^R&zgYhWiBZLUC}iy3Wg ztfkI5dR@b^4isAonPfZTk>;Y*AZKfN87UQfn-T0}j$rl8;^P3B z(oa{;ye8)?a>csmCI`>0SUpj4*0-tg@kTX#p`t9Um1>nqh($Y#b)R8b1S|0wQTyB@$Ib6 zz^%DqLAW|Tf#R`K9bQ5x oaA;T;>wH|NK!mD}FW@nsf&1`kWBu^6*z+j-&VxIhehr`g0r9xSAOHXW literal 0 HcmV?d00001 diff --git a/spreadsheet/styles/borders.go b/spreadsheet/borders.go similarity index 97% rename from spreadsheet/styles/borders.go rename to spreadsheet/borders.go index 78ac39bb..55c7b9ab 100644 --- a/spreadsheet/styles/borders.go +++ b/spreadsheet/borders.go @@ -5,7 +5,7 @@ // appearing in the file LICENSE included in the packaging of this file. A // commercial license can be purchased by contacting sales@baliance.com. -package styles +package spreadsheet import sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" diff --git a/spreadsheet/cell.go b/spreadsheet/cell.go index 600e83b2..b4360099 100644 --- a/spreadsheet/cell.go +++ b/spreadsheet/cell.go @@ -12,7 +12,6 @@ import ( "baliance.com/gooxml" sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" - "baliance.com/gooxml/spreadsheet/styles" ) // Cell is a single cell within a sheet. @@ -73,7 +72,7 @@ func (c Cell) SetBool(v bool) { // SetStyle applies a style to the cell. This style is referenced in the generated XML // via CellStyle.Index(). -func (c Cell) SetStyle(cs styles.CellStyle) { +func (c Cell) SetStyle(cs CellStyle) { c.x.SAttr = gooxml.Uint32(cs.Index()) } diff --git a/spreadsheet/cell_test.go b/spreadsheet/cell_test.go index 144e9578..33f5c28b 100644 --- a/spreadsheet/cell_test.go +++ b/spreadsheet/cell_test.go @@ -13,7 +13,7 @@ import sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" func TestCell(t *testing.T) { wb := spreadsheet.New() - sheet := wb.AddSheet("test") + sheet := wb.AddSheet() row := sheet.AddRow() cell := row.AddCell() diff --git a/spreadsheet/styles/cellstyle.go b/spreadsheet/cellstyle.go similarity index 72% rename from spreadsheet/styles/cellstyle.go rename to spreadsheet/cellstyle.go index 87819cf3..59a29247 100644 --- a/spreadsheet/styles/cellstyle.go +++ b/spreadsheet/cellstyle.go @@ -5,20 +5,42 @@ // appearing in the file LICENSE included in the packaging of this file. A // commercial license can be purchased by contacting sales@baliance.com. -package styles +package spreadsheet import ( "baliance.com/gooxml" sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" ) +// CellStyle is a formatting style for a cell. CellStyles are spreadsheet global +// and can be applied to cells across sheets. type CellStyle struct { xf *sml.CT_Xf xfs *sml.CT_CellXfs } -func NewCellStyle(xf *sml.CT_Xf, xfs *sml.CT_CellXfs) CellStyle { - return CellStyle{xf, xfs} +// Wrapped returns true if the cell will wrap text. +func (cs CellStyle) Wrapped() bool { + if cs.xf.Alignment == nil { + return false + } + if cs.xf.Alignment.WrapTextAttr == nil { + return false + } + return *cs.xf.Alignment.WrapTextAttr +} + +// SetWrapped configures the cell to wrap text. +func (cs CellStyle) SetWrapped(b bool) { + if cs.xf.Alignment == nil { + cs.xf.Alignment = sml.NewCT_CellAlignment() + } + if !b { + cs.xf.Alignment.WrapTextAttr = nil + } else { + cs.xf.Alignment.WrapTextAttr = gooxml.Bool(true) + cs.xf.ApplyAlignmentAttr = gooxml.Bool(true) + } } // SetHorizontalAlignment sets the horizontal alignment of a cell style. @@ -26,8 +48,8 @@ func (cs CellStyle) SetHorizontalAlignment(a sml.ST_HorizontalAlignment) { if cs.xf.Alignment == nil { cs.xf.Alignment = sml.NewCT_CellAlignment() } - cs.xf.ApplyAlignmentAttr = gooxml.Bool(true) cs.xf.Alignment.HorizontalAttr = a + cs.xf.ApplyAlignmentAttr = gooxml.Bool(true) } // SetVerticalAlignment sets the vertical alignment of a cell style. diff --git a/spreadsheet/styles/fills.go b/spreadsheet/fills.go similarity index 97% rename from spreadsheet/styles/fills.go rename to spreadsheet/fills.go index de1a671c..cfdeaf66 100644 --- a/spreadsheet/styles/fills.go +++ b/spreadsheet/fills.go @@ -5,7 +5,7 @@ // appearing in the file LICENSE included in the packaging of this file. A // commercial license can be purchased by contacting sales@baliance.com. -package styles +package spreadsheet import sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" diff --git a/spreadsheet/styles/font.go b/spreadsheet/font.go similarity index 91% rename from spreadsheet/styles/font.go rename to spreadsheet/font.go index 027a0984..0f60d3f3 100644 --- a/spreadsheet/styles/font.go +++ b/spreadsheet/font.go @@ -5,25 +5,24 @@ // appearing in the file LICENSE included in the packaging of this file. A // commercial license can be purchased by contacting sales@baliance.com. -package styles +package spreadsheet import ( "baliance.com/gooxml/color" sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" ) +// Font allows editing fonts within a spreadsheet stylesheet. type Font struct { font *sml.CT_Font styles *sml.StyleSheet } -func NewFont(font *sml.CT_Font, styles *sml.StyleSheet) Font { - return Font{font, styles} -} - +// X returns the inner wrapped XML type. func (f Font) X() *sml.CT_Font { return f.font } + func (f Font) Index() uint32 { for i, sf := range f.styles.Fonts.Font { if f.font == sf { diff --git a/spreadsheet/styles/patternfill.go b/spreadsheet/patternfill.go similarity index 98% rename from spreadsheet/styles/patternfill.go rename to spreadsheet/patternfill.go index 5f820cfe..32bce595 100644 --- a/spreadsheet/styles/patternfill.go +++ b/spreadsheet/patternfill.go @@ -5,7 +5,7 @@ // appearing in the file LICENSE included in the packaging of this file. A // commercial license can be purchased by contacting sales@baliance.com. -package styles +package spreadsheet import ( "baliance.com/gooxml/color" diff --git a/spreadsheet/styles/stylesheet.go b/spreadsheet/stylesheet.go similarity index 96% rename from spreadsheet/styles/stylesheet.go rename to spreadsheet/stylesheet.go index 2424e3fc..12aeba7a 100644 --- a/spreadsheet/styles/stylesheet.go +++ b/spreadsheet/stylesheet.go @@ -5,7 +5,7 @@ // appearing in the file LICENSE included in the packaging of this file. A // commercial license can be purchased by contacting sales@baliance.com. -package styles +package spreadsheet import ( "errors" @@ -76,7 +76,7 @@ func (s StyleSheet) AddFont() Font { font := sml.NewCT_Font() s.x.Fonts.Font = append(s.x.Fonts.Font, font) s.x.Fonts.CountAttr = gooxml.Uint32(uint32(len(s.x.Fonts.Font))) - return NewFont(font, s.x) + return Font{font, s.x} } // RemoveFont removes a font from the style sheet. It *does not* update styles that refer @@ -96,7 +96,7 @@ func (s StyleSheet) RemoveFont(f Font) error { func (s StyleSheet) Fonts() []Font { ret := []Font{} for _, f := range s.x.Fonts.Font { - ret = append(ret, NewFont(f, s.x)) + ret = append(ret, Font{f, s.x}) } return ret } @@ -106,7 +106,7 @@ func (s StyleSheet) AddCellStyle() CellStyle { xf := sml.NewCT_Xf() s.x.CellXfs.Xf = append(s.x.CellXfs.Xf, xf) s.x.CellXfs.CountAttr = gooxml.Uint32(uint32(len(s.x.CellXfs.Xf))) - return NewCellStyle(xf, s.x.CellXfs) + return CellStyle{xf, s.x.CellXfs} } // Fills returns a Fills object that can be used to add/create/edit fills. diff --git a/spreadsheet/styles/stylesheet_test.go b/spreadsheet/stylesheet_test.go similarity index 83% rename from spreadsheet/styles/stylesheet_test.go rename to spreadsheet/stylesheet_test.go index ee439ce6..36277b64 100644 --- a/spreadsheet/styles/stylesheet_test.go +++ b/spreadsheet/stylesheet_test.go @@ -5,7 +5,7 @@ // appearing in the file LICENSE included in the packaging of this file. A // commercial license can be purchased by contacting sales@baliance.com. -package styles_test +package spreadsheet_test import ( "bytes" @@ -14,7 +14,7 @@ import ( "os" "testing" - "baliance.com/gooxml/spreadsheet/styles" + "baliance.com/gooxml/spreadsheet" "baliance.com/gooxml/testhelper" "baliance.com/gooxml/zippkg" ) @@ -22,25 +22,25 @@ import ( func TestStyleSheetUnmarshal(t *testing.T) { f, err := os.Open("testdata/styles.xml") if err != nil { - t.Fatalf("error reading content types file") + t.Fatalf("error reading styles.xml") } dec := xml.NewDecoder(f) - r := styles.NewStyleSheet() + r := spreadsheet.NewStyleSheet() if err := dec.Decode(r.X()); err != nil { - t.Errorf("error decoding content types: %s", err) + t.Errorf("error decoding styles.xml: %s", err) } got := &bytes.Buffer{} fmt.Fprintf(got, zippkg.XMLHeader) enc := xml.NewEncoder(zippkg.SelfClosingWriter{W: got}) if err := enc.Encode(r.X()); err != nil { - t.Errorf("error encoding content types: %s", err) + t.Errorf("error encoding styles.xml: %s", err) } testhelper.CompareGoldenXML(t, "styles.xml", got.Bytes()) } func TestStyleSheetFonts(t *testing.T) { - ss := styles.NewStyleSheet() + ss := spreadsheet.NewStyleSheet() fc := len(ss.Fonts()) ft := ss.AddFont() diff --git a/spreadsheet/styles/testdata/styles.xml b/spreadsheet/testdata/styles.xml similarity index 100% rename from spreadsheet/styles/testdata/styles.xml rename to spreadsheet/testdata/styles.xml diff --git a/spreadsheet/workbook.go b/spreadsheet/workbook.go index 1d418071..e8d4848c 100644 --- a/spreadsheet/workbook.go +++ b/spreadsheet/workbook.go @@ -19,7 +19,6 @@ import ( "baliance.com/gooxml/common" "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml" - "baliance.com/gooxml/spreadsheet/styles" "baliance.com/gooxml/zippkg" ) @@ -28,7 +27,7 @@ type Workbook struct { common.DocBase x *spreadsheetml.Workbook - StyleSheet styles.StyleSheet + StyleSheet StyleSheet Theme common.Theme SharedStrings SharedStrings xws []*spreadsheetml.Worksheet @@ -43,7 +42,7 @@ func New() *Workbook { wb.AppProperties = common.NewAppProperties() wb.CoreProperties = common.NewCoreProperties() - wb.StyleSheet = styles.NewStyleSheet() + wb.StyleSheet = NewStyleSheet() wb.Rels = common.NewRelationships() wb.wbRels = common.NewRelationships() @@ -148,7 +147,7 @@ func Read(r io.ReaderAt, size int64) (*Workbook, error) { basePaths[wksRel] = basePath wb.xwsRels = append(wb.xwsRels, wksRel) case common.StylesType: - wb.StyleSheet = styles.NewStyleSheet() + wb.StyleSheet = NewStyleSheet() decMap[basePaths[wb.wbRels]+r.Target()] = wb.StyleSheet.X() case common.ThemeType: wb.Theme = common.NewTheme() diff --git a/spreadsheet/workbook_test.go b/spreadsheet/workbook_test.go index acbbc9cd..47ef858a 100644 --- a/spreadsheet/workbook_test.go +++ b/spreadsheet/workbook_test.go @@ -67,7 +67,7 @@ func TestWorkbookUnmarshal(t *testing.T) { func TestSimpleSheet(t *testing.T) { wb := spreadsheet.New() - sheet := wb.AddSheet("foo") + sheet := wb.AddSheet() row := sheet.AddRow() cell := row.AddCell() cell.SetString("testing 123") @@ -114,14 +114,14 @@ func TestSheetCount(t *testing.T) { if wb.SheetCount() != 0 { t.Errorf("expected 0 sheets, got %d", wb.SheetCount()) } - wb.AddSheet("Sheet 1") + wb.AddSheet() if err := wb.Validate(); err != nil { t.Errorf("created an invalid spreadsheet: %s", err) } if wb.SheetCount() != 1 { t.Errorf("expected 1 sheets, got %d", wb.SheetCount()) } - wb.AddSheet("Sheet 2") + wb.AddSheet() if err := wb.Validate(); err != nil { t.Errorf("created an invalid spreadsheet: %s", err) } @@ -132,7 +132,7 @@ func TestSheetCount(t *testing.T) { func TestPreserveSpace(t *testing.T) { ss := spreadsheet.New() - sheet := ss.AddSheet("Sheet 1") + sheet := ss.AddSheet() row := sheet.AddRow() values := []string{" foo ", " bar \t", "foo\r\nbar", "\t\r\nfoo\t123\r\n"} for i, s := range values {