2017-08-28 20:56:18 -05:00
|
|
|
// 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 (
|
2017-09-06 14:53:48 -04:00
|
|
|
"fmt"
|
2017-09-06 16:30:52 -04:00
|
|
|
"log"
|
2017-09-07 12:07:59 -04:00
|
|
|
"sort"
|
2017-09-07 07:23:30 -04:00
|
|
|
"strings"
|
2017-09-06 14:53:48 -04:00
|
|
|
|
2017-08-28 20:56:18 -05:00
|
|
|
"baliance.com/gooxml"
|
2017-09-03 18:39:35 -05:00
|
|
|
"baliance.com/gooxml/common"
|
2017-09-03 11:05:27 -05:00
|
|
|
sml "baliance.com/gooxml/schema/schemas.openxmlformats.org/spreadsheetml"
|
2017-08-28 20:56:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sheet is a single sheet within a workbook.
|
|
|
|
type Sheet struct {
|
2017-09-06 22:00:32 -04:00
|
|
|
w *Workbook
|
|
|
|
cts *sml.CT_Sheet
|
|
|
|
x *sml.Worksheet
|
2017-08-28 20:56:18 -05:00
|
|
|
}
|
|
|
|
|
2017-09-07 14:57:04 -05:00
|
|
|
// X returns the inner wrapped XML type.
|
|
|
|
func (s Sheet) X() *sml.Worksheet {
|
|
|
|
return s.x
|
|
|
|
}
|
|
|
|
|
2017-09-06 16:30:52 -04:00
|
|
|
// Row will return a row with a given row number, creating a new row if
|
2017-09-06 14:53:48 -04:00
|
|
|
// necessary.
|
2017-09-06 16:30:52 -04:00
|
|
|
func (s Sheet) Row(rowNum uint32) Row {
|
2017-09-06 14:53:48 -04:00
|
|
|
// see if the row exists
|
2017-09-06 22:00:32 -04:00
|
|
|
for _, r := range s.x.SheetData.Row {
|
2017-09-06 14:53:48 -04:00
|
|
|
if r.RAttr != nil && *r.RAttr == rowNum {
|
2017-09-06 20:24:36 -04:00
|
|
|
return Row{s.w, s.x, r}
|
2017-09-06 14:53:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// create a new row
|
|
|
|
return s.AddNumberedRow(rowNum)
|
|
|
|
}
|
|
|
|
|
2017-09-06 16:30:52 -04:00
|
|
|
// Cell creates or returns a cell given a cell reference of the form 'A10'
|
|
|
|
func (s Sheet) Cell(cellRef string) Cell {
|
|
|
|
col, row, err := ParseCellReference(cellRef)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error parsing cell reference: %s", err)
|
|
|
|
return s.AddRow().AddCell()
|
|
|
|
}
|
|
|
|
return s.Row(row).Cell(col)
|
|
|
|
}
|
|
|
|
|
2017-09-06 14:53:48 -04:00
|
|
|
// AddNumberedRow adds a row with a given row number. If you reuse a row number
|
|
|
|
// the resulting file will fail validation and fail to open in Office programs. Use
|
2017-09-06 16:30:52 -04:00
|
|
|
// Row instead which creates a new row or returns an existing row.
|
2017-09-06 14:53:48 -04:00
|
|
|
func (s Sheet) AddNumberedRow(rowNum uint32) Row {
|
2017-09-03 11:05:27 -05:00
|
|
|
r := sml.NewCT_Row()
|
2017-09-06 14:53:48 -04:00
|
|
|
r.RAttr = gooxml.Uint32(rowNum)
|
2017-09-06 22:00:32 -04:00
|
|
|
s.x.SheetData.Row = append(s.x.SheetData.Row, r)
|
2017-09-07 12:07:59 -04:00
|
|
|
|
|
|
|
// Excel wants the rows to be sorted
|
|
|
|
sort.Slice(s.x.SheetData.Row, func(i, j int) bool {
|
|
|
|
l := s.x.SheetData.Row[i].RAttr
|
|
|
|
r := s.x.SheetData.Row[j].RAttr
|
|
|
|
if l == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if r == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return *l < *r
|
|
|
|
})
|
|
|
|
|
2017-09-06 20:24:36 -04:00
|
|
|
return Row{s.w, s.x, r}
|
2017-08-28 20:56:18 -05:00
|
|
|
}
|
|
|
|
|
2017-09-06 16:30:52 -04:00
|
|
|
// AddRow adds a new row to a sheet. You can mix this with numbered rows,
|
|
|
|
// however it will get confusing. You should prefer to use either automatically
|
|
|
|
// numbered rows with AddRow or manually numbered rows with Row/AddNumberedRow
|
2017-09-06 14:53:48 -04:00
|
|
|
func (s Sheet) AddRow() Row {
|
|
|
|
maxRowID := uint32(0)
|
|
|
|
// find the max row number
|
2017-09-06 22:00:32 -04:00
|
|
|
for _, r := range s.x.SheetData.Row {
|
2017-09-06 14:53:48 -04:00
|
|
|
if r.RAttr != nil && *r.RAttr > maxRowID {
|
|
|
|
maxRowID = *r.RAttr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.AddNumberedRow(maxRowID + 1)
|
|
|
|
}
|
|
|
|
|
2017-09-02 14:54:17 -05:00
|
|
|
// Name returns the sheet name
|
|
|
|
func (s Sheet) Name() string {
|
2017-09-06 22:00:32 -04:00
|
|
|
return s.cts.NameAttr
|
2017-09-02 14:54:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetName sets the sheet name.
|
|
|
|
func (s Sheet) SetName(name string) {
|
2017-09-06 22:00:32 -04:00
|
|
|
s.cts.NameAttr = name
|
2017-09-02 14:54:17 -05:00
|
|
|
}
|
|
|
|
|
2017-08-28 20:56:18 -05:00
|
|
|
// Validate validates the sheet, returning an error if it is found to be invalid.
|
|
|
|
func (s Sheet) Validate() error {
|
2017-09-06 14:53:48 -04:00
|
|
|
|
2017-09-07 15:39:35 -05:00
|
|
|
// check for re-used row numbers
|
2017-09-06 14:53:48 -04:00
|
|
|
usedRows := map[uint32]struct{}{}
|
2017-09-06 22:00:32 -04:00
|
|
|
for _, r := range s.x.SheetData.Row {
|
2017-09-06 14:53:48 -04:00
|
|
|
if r.RAttr != nil {
|
|
|
|
if _, reusedRow := usedRows[*r.RAttr]; reusedRow {
|
|
|
|
return fmt.Errorf("'%s' reused row %d", s.Name(), *r.RAttr)
|
|
|
|
}
|
|
|
|
usedRows[*r.RAttr] = struct{}{}
|
|
|
|
}
|
2017-09-07 15:39:35 -05:00
|
|
|
// or re-used column labels within a row
|
2017-09-06 14:53:48 -04:00
|
|
|
usedCells := map[string]struct{}{}
|
|
|
|
for _, c := range r.C {
|
|
|
|
if c.RAttr == nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-09-06 16:30:52 -04:00
|
|
|
|
2017-09-06 14:53:48 -04:00
|
|
|
if _, reusedCell := usedCells[*c.RAttr]; reusedCell {
|
|
|
|
return fmt.Errorf("'%s' reused cell %s", s.Name(), *c.RAttr)
|
|
|
|
}
|
|
|
|
usedCells[*c.RAttr] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2017-09-07 15:39:35 -05:00
|
|
|
|
|
|
|
if len(s.Name()) > 31 {
|
|
|
|
return fmt.Errorf("sheet name '%s' has %d characters, max length is 31", s.Name(), len(s.Name()))
|
|
|
|
}
|
2017-09-06 22:00:32 -04:00
|
|
|
if err := s.cts.Validate(); err != nil {
|
2017-09-06 14:53:48 -04:00
|
|
|
return err
|
|
|
|
}
|
2017-09-06 22:00:32 -04:00
|
|
|
return s.x.Validate()
|
2017-08-28 20:56:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateWithPath validates the sheet passing path informaton for a better
|
|
|
|
// error message
|
|
|
|
func (s Sheet) ValidateWithPath(path string) error {
|
2017-09-06 22:00:32 -04:00
|
|
|
return s.cts.ValidateWithPath(path)
|
2017-08-28 20:56:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rows returns all of the rows in a sheet.
|
|
|
|
func (s Sheet) Rows() []Row {
|
|
|
|
ret := []Row{}
|
2017-09-06 22:00:32 -04:00
|
|
|
for _, r := range s.x.SheetData.Row {
|
2017-09-06 20:24:36 -04:00
|
|
|
ret = append(ret, Row{s.w, s.x, r})
|
2017-08-28 20:56:18 -05:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2017-09-03 18:39:35 -05:00
|
|
|
|
|
|
|
// SetDrawing sets the worksheet drawing. A worksheet can have a reference to a
|
|
|
|
// single drawing, but the drawing can have many charts.
|
|
|
|
func (s Sheet) SetDrawing(d Drawing) {
|
|
|
|
var rel common.Relationships
|
|
|
|
for i, wks := range s.w.xws {
|
2017-09-06 22:00:32 -04:00
|
|
|
if wks == s.x {
|
2017-09-03 18:39:35 -05:00
|
|
|
rel = s.w.xwsRels[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add relationship from drawing to the sheet
|
|
|
|
var drawingID string
|
|
|
|
for i, dr := range d.wb.drawings {
|
|
|
|
if dr == d.x {
|
|
|
|
rel := rel.AddAutoRelationship(gooxml.DocTypeSpreadsheet, i+1, gooxml.DrawingType)
|
|
|
|
drawingID = rel.ID()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-09-06 22:00:32 -04:00
|
|
|
s.x.Drawing = sml.NewCT_Drawing()
|
|
|
|
s.x.Drawing.IdAttr = drawingID
|
2017-09-03 18:39:35 -05:00
|
|
|
}
|
2017-09-06 22:13:08 -04:00
|
|
|
|
|
|
|
// AddHyperlink adds a hyperlink to a sheet. Adding the hyperlink to the sheet
|
|
|
|
// and setting it on a cell is more efficient than setting hyperlinks directly
|
|
|
|
// on a cell.
|
|
|
|
func (s Sheet) AddHyperlink(url string) common.Hyperlink {
|
|
|
|
// store the relationships so we don't need to do a lookup here?
|
|
|
|
for i, ws := range s.w.xws {
|
|
|
|
if ws == s.x {
|
|
|
|
// add a hyperlink relationship in the worksheet relationships file
|
|
|
|
return s.w.xwsRels[i].AddHyperlink(url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// should never occur
|
|
|
|
return common.Hyperlink{}
|
|
|
|
}
|
2017-09-07 07:23:30 -04:00
|
|
|
|
|
|
|
// RangeReference converts a range reference of the form 'A1:A5' to 'Sheet
|
|
|
|
// 1'!$A$1:$A$5 . Renaming a sheet after calculating a range reference will
|
|
|
|
// invalidate the reference.
|
|
|
|
func (s Sheet) RangeReference(n string) string {
|
|
|
|
sp := strings.Split(n, ":")
|
|
|
|
fc, fr, _ := ParseCellReference(sp[0])
|
|
|
|
from := fmt.Sprintf("$%s$%d", fc, fr)
|
|
|
|
if len(sp) == 1 {
|
|
|
|
return fmt.Sprintf(`'%s'!%s`, s.Name(), from)
|
|
|
|
}
|
|
|
|
tc, tr, _ := ParseCellReference(sp[1])
|
|
|
|
to := fmt.Sprintf("$%s$%d", tc, tr)
|
|
|
|
return fmt.Sprintf(`'%s'!%s:%s`, s.Name(), from, to)
|
|
|
|
}
|
2017-09-07 14:57:04 -05:00
|
|
|
|
|
|
|
const autoFilterName = "_xlnm._FilterDatabase"
|
|
|
|
|
|
|
|
// ClearAutoFilter removes the autofilters from the sheet.
|
|
|
|
func (s Sheet) ClearAutoFilter() {
|
|
|
|
s.x.AutoFilter = nil
|
|
|
|
sn := "'" + s.Name() + "'!"
|
|
|
|
// see if we have a defined auto filter name for the sheet
|
|
|
|
for _, dn := range s.w.DefinedNames() {
|
|
|
|
if dn.Name() == autoFilterName {
|
|
|
|
if strings.HasPrefix(dn.Content(), sn) {
|
|
|
|
s.w.RemoveDefinedName(dn)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAutoFilter creates autofilters on the sheet. These are the automatic
|
|
|
|
// filters that are common for a header row. The RangeRef should be of the form
|
|
|
|
// "A1:C5" and cover the entire range of cells to be filtered, not just the
|
|
|
|
// header. SetAutoFilter replaces any existing auto filter on the sheet.
|
|
|
|
func (s Sheet) SetAutoFilter(rangeRef string) {
|
|
|
|
// this should have no $ in it
|
|
|
|
rangeRef = strings.Replace(rangeRef, "$", "", -1)
|
|
|
|
|
|
|
|
s.x.AutoFilter = sml.NewCT_AutoFilter()
|
|
|
|
s.x.AutoFilter.RefAttr = gooxml.String(rangeRef)
|
|
|
|
sn := "'" + s.Name() + "'!"
|
|
|
|
var sdn DefinedName
|
|
|
|
|
|
|
|
// see if we already have a defined auto filter name for the sheet
|
|
|
|
for _, dn := range s.w.DefinedNames() {
|
|
|
|
if dn.Name() == autoFilterName {
|
|
|
|
if strings.HasPrefix(dn.Content(), sn) {
|
|
|
|
sdn = dn
|
|
|
|
// name must match, but make sure rangeRef matches as well
|
|
|
|
sdn.SetContent(s.RangeReference(rangeRef))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no existing name found, so add a new one
|
|
|
|
if sdn.X() == nil {
|
|
|
|
sdn = s.w.AddDefinedName(autoFilterName, s.RangeReference(rangeRef))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, ws := range s.w.xws {
|
|
|
|
if ws == s.x {
|
|
|
|
sdn.SetLocalSheetID(uint32(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|