Skip to content

Commit

Permalink
spreadsheet: add support for column formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaliance committed Sep 9, 2017
1 parent cdb3f64 commit ceb8570
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
1 change: 1 addition & 0 deletions measurement/distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
Pixel72 = 1.0 / 72.0 * Inch
Pixel96 = 1.0 / 96.0 * Inch
HalfPoint = 1.0 / 2.0 * Point
Character = 7 * Point
Millimeter = 2.83465 * Point
Centimeter = 10 * Millimeter
Inch = 72 * Point
Expand Down
53 changes: 53 additions & 0 deletions spreadsheet/column.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting [email protected].

package spreadsheet

import (
"baliance.com/gooxml"
"baliance.com/gooxml/measurement"
sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml"
)

// Column represents a column within a sheet. It's only used for formatting
// purposes, so it's possible to construct a sheet without configuring columns.
type Column struct {
x *sml.CT_Col
}

// X returns the inner wrapped XML type.
func (c Column) X() *sml.CT_Col {
return c.x
}

// SetWidth controls the width of a column.
func (c Column) SetWidth(w measurement.Distance) {
c.x.WidthAttr = gooxml.Float64(float64(w / measurement.Character))
}

// SetBestFit controls if the column width should be 'best fit'.
func (c Column) SetBestFit(b bool) {
if !b {
c.x.BestFitAttr = nil
} else {
c.x.BestFitAttr = gooxml.Bool(true)
}
}

// SetStyle sets the cell style for an entire column.
func (c Column) SetStyle(cs CellStyle) {
c.x.StyleAttr = gooxml.Uint32(cs.Index())
}

// SetHidden controls the visibility of a column.
func (c Column) SetHidden(b bool) {
if !b {
c.x.HiddenAttr = nil
} else {
c.x.HiddenAttr = gooxml.Bool(true)
}
}
35 changes: 33 additions & 2 deletions spreadsheet/sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,10 @@ func (s Sheet) Extents() string {
IndexToColumn(maxCol), maxRow)
}

func (c Sheet) AddConditionalFormatting(cellRanges []string) ConditionalFormatting {
// AddConditionalFormatting adds conditional formatting to the sheet.
func (s Sheet) AddConditionalFormatting(cellRanges []string) ConditionalFormatting {
cfmt := sml.NewCT_ConditionalFormatting()
c.x.ConditionalFormatting = append(c.x.ConditionalFormatting, cfmt)
s.x.ConditionalFormatting = append(s.x.ConditionalFormatting, cfmt)

// TODO: fix generator so this is not a pointer to a slice
slc := make(sml.ST_Sqref, 0, 0)
Expand All @@ -343,3 +344,33 @@ func (c Sheet) AddConditionalFormatting(cellRanges []string) ConditionalFormatti
}
return ConditionalFormatting{cfmt}
}

// Column returns or creates a column that with a given index (1-N). Columns
// can span multiple column indices, this method will return the column that
// applies to a column index if it exists or create a new column that only
// applies to the index passed in otherwise.
func (s Sheet) Column(idx uint32) Column {
// scan for any existing column that covers this index
for _, colSet := range s.x.Cols {
for _, col := range colSet.Col {
if idx >= col.MinAttr && idx <= col.MaxAttr {
return Column{col}
}
}
}
// does a column set exist?
var colSet *sml.CT_Cols
if len(s.x.Cols) == 0 {
colSet = sml.NewCT_Cols()
s.x.Cols = append(s.x.Cols, colSet)
} else {
colSet = s.x.Cols[0]
}

// create our new column
col := sml.NewCT_Col()
col.MinAttr = idx
col.MaxAttr = idx
colSet.Col = append(colSet.Col, col)
return Column{col}
}

0 comments on commit ceb8570

Please sign in to comment.