Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ func (opts *Chart) parseTitle() error {
// Set chart offset, scale, aspect ratio setting and print settings by 'Format',
// same as function 'AddPicture'.
//
// Set the position of the chart plot area by 'PlotArea' field with
// Set the properties of the chart plot area by 'PlotArea' field with
// 'ChartPlotArea' data type. The properties that can be set are:
//
// SecondPlotValues
Expand Down
5 changes: 3 additions & 2 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,16 @@ func (f *File) workSheetReader(sheet string) (ws *xlsxWorksheet, err error) {
}
}
ws = new(xlsxWorksheet)
data := f.readBytes(name)
if attrs, ok := f.xmlAttr.Load(name); !ok {
d := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readBytes(name))))
d := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(data)))
if attrs == nil {
attrs = []xml.Attr{}
}
attrs = append(attrs.([]xml.Attr), getRootElement(d)...)
f.xmlAttr.Store(name, attrs)
}
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readBytes(name)))).
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(data))).
Decode(ws); err != nil && err != io.EOF {
return
}
Expand Down
63 changes: 39 additions & 24 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"io"
"math"
"math/big"
"os"
"reflect"
"regexp"
Expand Down Expand Up @@ -225,6 +224,28 @@ func ColumnNameToNumber(name string) (int, error) {
return col, nil
}

// columnNames is a precomputed lookup table of column names for all valid
// column numbers (1 to MaxColumns). This eliminates per-call allocations
// in ColumnNumberToName.
var columnNames = func() []string {
names := make([]string, MaxColumns+1)
for i := 1; i <= MaxColumns; i++ {
num := i
l := 0
for n := num; n > 0; n = (n - 1) / 26 {
l++
}
buf := make([]byte, l)
for num > 0 {
l--
buf[l] = byte((num-1)%26 + 'A')
num = (num - 1) / 26
}
names[i] = string(buf)
}
return names
}()

// ColumnNumberToName provides a function to convert the integer to Excel
// sheet column title.
//
Expand All @@ -235,18 +256,7 @@ func ColumnNumberToName(num int) (string, error) {
if num < MinColumns || num > MaxColumns {
return "", ErrColumnNumber
}
estimatedLength := 0
for n := num; n > 0; n = (n - 1) / 26 {
estimatedLength++
}

result := make([]byte, estimatedLength)
for num > 0 {
estimatedLength--
result[estimatedLength] = byte((num-1)%26 + 'A')
num = (num - 1) / 26
}
return string(result), nil
return columnNames[num], nil
}

// CellNameToCoordinates converts alphanumeric cell name to [X, Y] coordinates
Expand Down Expand Up @@ -282,14 +292,16 @@ func CoordinatesToCellName(col, row int, abs ...bool) (string, error) {
if row > TotalRows {
return "", ErrMaxRows
}
sign := ""
colName, err := ColumnNumberToName(col)
if err != nil {
return "", err
}
for _, a := range abs {
if a {
sign = "$"
return "$" + colName + "$" + strconv.Itoa(row), nil
}
}
colName, err := ColumnNumberToName(col)
return sign + colName + sign + strconv.Itoa(row), err
return colName + strconv.Itoa(row), nil
}

// rangeRefToCoordinates provides a function to convert range reference to a
Expand Down Expand Up @@ -573,6 +585,9 @@ func (ext *xlsxExt) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// namespaceStrictToTransitional provides a method to convert Strict and
// Transitional namespaces.
func namespaceStrictToTransitional(content []byte) []byte {
if !bytes.Contains(content, []byte("purl.oclc.org")) {
return content
}
namespaceTranslationDic := map[string]string{
StrictNameSpaceDocumentPropertiesVariantTypes: NameSpaceDocumentPropertiesVariantTypes.Value,
StrictNameSpaceDrawingMLMain: NameSpaceDrawingMLMain,
Expand Down Expand Up @@ -781,15 +796,12 @@ func isNumeric(s string) (bool, int, float64) {
if strings.Contains(s, "_") {
return false, 0, 0
}
var decimal big.Float
_, ok := decimal.SetString(s)
if !ok {
flt, err := strconv.ParseFloat(s, 64)
if err != nil {
return false, 0, 0
}
var noScientificNotation string
flt, _ := decimal.Float64()
noScientificNotation = strconv.FormatFloat(flt, 'f', -1, 64)
return true, len(strings.ReplaceAll(noScientificNotation, ".", "")), flt
noScientificNotation := strconv.FormatFloat(flt, 'f', -1, 64)
return true, len(noScientificNotation) - strings.Count(noScientificNotation, "."), flt
}

var (
Expand All @@ -809,6 +821,9 @@ var (
// initial underscore shall itself be escaped (i.e. stored as _x005F_). For
// example: The string literal _x0008_ would be stored as _x005F_x0008_.
func bstrUnmarshal(s string) (result string) {
if !strings.Contains(s, "_x") {
return s
}
matches, l, cursor := bstrExp.FindAllStringSubmatchIndex(s, -1), len(s), 0
for _, match := range matches {
result += s[cursor:match[0]]
Expand Down
Loading