mirror of
https://github.com/unidoc/unioffice.git
synced 2025-04-25 13:48:53 +08:00
spreadsheet: add support for column formatting
This commit is contained in:
parent
cdb3f64c4a
commit
ceb8570558
@ -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
|
||||
|
53
spreadsheet/column.go
Normal file
53
spreadsheet/column.go
Normal file
@ -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 sales@baliance.com.
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
@ -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)
|
||||
@ -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}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user