Skip to content

add auto fit columns function #1386

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
95 changes: 95 additions & 0 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,101 @@ func (f *File) SetColWidth(sheet, startCol, endCol string, width float64) error
return err
}

// SetColAutoWidth provides a function to autofit from startCol to endCol columns according to their text content
//
// f := excelize.NewFile()
// err := f.SetColAutoWidth("Sheet1", "A", "H")
func (f *File) SetColAutoWidth(sheetName string, startCol, endCol string) error {
startColIdx, err := ColumnNameToNumber(startCol)
if err != nil {
return err
}

endColIdx, err := ColumnNameToNumber(endCol)
if err != nil {
return err
}

if startColIdx > endColIdx {
startColIdx, endColIdx = endColIdx, startColIdx
}

cols, err := f.Cols(sheetName)
if err != nil {
return err
}

colIdx := 1
for cols.Next() {
if colIdx >= startColIdx && colIdx <= endColIdx {
rowCells, _ := cols.Rows()
max := defaultColWidth
for i := range rowCells {
rowCell := rowCells[i]
cellWidth := float64(len(rowCell) + 3) // + 3 for margin
if cellWidth > max && cellWidth < MaxColumnWidth {
max = cellWidth
}
}

name, err := ColumnNumberToName(colIdx)
if err != nil {
return err
}

if err := f.SetColWidth(sheetName, name, name, float64(max)); err != nil {
return err
}
}

// fast go away
if colIdx == endColIdx {
break
}

colIdx++
}

return nil
}

// SetAllColAutoWidth provides a function to autofit all columns according to their text content
//
// f := excelize.NewFile()
// err := f.SetAllColAutoWidth("Sheet1")
func (f *File) SetAllColAutoWidth(sheetName string) error {
cols, err := f.Cols(sheetName)
if err != nil {
return err
}

colIdx := 1
for cols.Next() {
rowCells, _ := cols.Rows()
max := defaultColWidth
for i := range rowCells {
rowCell := rowCells[i]
cellWidth := float64(len(rowCell) + 3) // + 3 for margin
if cellWidth > max && cellWidth < MaxColumnWidth {
max = cellWidth
}
}

name, err := ColumnNumberToName(colIdx)
if err != nil {
return err
}

if err := f.SetColWidth(sheetName, name, name, float64(max)); err != nil {
return err
}

colIdx++
}

return nil
}

// flatCols provides a method for the column's operation functions to flatten
// and check the worksheet columns.
func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol) []xlsxCol {
Expand Down