From 0c9b983cf36d0a77a603b9867e9d6b859ad85837 Mon Sep 17 00:00:00 2001 From: Geoffrey Teale Date: Mon, 18 Nov 2024 18:19:52 +0100 Subject: [PATCH] Apply changes suggested by staticcheck --- cell.go | 16 -- cell_test.go | 14 +- col.go | 583 ++++++++++++++++++++++---------------------- data_validation.go | 3 +- diskv.go | 36 +-- diskv_test.go | 8 +- file.go | 6 +- file_test.go | 44 ++-- format_code.go | 6 +- lib.go | 70 +++--- lib_test.go | 4 +- memory.go | 9 +- read_test.go | 4 +- row_test.go | 9 +- sheet.go | 3 +- sheet_test.go | 10 +- utility.go | 4 - write_test.go | 14 +- xmlStyle.go | 29 --- xmlWorkbook.go | 2 +- xmlWorkbook_test.go | 6 +- xmlWorksheet.go | 4 +- 22 files changed, 380 insertions(+), 504 deletions(-) diff --git a/cell.go b/cell.go index 3a839894..75ec32d4 100644 --- a/cell.go +++ b/cell.go @@ -509,22 +509,6 @@ func (c *Cell) GetNumberFormat() string { return c.NumFmt } -func (c *Cell) formatToFloat(format string) (string, error) { - f, err := strconv.ParseFloat(c.Value, 64) - if err != nil { - return c.Value, err - } - return fmt.Sprintf(format, f), nil -} - -func (c *Cell) formatToInt(format string) (string, error) { - f, err := strconv.ParseFloat(c.Value, 64) - if err != nil { - return c.Value, err - } - return fmt.Sprintf(format, int(f)), nil -} - // getNumberFormat will update the parsedNumFmt struct if it has become out of date, since a cell's NumFmt string is a // public field that could be edited by clients. func (c *Cell) getNumberFormat() *parsedNumberFormat { diff --git a/cell_test.go b/cell_test.go index 9586dc45..913069d4 100644 --- a/cell_test.go +++ b/cell_test.go @@ -629,7 +629,7 @@ func TestCell(t *testing.T) { smallDate := time.Date(1899, 12, 30, 0, 0, 0, 1000, time.UTC) smallExcelTime := TimeToExcelTime(smallDate, false) - c.Assert(true, qt.Equals, 0.0 != smallExcelTime) + c.Assert(0.0, qt.Not(qt.Equals), smallExcelTime) roundTrippedDate := TimeFromExcelTime(smallExcelTime, false) c.Assert(roundTrippedDate, qt.Equals, smallDate) }) @@ -933,16 +933,6 @@ func (fvc *formattedValueChecker) Equals(cell Cell, expected string) { fvc.c.Assert(val, qt.Equals, expected) } -func cellsFormattedValueEquals(t *testing.T, cell *Cell, expected string) { - val, err := cell.FormattedValue() - if err != nil { - t.Error(err) - } - if val != expected { - t.Errorf("Expected cell.FormattedValue() to be %v, got %v", expected, val) - } -} - func TestCellMerge(t *testing.T) { c := qt.New(t) csRunO(c, "MergeAndSave", func(c *qt.C, option FileOption) { @@ -956,7 +946,7 @@ func TestCellMerge(t *testing.T) { cell := row.AddCell() cell.Value = "test" cell.Merge(1, 0) - path := filepath.Join(c.Mkdir(), "merged.xlsx") + path := filepath.Join(t.TempDir(), "merged.xlsx") err = f.Save(path) c.Assert(err, qt.Equals, nil) }) diff --git a/col.go b/col.go index d5f79397..02ad4f21 100644 --- a/col.go +++ b/col.go @@ -6,19 +6,19 @@ const Excel2006MaxRowCount = 1048576 const Excel2006MaxRowIndex = Excel2006MaxRowCount - 1 type Col struct { - Min int - Max int - Hidden *bool - Width *float64 - Collapsed *bool - OutlineLevel *uint8 - BestFit *bool - CustomWidth *bool - Phonetic *bool - numFmt string - parsedNumFmt *parsedNumberFormat - style *Style - outXfID int + Min int + Max int + Hidden *bool + Width *float64 + Collapsed *bool + OutlineLevel *uint8 + BestFit *bool + CustomWidth *bool + Phonetic *bool + numFmt string + parsedNumFmt *parsedNumberFormat + style *Style + outXfID int } // NewColForRange return a pointer to a new Col, which will apply to @@ -27,15 +27,15 @@ type Col struct { // and then apply it to a Sheet by calling sheet.SetColParameters. // Column numbers start from 1. func NewColForRange(min, max int) *Col { - if min < 1 { - panic("min col must be >= 1") - } - if max < min { - // Nice try ;-) - return &Col{Min: max, Max: min} - } - - return &Col{Min: min, Max: max} + if min < 1 { + panic("min col must be >= 1") + } + if max < min { + // Nice try ;-) + return &Col{Min: max, Max: min} + } + + return &Col{Min: min, Max: max} } // SetWidth sets the width of columns that have this Col applied to @@ -43,46 +43,46 @@ func NewColForRange(min, max int) *Col { // maximum digit width of the numbers 0-9 as rendered in the normal // style's font. func (c *Col) SetWidth(width float64) { - c.Width = &width - custom := true - c.CustomWidth = &custom + c.Width = &width + custom := true + c.CustomWidth = &custom } // SetType will set the format string of a column based on the type that you want to set it to. // This function does not really make a lot of sense. func (c *Col) SetType(cellType CellType) { - switch cellType { - case CellTypeString: - c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING] - case CellTypeNumeric: - c.numFmt = builtInNumFmt[builtInNumFmtIndex_INT] - case CellTypeBool: - c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] //TEMP - case CellTypeInline: - c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING] - case CellTypeError: - c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] //TEMP - case CellTypeDate: - // Cells that are stored as dates are not properly supported in this library. - // They should instead be stored as a Numeric with a date format. - c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] - case CellTypeStringFormula: - c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING] - } + switch cellType { + case CellTypeString: + c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING] + case CellTypeNumeric: + c.numFmt = builtInNumFmt[builtInNumFmtIndex_INT] + case CellTypeBool: + c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] //TEMP + case CellTypeInline: + c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING] + case CellTypeError: + c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] //TEMP + case CellTypeDate: + // Cells that are stored as dates are not properly supported in this library. + // They should instead be stored as a Numeric with a date format. + c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] + case CellTypeStringFormula: + c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING] + } } // GetStyle returns the Style associated with a Col func (c *Col) GetStyle() *Style { - return c.style + return c.style } // SetStyle sets the style of a Col func (c *Col) SetStyle(style *Style) { - c.style = style + c.style = style } func (c *Col) SetOutlineLevel(outlineLevel uint8) { - c.OutlineLevel = &outlineLevel + c.OutlineLevel = &outlineLevel } // copyToRange is an internal convenience function to make a copy of a @@ -90,118 +90,118 @@ func (c *Col) SetOutlineLevel(outlineLevel uint8) { // general purpose Col copying function as you must still insert the // resulting Col into the Col Store. func (c *Col) copyToRange(min, max int) *Col { - return &Col{ - Min: min, - Max: max, - Hidden: c.Hidden, - Width: c.Width, - Collapsed: c.Collapsed, - OutlineLevel: c.OutlineLevel, - BestFit: c.BestFit, - CustomWidth: c.CustomWidth, - Phonetic: c.Phonetic, - numFmt: c.numFmt, - parsedNumFmt: c.parsedNumFmt, - style: c.style, - } + return &Col{ + Min: min, + Max: max, + Hidden: c.Hidden, + Width: c.Width, + Collapsed: c.Collapsed, + OutlineLevel: c.OutlineLevel, + BestFit: c.BestFit, + CustomWidth: c.CustomWidth, + Phonetic: c.Phonetic, + numFmt: c.numFmt, + parsedNumFmt: c.parsedNumFmt, + style: c.style, + } } type ColStoreNode struct { - Col *Col - Prev *ColStoreNode - Next *ColStoreNode + Col *Col + Prev *ColStoreNode + Next *ColStoreNode } func (csn *ColStoreNode) findNodeForColNum(num int) *ColStoreNode { - switch { - case num >= csn.Col.Min && num <= csn.Col.Max: - return csn - - case num < csn.Col.Min: - if csn.Prev == nil { - return nil - } - if csn.Prev.Col.Max < num { - return nil - } - return csn.Prev.findNodeForColNum(num) - - case num > csn.Col.Max: - if csn.Next == nil { - return nil - } - if csn.Next.Col.Min > num { - return nil - } - return csn.Next.findNodeForColNum(num) + switch { + case num >= csn.Col.Min && num <= csn.Col.Max: + return csn + + case num < csn.Col.Min: + if csn.Prev == nil { + return nil } - return nil + if csn.Prev.Col.Max < num { + return nil + } + return csn.Prev.findNodeForColNum(num) + + case num > csn.Col.Max: + if csn.Next == nil { + return nil + } + if csn.Next.Col.Min > num { + return nil + } + return csn.Next.findNodeForColNum(num) + } + return nil } // ColStore is the working store of Col definitions, it will simplify all Cols added to it, to ensure there ar no overlapping definitions. type ColStore struct { - Root *ColStoreNode - Len int + Root *ColStoreNode + Len int } // Add a Col to the ColStore. If it overwrites all, or part of some // existing Col's range of columns the that Col will be adjusted // and/or split to make room for the new Col. func (cs *ColStore) Add(col *Col) *ColStoreNode { - newNode := &ColStoreNode{Col: col} - if cs.Root == nil { - cs.Root = newNode - cs.Len = 1 - return newNode - } - cs.makeWay(cs.Root, newNode) + newNode := &ColStoreNode{Col: col} + if cs.Root == nil { + cs.Root = newNode + cs.Len = 1 return newNode + } + cs.makeWay(cs.Root, newNode) + return newNode } func (cs *ColStore) FindColByIndex(index int) *Col { - csn := cs.findNodeForColNum(index) - if csn != nil { - return csn.Col - } - return nil + csn := cs.findNodeForColNum(index) + if csn != nil { + return csn.Col + } + return nil } func (cs *ColStore) findNodeForColNum(num int) *ColStoreNode { - if cs.Root == nil { - return nil - } - return cs.Root.findNodeForColNum(num) + if cs.Root == nil { + return nil + } + return cs.Root.findNodeForColNum(num) } func (cs *ColStore) removeNode(node *ColStoreNode) { - if node.Prev != nil { - if node.Next != nil { - node.Prev.Next = node.Next - } else { - node.Prev.Next = nil - } - - } + if node.Prev != nil { if node.Next != nil { - if node.Prev != nil { - node.Next.Prev = node.Prev - } else { - node.Next.Prev = nil - } + node.Prev.Next = node.Next + } else { + node.Prev.Next = nil + } + + } + if node.Next != nil { + if node.Prev != nil { + node.Next.Prev = node.Prev + } else { + node.Next.Prev = nil } - if cs.Root == node { - switch { - case node.Prev != nil: - cs.Root = node.Prev - case node.Next != nil: - cs.Root = node.Next - default: - cs.Root = nil - } + } + if cs.Root == node { + switch { + case node.Prev != nil: + cs.Root = node.Prev + case node.Next != nil: + cs.Root = node.Next + default: + cs.Root = nil } - node.Next = nil - node.Prev = nil - cs.Len -= 1 + } + node.Next = nil + node.Prev = nil + cs.Len -= 1 } // makeWay will adjust the Min and Max of this ColStoreNode's Col to @@ -211,193 +211,192 @@ func (cs *ColStore) removeNode(node *ColStoreNode) { // completely within the range of this one, but without reaching its // maximum extent. func (cs *ColStore) makeWay(node1, node2 *ColStoreNode) { - switch { - case node1.Col.Max < node2.Col.Min: - // The node2 starts after node1 ends, there's no overlap - // - // Node1 |----| - // Node2 |----| - if node1.Next != nil { - if node1.Next.Col.Min <= node2.Col.Max { - cs.makeWay(node1.Next, node2) - return - } - cs.addNode(node1, node2, node1.Next) - return - } - cs.addNode(node1, node2, nil) + switch { + case node1.Col.Max < node2.Col.Min: + // The node2 starts after node1 ends, there's no overlap + // + // Node1 |----| + // Node2 |----| + if node1.Next != nil { + if node1.Next.Col.Min <= node2.Col.Max { + cs.makeWay(node1.Next, node2) return + } + cs.addNode(node1, node2, node1.Next) + return + } + cs.addNode(node1, node2, nil) + return - case node1.Col.Min > node2.Col.Max: - // Node2 ends before node1 begins, there's no overlap - // - // Node1 |-----| - // Node2 |----| - if node1.Prev != nil { - if node1.Prev.Col.Max >= node2.Col.Min { - cs.makeWay(node1.Prev, node2) - return - } - cs.addNode(node1.Prev, node2, node1) - return - } - cs.addNode(nil, node2, node1) + case node1.Col.Min > node2.Col.Max: + // Node2 ends before node1 begins, there's no overlap + // + // Node1 |-----| + // Node2 |----| + if node1.Prev != nil { + if node1.Prev.Col.Max >= node2.Col.Min { + cs.makeWay(node1.Prev, node2) return + } + cs.addNode(node1.Prev, node2, node1) + return + } + cs.addNode(nil, node2, node1) + return - case node1.Col.Min == node2.Col.Min && node1.Col.Max == node2.Col.Max: - // Exact match - // - // Node1 |xxx| - // Node2 |---| - - prev := node1.Prev - next := node1.Next - cs.removeNode(node1) - cs.addNode(prev, node2, next) - // Remove node may have set the root to nil - if cs.Root == nil { - cs.Root = node2 - } - return + case node1.Col.Min == node2.Col.Min && node1.Col.Max == node2.Col.Max: + // Exact match + // + // Node1 |xxx| + // Node2 |---| + + prev := node1.Prev + next := node1.Next + cs.removeNode(node1) + cs.addNode(prev, node2, next) + // Remove node may have set the root to nil + if cs.Root == nil { + cs.Root = node2 + } + return - case node1.Col.Min > node2.Col.Min && node1.Col.Max < node2.Col.Max: - // Node2 envelopes node1 - // - // Node1 |xx| - // Node2 |----| - - prev := node1.Prev - next := node1.Next - cs.removeNode(node1) - switch { - case prev == node2: - node2.Next = next - case next == node2: - node2.Prev = prev - default: - cs.addNode(prev, node2, next) - } - - if node2.Prev != nil && node2.Prev.Col.Max >= node2.Col.Min { - cs.makeWay(prev, node2) - } - if node2.Next != nil && node2.Next.Col.Min <= node2.Col.Max { - cs.makeWay(next, node2) - } - - if cs.Root == nil { - cs.Root = node2 - } - - case node1.Col.Min < node2.Col.Min && node1.Col.Max > node2.Col.Max: - // Node2 bisects node1: - // - // Node1 |---xx---| - // Node2 |--| - newCol := node1.Col.copyToRange(node2.Col.Max+1, node1.Col.Max) - newNode := &ColStoreNode{Col: newCol} - cs.addNode(node1, newNode, node1.Next) - node1.Col.Max = node2.Col.Min - 1 - cs.addNode(node1, node2, newNode) - return + case node1.Col.Min > node2.Col.Min && node1.Col.Max < node2.Col.Max: + // Node2 envelopes node1 + // + // Node1 |xx| + // Node2 |----| - case node1.Col.Max >= node2.Col.Min && node1.Col.Min < node2.Col.Min: - // Node2 overlaps node1 at some point above it's minimum: - // - // Node1 |----xx| - // Node2 |-------| - next := node1.Next - node1.Col.Max = node2.Col.Min - 1 - if next == node2 { - return - } - cs.addNode(node1, node2, next) - if next != nil && next.Col.Min <= node2.Col.Max { - cs.makeWay(next, node2) - } - return + prev := node1.Prev + next := node1.Next + cs.removeNode(node1) + switch { + case prev == node2: + node2.Next = next + case next == node2: + node2.Prev = prev + default: + cs.addNode(prev, node2, next) + } - case node1.Col.Min <= node2.Col.Max && node1.Col.Min > node2.Col.Min: - // Node2 overlaps node1 at some point below it's maximum: - // - // Node1: |------| - // Node2: |----xx| - prev := node1.Prev - node1.Col.Min = node2.Col.Max + 1 - if prev == node2 { - return - } - cs.addNode(prev, node2, node1) - if prev != nil && prev.Col.Max >= node2.Col.Min { - cs.makeWay(node1.Prev, node2) - } - return + if node2.Prev != nil && node2.Prev.Col.Max >= node2.Col.Min { + cs.makeWay(prev, node2) + } + if node2.Next != nil && node2.Next.Col.Min <= node2.Col.Max { + cs.makeWay(next, node2) } + + if cs.Root == nil { + cs.Root = node2 + } + + case node1.Col.Min < node2.Col.Min && node1.Col.Max > node2.Col.Max: + // Node2 bisects node1: + // + // Node1 |---xx---| + // Node2 |--| + newCol := node1.Col.copyToRange(node2.Col.Max+1, node1.Col.Max) + newNode := &ColStoreNode{Col: newCol} + cs.addNode(node1, newNode, node1.Next) + node1.Col.Max = node2.Col.Min - 1 + cs.addNode(node1, node2, newNode) return -} -func (cs *ColStore) addNode(prev, this, next *ColStoreNode) { - if prev != nil { - prev.Next = this + case node1.Col.Max >= node2.Col.Min && node1.Col.Min < node2.Col.Min: + // Node2 overlaps node1 at some point above it's minimum: + // + // Node1 |----xx| + // Node2 |-------| + next := node1.Next + node1.Col.Max = node2.Col.Min - 1 + if next == node2 { + return + } + cs.addNode(node1, node2, next) + if next != nil && next.Col.Min <= node2.Col.Max { + cs.makeWay(next, node2) + } + return + + case node1.Col.Min <= node2.Col.Max && node1.Col.Min > node2.Col.Min: + // Node2 overlaps node1 at some point below it's maximum: + // + // Node1: |------| + // Node2: |----xx| + prev := node1.Prev + node1.Col.Min = node2.Col.Max + 1 + if prev == node2 { + return } - this.Prev = prev - this.Next = next - if next != nil { - next.Prev = this + cs.addNode(prev, node2, node1) + if prev != nil && prev.Col.Max >= node2.Col.Min { + cs.makeWay(node1.Prev, node2) } - cs.Len += 1 + return + } +} + +func (cs *ColStore) addNode(prev, this, next *ColStoreNode) { + if prev != nil { + prev.Next = this + } + this.Prev = prev + this.Next = next + if next != nil { + next.Prev = this + } + cs.Len += 1 } func (cs *ColStore) getOrMakeColsForRange(start *ColStoreNode, min, max int) []*Col { - cols := []*Col{} - var csn *ColStoreNode - var newCol *Col - switch { - case start == nil: - newCol = NewColForRange(min, max) - csn = cs.Add(newCol) - case start.Col.Min <= min && start.Col.Max >= min: - csn = start - case start.Col.Min < min && start.Col.Max < min: - if start.Next != nil { - return cs.getOrMakeColsForRange(start.Next, min, max) - } - newCol = NewColForRange(min, max) - csn = cs.Add(newCol) - case start.Col.Min > min: - if start.Col.Min > max { - newCol = NewColForRange(min, max) - } else { - newCol = NewColForRange(min, start.Col.Min-1) - } - csn = cs.Add(newCol) + cols := []*Col{} + var csn *ColStoreNode + var newCol *Col + switch { + case start == nil: + newCol = NewColForRange(min, max) + csn = cs.Add(newCol) + case start.Col.Min <= min && start.Col.Max >= min: + csn = start + case start.Col.Min < min && start.Col.Max < min: + if start.Next != nil { + return cs.getOrMakeColsForRange(start.Next, min, max) } - - cols = append(cols, csn.Col) - if csn.Col.Max >= max { - return cols + newCol = NewColForRange(min, max) + csn = cs.Add(newCol) + case start.Col.Min > min: + if start.Col.Min > max { + newCol = NewColForRange(min, max) + } else { + newCol = NewColForRange(min, start.Col.Min-1) } - cols = append(cols, cs.getOrMakeColsForRange(csn.Next, csn.Col.Max+1, max)...) + csn = cs.Add(newCol) + } + + cols = append(cols, csn.Col) + if csn.Col.Max >= max { return cols + } + cols = append(cols, cs.getOrMakeColsForRange(csn.Next, csn.Col.Max+1, max)...) + return cols } func chainOp(csn *ColStoreNode, fn func(idx int, col *Col)) { - for csn.Prev != nil { - csn = csn.Prev - } - - var i int - for i = 0; csn.Next != nil; i++ { - fn(i, csn.Col) - csn = csn.Next - } - fn(i+1, csn.Col) + for csn.Prev != nil { + csn = csn.Prev + } + + var i int + for i = 0; csn.Next != nil; i++ { + fn(i, csn.Col) + csn = csn.Next + } + fn(i+1, csn.Col) } // ForEach calls the function fn for each Col defined in the ColStore. func (cs *ColStore) ForEach(fn func(idx int, col *Col)) { - if cs.Root == nil { - return - } - chainOp(cs.Root, fn) + if cs.Root == nil { + return + } + chainOp(cs.Root, fn) } diff --git a/data_validation.go b/data_validation.go index 7b426726..bb17df87 100644 --- a/data_validation.go +++ b/data_validation.go @@ -1,6 +1,7 @@ package xlsx import ( + "errors" "fmt" "strings" "unicode/utf8" @@ -109,7 +110,7 @@ func (dd *xlsxDataValidation) SetInput(title, msg *string) { func (dd *xlsxDataValidation) SetDropList(keys []string) error { formula := "\"" + strings.Join(keys, ",") + "\"" if dataValidationFormulaStrLen < utf8.RuneCountInString(formula) { - return fmt.Errorf(dataValidationFormulaStrLenErr) + return errors.New(dataValidationFormulaStrLenErr) } dd.Formula1 = formula dd.Type = convDataValidationType(dataValidationTypeList) diff --git a/diskv.go b/diskv.go index d77c9922..bd219014 100644 --- a/diskv.go +++ b/diskv.go @@ -333,7 +333,6 @@ func (dvr *DiskVRow) CellCount() int { type DiskVCellStore struct { baseDir string buf *bytes.Buffer - reader *bytes.Reader store *diskv.Diskv } @@ -402,7 +401,7 @@ func (cs *DiskVCellStore) MoveRow(r *Row, index int) error { r.num = index newKey := r.key() if cs.store.Has(newKey) { - return fmt.Errorf("Target index for row (%d) would overwrite a row already exists", index) + return fmt.Errorf("target index for row (%d) would overwrite a row already exists", index) } err := cs.store.Erase(oldKey) if err != nil { @@ -508,7 +507,7 @@ func readUnitSeparator(reader *bytes.Reader) error { return err } if us != US { - return errors.New("Invalid format in cellstore, no unit separator found") + return errors.New("invalid format in cellstore, no unit separator found") } return nil } @@ -521,17 +520,6 @@ func writeGroupSeparator(buf *bytes.Buffer) error { return buf.WriteByte(GS) } -func readGroupSeparator(reader *bytes.Reader) error { - gs, err := reader.ReadByte() - if err != nil { - return err - } - if gs != GS { - return errors.New("Invalid format in cellstore, no group separator found") - } - return nil -} - func readBool(reader *bytes.Reader) (bool, error) { b, err := reader.ReadByte() if err != nil { @@ -656,7 +644,7 @@ func readEndOfRecord(reader *bytes.Reader) error { return err } if b != RS { - return errors.New("Expected end of record, but not found") + return errors.New("expected end of record, but not found") } return nil } @@ -1238,7 +1226,7 @@ func readCell(reader *bytes.Reader) (*Cell, error) { func (cs *DiskVCellStore) WriteRow(r *Row) error { dvr, ok := r.cellStoreRow.(*DiskVRow) if !ok { - return fmt.Errorf("cellStoreRow for a DiskVCellStore is not DiskVRow (%T)!", r.cellStoreRow) + return fmt.Errorf("cellStoreRow for a DiskVCellStore is not DiskVRow (%T)", r.cellStoreRow) } if dvr.currentCell != nil { err := dvr.writeCell(dvr.currentCell) @@ -1255,10 +1243,6 @@ func (cs *DiskVCellStore) WriteRow(r *Row) error { return cs.store.WriteStream(key, cs.buf, true) } -func cellTransform(s string) []string { - return strings.Split(s, ":") -} - func writeRichTextColor(buf *bytes.Buffer, c *RichTextColor) error { var err error var hasIndexed bool @@ -1354,9 +1338,7 @@ func readRichTextColor(reader *bytes.Reader) (*RichTextColor, error) { func writeRichTextFont(buf *bytes.Buffer, f *RichTextFont) error { var err error - var hasColor bool - - hasColor = f.Color != nil + var hasColor bool = f.Color != nil if err = writeString(buf, f.Name); err != nil { return err @@ -1460,9 +1442,7 @@ func readRichTextFont(reader *bytes.Reader) (*RichTextFont, error) { func writeRichTextRun(buf *bytes.Buffer, r *RichTextRun) error { var err error - var hasFont bool - - hasFont = r.Font != nil + var hasFont bool = r.Font != nil if err = writeBool(buf, hasFont); err != nil { return err @@ -1510,9 +1490,7 @@ func readRichTextRun(reader *bytes.Reader) (*RichTextRun, error) { func writeRichText(buf *bytes.Buffer, rt []RichTextRun) error { var err error - var length int - - length = len(rt) + var length int = len(rt) if err = writeInt(buf, length); err != nil { return err diff --git a/diskv_test.go b/diskv_test.go index 54e99f71..88c28388 100644 --- a/diskv_test.go +++ b/diskv_test.go @@ -176,7 +176,7 @@ func TestDiskVCellStore(t *testing.T) { v, err = readBool(reader) c.Assert(err, qt.IsNil) c.Assert(v, qt.Equals, false) - v, err = readBool(reader) + _, err = readBool(reader) c.Assert(err, qt.Not(qt.IsNil)) }) @@ -211,7 +211,7 @@ line!`) v, err = readString(reader) c.Assert(err, qt.IsNil) c.Assert(v, qt.Equals, "Scheiß encoding") - v, err = readString(reader) + _, err = readString(reader) c.Assert(err, qt.Not(qt.IsNil)) }) @@ -230,7 +230,7 @@ line!`) v, err = readInt(reader) c.Assert(err, qt.IsNil) c.Assert(v, qt.Equals, math.MaxInt64) - v, err = readInt(reader) + _, err = readInt(reader) c.Assert(err, qt.Not(qt.IsNil)) }) @@ -252,7 +252,7 @@ line!`) v, err = readStringPointer(reader) c.Assert(err, qt.IsNil) c.Assert(*v, qt.Equals, "bar") - v, err = readStringPointer(reader) + _, err = readStringPointer(reader) c.Assert(err, qt.Not(qt.IsNil)) }) diff --git a/file.go b/file.go index 1d1e3625..b09449ab 100644 --- a/file.go +++ b/file.go @@ -200,7 +200,7 @@ func (f *File) AddSheet(sheetName string) (*Sheet, error) { func (f *File) AddSheetWithCellStore(sheetName string, constructor CellStoreConstructor) (*Sheet, error) { var err error if _, exists := f.Sheet[sheetName]; exists { - return nil, fmt.Errorf("duplicate sheet name '%s'.", sheetName) + return nil, fmt.Errorf("duplicate sheet name '%s'", sheetName) } if err := IsSaneSheetName(sheetName); err != nil { @@ -226,7 +226,7 @@ func (f *File) AddSheetWithCellStore(sheetName string, constructor CellStoreCons // Appends an existing Sheet, with the provided name, to a File func (f *File) AppendSheet(sheet Sheet, sheetName string) (*Sheet, error) { if _, exists := f.Sheet[sheetName]; exists { - return nil, fmt.Errorf("duplicate sheet name '%s'.", sheetName) + return nil, fmt.Errorf("duplicate sheet name '%s'", sheetName) } if err := IsSaneSheetName(sheetName); err != nil { return nil, fmt.Errorf("sheet name is not valid: %w", err) @@ -359,7 +359,7 @@ func (f *File) MakeStreamParts() (map[string]string, error) { } f.styles.reset() if len(f.Sheets) == 0 { - err := errors.New("Workbook must contains atleast one worksheet") + err := errors.New("workbook must contains at least one worksheet") return nil, err } for _, sheet := range f.Sheets { diff --git a/file_test.go b/file_test.go index 8b1f3963..ade72bc7 100644 --- a/file_test.go +++ b/file_test.go @@ -302,9 +302,7 @@ func TestFile(t *testing.T) { // Test we can create a File object from scratch csRunO(c, "TestCreateFile", func(c *qt.C, option FileOption) { - var xlsxFile *File - - xlsxFile = NewFile(option) + xlsxFile := NewFile(option) c.Assert(xlsxFile, qt.Not(qt.IsNil)) }) @@ -337,9 +335,7 @@ func TestFile(t *testing.T) { // Test that we can add a sheet to a File csRunO(c, "TestAddSheet", func(c *qt.C, option FileOption) { - var f *File - - f = NewFile(option) + f := NewFile(option) sheet, err := f.AddSheet("MySheet") c.Assert(err, qt.IsNil) c.Assert(sheet, qt.Not(qt.IsNil)) @@ -353,7 +349,7 @@ func TestFile(t *testing.T) { _, err := f.AddSheet("MySheet") c.Assert(err, qt.IsNil) _, err = f.AddSheet("MySheet") - c.Assert(err.Error(), qt.Equals, "duplicate sheet name 'MySheet'.") + c.Assert(err.Error(), qt.Equals, "duplicate sheet name 'MySheet'") }) // Test that AddSheet returns an error if you try to add sheet with name as empty string @@ -365,9 +361,7 @@ func TestFile(t *testing.T) { // Test that we can append a sheet to a File csRunO(c, "TestAppendSheet", func(c *qt.C, option FileOption) { - var f *File - - f = NewFile(option) + f := NewFile(option) s := Sheet{} sheet, err := f.AppendSheet(s, "MySheet") c.Assert(err, qt.IsNil) @@ -383,7 +377,7 @@ func TestFile(t *testing.T) { _, err := f.AppendSheet(s, "MySheet") c.Assert(err, qt.IsNil) _, err = f.AppendSheet(s, "MySheet") - c.Assert(err.Error(), qt.Equals, "duplicate sheet name 'MySheet'.") + c.Assert(err.Error(), qt.Equals, "duplicate sheet name 'MySheet'") }) // Test that AppendSheet doesn't lose rows because of a change in the sheet name (this really occurred see https://github.com/tealeg/xlsx/issues/783 ) @@ -432,9 +426,7 @@ func TestFile(t *testing.T) { // Test that we can get the Nth sheet csRunO(c, "TestNthSheet", func(c *qt.C, option FileOption) { - var f *File - - f = NewFile(option) + f := NewFile(option) sheet, _ := f.AddSheet("MySheet") sheetByIndex := f.Sheets[0] sheetByName := f.Sheet["MySheet"] @@ -454,9 +446,7 @@ func TestFile(t *testing.T) { // Test that we can create a Workbook and marshal it to XML. csRunO(c, "TestMarshalWorkbook", func(c *qt.C, option FileOption) { - var f *File - - f = NewFile(option) + f := NewFile(option) f.AddSheet("MyFirstSheet") f.AddSheet("MySecondSheet") @@ -484,8 +474,7 @@ func TestFile(t *testing.T) { // Test that we can marshall a File to a collection of xml files csRunO(c, "TestMarshalFile", func(c *qt.C, option FileOption) { - var f *File - f = NewFile(option) + f := NewFile(option) sheet1, _ := f.AddSheet("MySheet") row1 := sheet1.AddRow() cell1 := row1.AddCell() @@ -888,8 +877,7 @@ func TestFile(t *testing.T) { tmpPath, err := ioutil.TempDir("", "testsavefile") c.Assert(err, qt.IsNil) defer os.RemoveAll(tmpPath) - var f *File - f = NewFile(option) + f := NewFile(option) sheet1, _ := f.AddSheet("MySheet") row1 := sheet1.AddRow() cell1 := row1.AddCell() @@ -920,8 +908,7 @@ func TestFile(t *testing.T) { }) csRunO(c, "TestMarshalFileWithHyperlinks", func(c *qt.C, option FileOption) { - var f *File - f = NewFile(option) + f := NewFile(option) sheet1, _ := f.AddSheet("MySheet") row1 := sheet1.AddRow() cell1 := row1.AddCell() @@ -940,8 +927,7 @@ func TestFile(t *testing.T) { }) csRunO(c, "TestMarshalFileWithHiddenSheet", func(c *qt.C, option FileOption) { - var f *File - f = NewFile(option) + f := NewFile(option) sheet1, _ := f.AddSheet("MySheet") row1 := sheet1.AddRow() cell1 := row1.AddCell() @@ -952,7 +938,7 @@ func TestFile(t *testing.T) { cell2 := row2.AddCell() cell2.SetString("A cell!") - path := filepath.Join(c.Mkdir(), "test.xlsx") + path := filepath.Join(t.TempDir(), "test.xlsx") err := f.Save(path) c.Assert(err, qt.IsNil) @@ -965,8 +951,7 @@ func TestFile(t *testing.T) { }) csRunO(c, "TestMarshalFileWithAutoFilter", func(c *qt.C, option FileOption) { - var f *File - f = NewFile(option) + f := NewFile(option) sheet1, _ := f.AddSheet("MySheet") sheet1.AutoFilter = &AutoFilter{ TopLeftCell: "A1", @@ -988,8 +973,7 @@ func TestFile(t *testing.T) { tmpPath, err := ioutil.TempDir("", "testsavefilewithhyperlinks") c.Assert(err, qt.IsNil) defer os.RemoveAll(tmpPath) - var f *File - f = NewFile(option) + f := NewFile(option) sheet1, _ := f.AddSheet("MySheet") row1 := sheet1.AddRow() cell1 := row1.AddCell() diff --git a/format_code.go b/format_code.go index 457dbcbb..3b605f65 100644 --- a/format_code.go +++ b/format_code.go @@ -374,8 +374,8 @@ func parseNumberFormatSection(fullFormat string) (*formatOptions, error) { } if isTimeFormat(reducedFormat) { return &formatOptions{ - fullFormatString: fullFormat, - isTimeFormat: true, + fullFormatString: fullFormat, + isTimeFormat: true, reducedFormatString: reducedFormat, }, nil } @@ -599,7 +599,7 @@ func skipToRune(runes []rune, r rune) (int, error) { return i, nil } } - return -1, fmt.Errorf("No closing quote found") + return -1, fmt.Errorf("no closing quote found") } // isTimeFormat checks whether an Excel format string represents a time.Time. diff --git a/lib.go b/lib.go index 6217f11e..92cec419 100644 --- a/lib.go +++ b/lib.go @@ -24,12 +24,6 @@ const ( ) var ( - tokPool = sync.Pool{ - New: func() interface{} { - return &xml.StartElement{} - }, - } - xlsxSIPool = sync.Pool{ New: func() interface{} { return &xlsxSI{} @@ -58,24 +52,25 @@ func (e *XLSXReaderError) Error() string { // getRangeFromString is an internal helper function that converts // XLSX internal range syntax to a pair of integers. For example, // the range string "1:3" yield the upper and lower integers 1 and 3. -func getRangeFromString(rangeString string) (lower int, upper int, error error) { - var parts []string - parts = strings.SplitN(rangeString, cellRangeChar, 2) +func getRangeFromString(rangeString string) (int, int, error) { + var lower, upper int + var err error + parts := strings.SplitN(rangeString, cellRangeChar, 2) if parts[0] == "" { - error = fmt.Errorf("Invalid range '%s'\n", rangeString) + return 0, 0, fmt.Errorf("invalid range '%s'", rangeString) } if parts[1] == "" { - error = fmt.Errorf("Invalid range '%s'\n", rangeString) + return 0, 0, fmt.Errorf("invalid range '%s'", rangeString) } - lower, error = strconv.Atoi(parts[0]) - if error != nil { - error = fmt.Errorf("Invalid range (not integer in lower bound) %s\n", rangeString) + lower, err = strconv.Atoi(parts[0]) + if err != nil { + return 0, 0, fmt.Errorf("invalid range (not integer in lower bound) %s", rangeString) } - upper, error = strconv.Atoi(parts[1]) - if error != nil { - error = fmt.Errorf("Invalid range (not integer in upper bound) %s\n", rangeString) + upper, err = strconv.Atoi(parts[1]) + if err != nil { + return 0, 0, fmt.Errorf("invalid range (not integer in upper bound) %s", rangeString) } - return lower, upper, error + return lower, upper, err } // ColLettersToIndex is used to convert a character based column @@ -289,12 +284,6 @@ func makeRowFromRaw(rawrow xlsxRow, sheet *Sheet) *Row { return row } -func makeEmptyRow(sheet *Sheet) *Row { - row := new(Row) - row.Sheet = sheet - return row -} - type sharedFormula struct { x, y int formula string @@ -623,7 +612,7 @@ type indexedSheet struct { } func readSheetViews(xSheetViews xlsxSheetViews) []SheetView { - if xSheetViews.SheetView == nil || len(xSheetViews.SheetView) == 0 { + if len(xSheetViews.SheetView) == 0 { return nil } sheetViews := []SheetView{} @@ -723,7 +712,7 @@ func makeHyperlinkTable(worksheet *xlsxWorksheet, fi *File, rsheet *xlsxSheet) ( func readSheetFromFile(rsheet xlsxSheet, fi *File, sheetXMLMap map[string]string, rowLimit, colLimit int, valueOnly bool) (sheet *Sheet, errRes error) { defer func() { if x := recover(); x != nil { - errRes = fmt.Errorf("%v\n%s\n", x, debug.Stack()) + errRes = fmt.Errorf("%v\n%s", x, debug.Stack()) } }() @@ -847,21 +836,22 @@ func readSheetsFromZipFile(f *zip.File, file *File, sheetXMLMap map[string]strin sb.WriteString("{SheetIndex: ") sb.WriteString(strconv.Itoa(j)) sb.WriteString("} No sheet returned from readSheetFromFile\n") + } else { + if sheet.Error != nil { + errFound = true + sb.WriteString("{SheetIndex: ") + sb.WriteString(strconv.Itoa(sheet.Index)) + sb.WriteString("} ") + sb.WriteString(sheet.Error.Error()) + } + sheetName := sheet.Sheet.Name + sheetsByName[sheetName] = sheet.Sheet + sheets[sheet.Index] = sheet.Sheet } - if sheet.Error != nil { - errFound = true - sb.WriteString("{SheetIndex: ") - sb.WriteString(strconv.Itoa(sheet.Index)) - sb.WriteString("} ") - sb.WriteString(sheet.Error.Error()) - } - sheetName := sheet.Sheet.Name - sheetsByName[sheetName] = sheet.Sheet - sheets[sheet.Index] = sheet.Sheet } close(sheetChan) if errFound { - err = fmt.Errorf(sb.String()) + err = errors.New(sb.String()) } return sheetsByName, sheets, err } @@ -884,7 +874,6 @@ func readSharedStrings(rc io.Reader) (*RefTable, error) { decoder = xml.NewDecoder(rc) for { - tok = tokPool.Get().(xml.Token) tok, err = decoder.Token() if tok == nil { break @@ -948,7 +937,6 @@ func readSharedStrings(rc io.Reader) (*RefTable, error) { default: // Do nothing } - tokPool.Put(tok) } if reftable == nil { @@ -1184,14 +1172,14 @@ func ReadZipReader(r *zip.Reader, options ...FileOption) (*File, error) { } } if workbookRels == nil { - return wrap(fmt.Errorf("workbook.xml.rels not found in input xlsx.")) + return wrap(fmt.Errorf("workbook.xml.rels not found in input xlsx")) } sheetXMLMap, err = readWorkbookRelationsFromZipFile(workbookRels) if err != nil { return wrap(err) } if len(worksheets) == 0 { - return wrap(fmt.Errorf("Input xlsx contains no worksheets.")) + return wrap(fmt.Errorf("input XLSX contains no worksheets")) } file.worksheets = worksheets file.worksheetRels = worksheetRels diff --git a/lib_test.go b/lib_test.go index ca40d951..5b0a67df 100644 --- a/lib_test.go +++ b/lib_test.go @@ -32,14 +32,14 @@ func TestLib(t *testing.T) { csRunO(c, "ReadZipReaderWithFileWithNoWorkbookRels", func(c *qt.C, option FileOption) { _, err := OpenFile("./testdocs/badfile_noWorkbookRels.xlsx", option) c.Assert(err, qt.Not(qt.IsNil)) - c.Assert(err.Error(), qt.Equals, "OpenFile: ReadZip: ReadZipReader: workbook.xml.rels not found in input xlsx.") + c.Assert(err.Error(), qt.Equals, "OpenFile: ReadZip: ReadZipReader: workbook.xml.rels not found in input xlsx") }) // Attempting to open a file with no worksheets returns an error. csRunO(c, "ReadZipReaderWithFileWithNoWorksheets", func(c *qt.C, option FileOption) { _, err := OpenFile("./testdocs/badfile_noWorksheets.xlsx", option) c.Assert(err, qt.Not(qt.IsNil)) - c.Assert(err.Error(), qt.Equals, "OpenFile: ReadZip: ReadZipReader: Input xlsx contains no worksheets.") + c.Assert(err.Error(), qt.Equals, "OpenFile: ReadZip: ReadZipReader: input XLSX contains no worksheets") }) // Read a file containing hyperlinks in cells diff --git a/memory.go b/memory.go index aab8d924..f287829c 100644 --- a/memory.go +++ b/memory.go @@ -2,7 +2,6 @@ package xlsx import ( "fmt" - "strings" ) type MemoryRow struct { @@ -183,7 +182,7 @@ func (mcs *MemoryCellStore) MoveRow(r *Row, index int) error { r.num = index newKey := r.key() if _, exists := mcs.rows[newKey]; exists { - return fmt.Errorf("Target index for row (%d) would overwrite a row already exists", index) + return fmt.Errorf("target index for row (%d) would overwrite a row already exists", index) } mcs.rows[newKey] = r delete(mcs.rows, oldKey) @@ -213,9 +212,3 @@ func (mcs *MemoryCellStore) MakeRowWithLen(sheet *Sheet, len int) *Row { func (mcs *MemoryCellStore) MakeRow(sheet *Sheet) *Row { return makeMemoryRow(sheet).row } - -// Extract the row key from a provided cell key -func keyToRowKey(key string) string { - parts := strings.Split(key, ":") - return parts[0] + ":" + parts[1] -} diff --git a/read_test.go b/read_test.go index 84867e02..a456ecdd 100644 --- a/read_test.go +++ b/read_test.go @@ -10,8 +10,8 @@ import ( ) var ( - errorNoPair = errors.New("Integer to be unmarshaled is not a pair") - errorNotEnoughCells = errors.New("Row has not enough cells") + errorNoPair = errors.New("integer to be unmarshaled is not a pair") + errorNotEnoughCells = errors.New("row has not enough cells") ) type pairUnmarshaler int diff --git a/row_test.go b/row_test.go index ce151bc8..2caaf287 100644 --- a/row_test.go +++ b/row_test.go @@ -10,8 +10,7 @@ func TestRow(t *testing.T) { c := qt.New(t) // Test we can add a new Cell to a Row csRunO(c, "TestAddCell", func(c *qt.C, option FileOption) { - var f *File - f = NewFile(option) + f := NewFile(option) sheet, _ := f.AddSheet("MySheet") row := sheet.AddRow() cell := row.AddCell() @@ -21,8 +20,7 @@ func TestRow(t *testing.T) { }) csRunO(c, "TestGetCell", func(c *qt.C, option FileOption) { - var f *File - f = NewFile(option) + f := NewFile(option) sheet, _ := f.AddSheet("MySheet") row := sheet.AddRow() cell := row.AddCell() @@ -89,8 +87,7 @@ func TestRow(t *testing.T) { }) csRunO(c, "Test Set Height", func(c *qt.C, option FileOption) { - var f *File - f = NewFile(option) + f := NewFile(option) sheet, _ := f.AddSheet("MySheet") row := sheet.AddRow() c.Assert(row.height, qt.Equals, 0.0) diff --git a/sheet.go b/sheet.go index c2be8dd2..91be4e13 100644 --- a/sheet.go +++ b/sheet.go @@ -260,7 +260,7 @@ func (s *Sheet) AddDataValidation(dv *xlsxDataValidation) { func (s *Sheet) RemoveRowAtIndex(index int) error { s.mustBeOpen() if index < 0 || index >= s.MaxRow { - return fmt.Errorf("Cannot remove row: index out of range: %d", index) + return fmt.Errorf("cannot remove row: index out of range: %d", index) } if s.currentRow != nil { s.setCurrentRow(nil) @@ -413,7 +413,6 @@ func (s *Sheet) setCol(min, max int, setter func(col *Col)) { } } - return } // Set the width of a range of columns. diff --git a/sheet_test.go b/sheet_test.go index 6eb37c52..a3485982 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -18,8 +18,7 @@ func TestSheet(t *testing.T) { csRunO(c, "TestAddAndRemoveRow", func(c *qt.C, option FileOption) { option = UseDiskVCellStore setUp := func() (*Sheet, error) { - var f *File - f = NewFile(option) + f := NewFile(option) sheet, err := f.AddSheet("MySheet") if err != nil { return nil, err @@ -164,8 +163,7 @@ func TestSheet(t *testing.T) { // Test we can get row by index from Sheet csRunO(c, "TestGetRowByIndex", func(c *qt.C, option FileOption) { - var f *File - f = NewFile() + f := NewFile() sheet, _ := f.AddSheet("MySheet") row, err := sheet.Row(10) c.Assert(err, qt.Equals, nil) @@ -228,6 +226,7 @@ func TestSheet(t *testing.T) { var xSheet xlsxWorksheet err = xml.Unmarshal(output.Bytes(), &xSheet) + c.Assert(err, qt.IsNil) c.Assert(xSheet.Dimension.Ref, qt.Equals, "A1") c.Assert(len(xSheet.SheetData.Row), qt.Equals, 1) xRow := xSheet.SheetData.Row[0] @@ -265,6 +264,7 @@ func TestSheet(t *testing.T) { var result xlsxWorksheet err = xml.Unmarshal(output.Bytes(), &result) + c.Assert(err, qt.IsNil) c.Assert(result.Cols, qt.IsNil) }) @@ -408,7 +408,7 @@ func TestSheet(t *testing.T) { style.ApplyAlignment = true cell.SetStyle(style) - dir := c.Mkdir() + dir := t.TempDir() path := filepath.Join(dir, "test.xlsx") err := file.Save(path) c.Assert(err, qt.IsNil) diff --git a/utility.go b/utility.go index f5ab5f3f..11c9b0bf 100644 --- a/utility.go +++ b/utility.go @@ -5,10 +5,6 @@ func sPtr(s string) *string { return &s } -func iPtr(i int) *int { - return &i -} - func fPtr(f float64) *float64 { return &f } diff --git a/write_test.go b/write_test.go index 0eb7d204..61bf0b54 100644 --- a/write_test.go +++ b/write_test.go @@ -15,8 +15,8 @@ type testStringerImpl struct { Value string } -func (this testStringerImpl) String() string { - return this.Value +func (tsi testStringerImpl) String() string { + return tsi.Value } func TestWrite(t *testing.T) { @@ -24,8 +24,7 @@ func TestWrite(t *testing.T) { // Test if we can write a struct to a row csRunO(c, "TestWriteStruct", func(c *qt.C, option FileOption) { - var f *File - f = NewFile(option) + f := NewFile(option) sheet, _ := f.AddSheet("Test1") row := sheet.AddRow() type e struct { @@ -132,8 +131,7 @@ func TestWrite(t *testing.T) { // Test if we can write a slice to a row csRunO(c, "TestWriteSlice", func(c *qt.C, option FileOption) { - var f *File - f = NewFile(option) + f := NewFile(option) sheet, _ := f.AddSheet("Test1") type strA []string @@ -290,7 +288,7 @@ func TestBigWrite(t *testing.T) { c := qt.New(t) p := profile.Start(profile.MemProfile) - testDir := c.Mkdir() + testDir := t.TempDir() path := filepath.Join(testDir, "test.xlsx") @@ -332,7 +330,7 @@ func TestWriteFileWithUnvisitedSheets(t *testing.T) { c.Assert(err, qt.IsNil) cell.SetInt64(39491) - testDir := c.Mkdir() + testDir := t.TempDir() path := filepath.Join(testDir, "test.xlsx") // With issue 644 this line would panic diff --git a/xmlStyle.go b/xmlStyle.go index 671d51b0..6b2e5e23 100644 --- a/xmlStyle.go +++ b/xmlStyle.go @@ -59,19 +59,6 @@ var builtInNumFmt = map[int]string{ 49: "@", } -// These are the color annotations from number format codes that contain color names. -// Also possible are [color1] through [color56] -var numFmtColorCodes = []string{ - "[red]", - "[black]", - "[green]", - "[white]", - "[blue]", - "[magenta]", - "[yellow]", - "[cyan]", -} - var builtInNumFmtInv = make(map[string]int, 40) func init() { @@ -428,22 +415,6 @@ func (styles *xlsxStyleSheet) addBorder(xBorder xlsxBorder) (index int) { return } -func (styles *xlsxStyleSheet) addCellStyleXf(xCellStyleXf xlsxXf) (index int) { - var cellStyleXf xlsxXf - if styles.CellStyleXfs == nil { - styles.CellStyleXfs = &xlsxCellStyleXfs{Count: 0} - } - for index, cellStyleXf = range styles.CellStyleXfs.Xf { - if cellStyleXf.Equals(xCellStyleXf) { - return index - } - } - styles.CellStyleXfs.Xf = append(styles.CellStyleXfs.Xf, xCellStyleXf) - index = styles.CellStyleXfs.Count - styles.CellStyleXfs.Count++ - return -} - func (styles *xlsxStyleSheet) addCellXf(xCellXf xlsxXf) (index int) { var cellXf xlsxXf for index, cellXf = range styles.CellXfs.Xf { diff --git a/xmlWorkbook.go b/xmlWorkbook.go index 6ad59683..2949f8b9 100644 --- a/xmlWorkbook.go +++ b/xmlWorkbook.go @@ -193,7 +193,7 @@ func getWorksheetFromSheet(sheet xlsxSheet, worksheets map[string]*zip.File, she f := worksheetFileForSheet(sheet, worksheets, sheetXMLMap) if f == nil { - return wrap(fmt.Errorf("Unable to find sheet '%s'", sheet)) + return wrap(fmt.Errorf("unable to find sheet '%s'", sheet)) } if rc, err := f.Open(); err != nil { return wrap(fmt.Errorf("file.Open: %w", err)) diff --git a/xmlWorkbook_test.go b/xmlWorkbook_test.go index c51565a5..defe5fc9 100644 --- a/xmlWorkbook_test.go +++ b/xmlWorkbook_test.go @@ -51,8 +51,7 @@ func TestUnmarshallWorkbookXML(t *testing.T) { `) - var workbook *xlsxWorkbook - workbook = new(xlsxWorkbook) + workbook := new(xlsxWorkbook) err := xml.NewDecoder(buf).Decode(workbook) c.Assert(err, qt.IsNil) c.Assert(workbook.FileVersion.AppName, qt.Equals, "xl") @@ -86,8 +85,7 @@ func TestUnmarshallWorkbookXML(t *testing.T) { // Test we can marshall a Workbook to xml func TestMarshallWorkbook(t *testing.T) { c := qt.New(t) - var workbook *xlsxWorkbook - workbook = new(xlsxWorkbook) + workbook := new(xlsxWorkbook) workbook.FileVersion = xlsxFileVersion{} workbook.FileVersion.AppName = "xlsx" workbook.WorkbookPr = xlsxWorkbookPr{BackupFile: false} diff --git a/xmlWorksheet.go b/xmlWorksheet.go index f0bfac58..29ad9d72 100644 --- a/xmlWorksheet.go +++ b/xmlWorksheet.go @@ -499,7 +499,7 @@ func makeXMLAttr(fv reflect.Value, parentName, name string) (xmlwriter.Attr, err case reflect.String: attr.Value = fv.String() default: - return attr, fmt.Errorf("Not yet handled %s.%s (%s)", parentName, name, fv.Kind()) + return attr, fmt.Errorf("not yet handled %s.%s (%s)", parentName, name, fv.Kind()) } @@ -636,7 +636,7 @@ func emitStructAsXML(v reflect.Value, name, xmlNS string) (xmlwriter.Elem, error elem.Content = append(elem.Content, xmlwriter.Text(fv.String())) output.Content = append(output.Content, elem) default: - return output, fmt.Errorf("Todo with unhandled kind %s : %s", fv.Kind(), name) + return output, fmt.Errorf("todo with unhandled kind %s : %s", fv.Kind(), name) } } }