Skip to content

Commit a2bda9b

Browse files
authored
Fix autoFitColWidth overriding width with latest value instead of max value (#2274)
- Update unit tests
1 parent 421c405 commit a2bda9b

4 files changed

Lines changed: 60 additions & 56 deletions

File tree

adjust_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ func TestAdjustFormula(t *testing.T) {
967967
// Test adjust array formula with invalid range reference
968968
formulaType, ref = STCellFormulaTypeArray, "E1:E2"
969969
assert.NoError(t, f.SetCellFormula("Sheet1", "E1", "XFD1:XFD1", FormulaOpts{Ref: &ref, Type: &formulaType}))
970-
assert.EqualError(t, f.InsertCols("Sheet1", "A", 1), "the column number must be greater than or equal to 1 and less than or equal to 16384")
970+
assert.Equal(t, ErrColumnNumber, f.InsertCols("Sheet1", "A", 1))
971971
})
972972
}
973973

@@ -1271,7 +1271,7 @@ func TestAdjustDrawings(t *testing.T) {
12711271
f, err = OpenFile(wb)
12721272
assert.NoError(t, err)
12731273
f.Pkg.Store("xl/drawings/drawing1.xml", []byte(xml.Header+fmt.Sprintf(`<wsDr xmlns="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"><oneCellAnchor><from><col>%d</col><row>0</row></from><mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"></mc:AlternateContent><clientData/></oneCellAnchor></wsDr>`, MaxColumns)))
1274-
assert.EqualError(t, f.InsertCols("Sheet1", "A", 1), "the column number must be greater than or equal to 1 and less than or equal to 16384")
1274+
assert.Equal(t, ErrColumnNumber, f.InsertCols("Sheet1", "A", 1))
12751275
}
12761276

