Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ColumnNumberToName function performance, reduce about 50% memory usage and 50% time cost #1935

Merged
merged 7 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 9 additions & 3 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,18 @@ func ColumnNumberToName(num int) (string, error) {
if num < MinColumns || num > MaxColumns {
return "", ErrColumnNumber
}
var col string
estimatedLength := 0
xuri marked this conversation as resolved.
Show resolved Hide resolved
for n := num; n > 0; n = (n - 1) / 26 {
estimatedLength++
}

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

// CellNameToCoordinates converts alphanumeric cell name to [X, Y] coordinates
Expand Down
19 changes: 16 additions & 3 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@ import (
"github.com/mohae/deepcopy"
)

type Option func(ws *xlsxWorksheet)

func WithMaxSheetRow(count int) Option {
return func(ws *xlsxWorksheet) {
if count > 0 {
ws.SheetData.Row = make([]xlsxRow, 0, count)
}
}
}

// NewSheet provides the function to create a new sheet by given a worksheet
// name and returns the index of the sheets in the workbook after it appended.
// Note that when creating a new workbook, the default worksheet named
// `Sheet1` will be created.
func (f *File) NewSheet(sheet string) (int, error) {
func (f *File) NewSheet(sheet string, options ...Option) (int, error) {
xuri marked this conversation as resolved.
Show resolved Hide resolved
var err error
if err = checkSheetName(sheet); err != nil {
return -1, err
Expand All @@ -57,7 +67,7 @@ func (f *File) NewSheet(sheet string) (int, error) {
// Update [Content_Types].xml
_ = f.setContentTypes("/xl/worksheets/sheet"+strconv.Itoa(sheetID)+".xml", ContentTypeSpreadSheetMLWorksheet)
// Create new sheet /xl/worksheets/sheet%d.xml
f.setSheet(sheetID, sheet)
f.setSheet(sheetID, sheet, options...)
// Update workbook.xml.rels
rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipWorkSheet, fmt.Sprintf("/xl/worksheets/sheet%d.xml", sheetID), "")
// Update workbook.xml
Expand Down Expand Up @@ -230,13 +240,16 @@ func (f *File) setContentTypes(partName, contentType string) error {
}

// setSheet provides a function to update sheet property by given index.
func (f *File) setSheet(index int, name string) {
func (f *File) setSheet(index int, name string, options ...Option) {
ws := xlsxWorksheet{
Dimension: &xlsxDimension{Ref: "A1"},
SheetViews: &xlsxSheetViews{
SheetView: []xlsxSheetView{{WorkbookViewID: 0}},
},
}
for _, opt := range options {
opt(&ws)
}
sheetXMLPath := "xl/worksheets/sheet" + strconv.Itoa(index) + ".xml"
f.sheetMap[name] = sheetXMLPath
f.Sheet.Store(sheetXMLPath, &ws)
Expand Down
3 changes: 3 additions & 0 deletions sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func TestNewSheet(t *testing.T) {
sheetID, err = f.NewSheet(":\\/?*[]")
assert.EqualError(t, err, ErrSheetNameInvalid.Error())
assert.Equal(t, -1, sheetID)
sheetID, err = f.NewSheet("Sheet3", WithMaxSheetRow(50))
assert.NoError(t, err)

}

func TestPanes(t *testing.T) {
Expand Down