@@ -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}
0 commit comments