12771277
func TestAdjustDefinedNames(t *testing.T) {

col.go

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -884,61 +884,52 @@ func (fnt *Font) calcRichTextWidth(runs []RichTextRun) float64 {
884884

885885
// autoFitColWidth provides a function to auto fit columns width according to
886886
// their text content with default font size and font.
887-
func (f *File) autoFitColWidth(sheet string, from, to, rows int, defaultFnt *Font) error {
888-
ws, err := f.workSheetReader(sheet)
889-
if err != nil {
890-
return err
891-
}
892-
for col := from; col <= to; col++ {
893-
var width float64
894-
for row := 1; row <= rows; row++ {
895-
cell, _ := CoordinatesToCellName(col, row)
896-
val, err := f.CalcCellValue(sheet, cell)
897-
if err != nil && inStrSlice([]string{
898-
formulaErrorDIV,
899-
formulaErrorNAME,
900-
formulaErrorNA,
901-
formulaErrorNUM,
902-
formulaErrorVALUE,
903-
formulaErrorREF,
904-
formulaErrorNULL,
905-
formulaErrorSPILL,
906-
formulaErrorCALC,
907-
formulaErrorGETTINGDATA,
908-
}, err.Error(), true) != -1 {
909-
val = err.Error()
910-
}
911-
if val == "" {
912-
continue
913-
}
914-
styleID, _ := f.GetCellStyle(sheet, cell)
915-
fnt := defaultFnt
916-
style, err := f.GetStyle(styleID)
917-
if err != nil {
918-
return err
919-
}
920-
if style != nil && style.Font != nil {
921-
fnt = style.Font
922-
}
923-
if cellType, _ := f.GetCellType(sheet, cell); cellType == CellTypeInlineString || cellType == CellTypeSharedString {
924-
runs, _ := f.GetCellRichText(sheet, cell)
925-
width = fnt.calcRichTextWidth(runs)
926-
continue
927-
}
928-
if w := fnt.calcTextWidth(val); w > width {
887+
func (f *File) autoFitColWidth(sheet string, col, rows int, defaultFnt *Font) (float64, error) {
888+
var width float64
889+
for row := 1; row <= rows; row++ {
890+
cell, err := CoordinatesToCellName(col, row)
891+
if err != nil {
892+
return width, err
893+
}
894+
val, err := f.CalcCellValue(sheet, cell)
895+
if err != nil && inStrSlice([]string{
896+
formulaErrorDIV,
897+
formulaErrorNAME,
898+
formulaErrorNA,
899+
formulaErrorNUM,
900+
formulaErrorVALUE,
901+
formulaErrorREF,
902+
formulaErrorNULL,
903+
formulaErrorSPILL,
904+
formulaErrorCALC,
905+
formulaErrorGETTINGDATA,
906+
}, err.Error(), true) != -1 {
907+
val = err.Error()
908+
}
909+
if val == "" {
910+
continue
911+
}
912+
styleID, _ := f.GetCellStyle(sheet, cell)
913+
fnt := defaultFnt
914+
style, err := f.GetStyle(styleID)
915+
if err != nil {
916+
return width, err
917+
}
918+
if style != nil && style.Font != nil {
919+
fnt = style.Font
920+
}
921+
if cellType, _ := f.GetCellType(sheet, cell); cellType == CellTypeInlineString || cellType == CellTypeSharedString {
922+
runs, _ := f.GetCellRichText(sheet, cell)
923+
if w := fnt.calcRichTextWidth(runs); w > width {
929924
width = w
930925
}
926+
continue
931927
}
932-
if width > 0 {
933-
width += 2
934-
if width > MaxColumnWidth {
935-
width = MaxColumnWidth
936-
}
937-
ws.setColWidth(col, col, width)
938-
ws.setColVisible(col, col, true)
928+
if w := fnt.calcTextWidth(val); w > width {
929+
width = w
939930
}
940931
}
941-
return nil
932+
return width, nil
942933
}
943934

944935
// AutoFitColWidth provides a function to auto fit columns width according to
@@ -980,5 +971,16 @@ func (f *File) AutoFitColWidth(sheet, columns string) error {
980971
defaultFnt.Family = *font.Name.Val
981972
}
982973
}
983-
return f.autoFitColWidth(sheet, minVal, maxVal, len(rows), defaultFnt)
974+
ws, _ := f.workSheetReader(sheet)
975+
for col := minVal; col <= maxVal; col++ {
976+
if width, _ := f.autoFitColWidth(sheet, col, len(rows), defaultFnt); width > 0 {
977+
width += 2
978+
if width > MaxColumnWidth {
979+
width = MaxColumnWidth
980+
}
981+
ws.setColVisible(col, col, true)
982+
ws.setColWidth(col, col, width)
983+
}
984+
}
985+
return err
984986
}

col_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ func TestAutoFitColWidth(t *testing.T) {
549549

550550
assert.Equal(t, f.AutoFitColWidth("Sheet1", ""), newInvalidColumnNameError(""))
551551
assert.Equal(t, f.AutoFitColWidth("SheetN", "A"), ErrSheetNotExist{"SheetN"})
552-
assert.Equal(t, f.autoFitColWidth("SheetN", 1, 1, 0, &Font{}), ErrSheetNotExist{"SheetN"})
552+
_, err = f.autoFitColWidth("Sheet1", TotalRows, 1, &Font{})
553+
assert.Equal(t, ErrColumnNumber, err)
553554
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAutoFitColWidth.xlsx")))
554555

555556
f = NewFile()
@@ -563,5 +564,6 @@ func TestAutoFitColWidth(t *testing.T) {
563564
{R: 1, C: []xlsxC{{R: "A1", S: 1, V: "Test"}}},
564565
}},
565566
})
566-
assert.Equal(t, f.autoFitColWidth("Sheet1", 1, 1, 1, &Font{}), newInvalidStyleID(1))
567+
_, err = f.autoFitColWidth("Sheet1", 1, 1, &Font{})
568+
assert.Equal(t, err, newInvalidStyleID(1))
567569
}

sheet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ func TestSheetDimension(t *testing.T) {
819819
{"Sheet1", "123", "cannot convert cell \"123\" to coordinates: invalid cell name \"123\""},
820820
{"Sheet1", "A:B", "cannot convert cell \"A\" to coordinates: invalid cell name \"A\""},
821821
{"Sheet1", ":B10", "cannot convert cell \"\" to coordinates: invalid cell name \"\""},
822-
{"Sheet1", "XFE1", "the column number must be greater than or equal to 1 and less than or equal to 16384"},
822+
{"Sheet1", "XFE1", ErrColumnNumber.Error()},
823823
{"Sheet1", "A1048577", "row number exceeds maximum limit"},
824824
{"Sheet1", "ZZZ", "cannot convert cell \"ZZZ\" to coordinates: invalid cell name \"ZZZ\""},
825825
{"SheetN", "A1", "sheet SheetN does not exist"},

0 commit comments

Comments
 (0)