Skip to content

Commit 86cef47

Browse files
authored
This related qax-os#1813, mitigate inserted image aspect ratio incorrect issue in some cases (qax-os#1842)
- Check the specified base column width and convert it to pixels - GetPictures returns the Format.ScaleX and Format.ScaleY property with a precision of two decimal places - Update docs for AddPicture and GetPictures functions - Update unit tests
1 parent 5618929 commit 86cef47

4 files changed

Lines changed: 34 additions & 12 deletions

File tree

col.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,13 @@ func (f *File) getColWidth(sheet string, col int) int {
669669
return int(convertColWidthToPixels(width))
670670
}
671671
}
672-
if ws.SheetFormatPr != nil && ws.SheetFormatPr.DefaultColWidth > 0 {
673-
return int(convertColWidthToPixels(ws.SheetFormatPr.DefaultColWidth))
672+
if ws.SheetFormatPr != nil {
673+
if ws.SheetFormatPr.DefaultColWidth > 0 {
674+
return int(convertColWidthToPixels(ws.SheetFormatPr.DefaultColWidth))
675+
}
676+
if ws.SheetFormatPr.BaseColWidth > 0 {
677+
return int(convertColWidthToPixels(float64(ws.SheetFormatPr.BaseColWidth))) + 5
678+
}
674679
}
675680
// Optimization for when the column widths haven't changed.
676681
return int(defaultColWidthPixels)
@@ -730,8 +735,13 @@ func (f *File) GetColWidth(sheet, col string) (float64, error) {
730735
return width, err
731736
}
732737
}
733-
if ws.SheetFormatPr != nil && ws.SheetFormatPr.DefaultColWidth > 0 {
734-
return ws.SheetFormatPr.DefaultColWidth, err
738+
if ws.SheetFormatPr != nil {
739+
if ws.SheetFormatPr.DefaultColWidth > 0 {
740+
return ws.SheetFormatPr.DefaultColWidth, err
741+
}
742+
if ws.SheetFormatPr.BaseColWidth > 0 {
743+
return float64(ws.SheetFormatPr.BaseColWidth), err
744+
}
735745
}
736746
// Optimization for when the column widths haven't changed.
737747
return defaultColWidth, err

picture.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"encoding/xml"
1717
"image"
1818
"io"
19+
"math"
1920
"os"
2021
"path"
2122
"path/filepath"
@@ -151,10 +152,14 @@ func parseGraphicOptions(opts *GraphicOptions) *GraphicOptions {
151152
// object with the cell, the default value of that is 0.
152153
//
153154
// The optional parameter "ScaleX" specifies the horizontal scale of graph
154-
// object, the default value of that is 1.0 which presents 100%.
155+
// object. The value of ScaleY must be a floating-point number greater than 0
156+
// with a precision of two decimal places. The default value of that is 1.0
157+
// which presents 100%.
155158
//
156-
// The optional parameter "ScaleY" specifies the vertical scale of graph object,
157-
// the default value of that is 1.0 which presents 100%.
159+
// The optional parameter "ScaleY" specifies the vertical scale of graph object.
160+
// The value of ScaleY must be a floating-point number greater than 0 with a
161+
// precision of two decimal places. The default value of that is 1.0 which
162+
// presents 100%.
158163
//
159164
// The optional parameter "Hyperlink" specifies the hyperlink of the graph
160165
// object.
@@ -488,8 +493,8 @@ func (f *File) addMedia(file []byte, ext string) string {
488493
// returns the image contents as []byte data types. This function is
489494
// concurrency safe. Note that this function currently does not support
490495
// retrieving all properties from the image's Format property, and the value of
491-
// the ScaleX and ScaleY property may be an approximate precision value in some
492-
// cases. For example:
496+
// the ScaleX and ScaleY property is a floating-point number greater than 0 with
497+
// a precision of two decimal places. For example:
493498
//
494499
// f, err := excelize.OpenFile("Book1.xlsx")
495500
// if err != nil {
@@ -707,8 +712,8 @@ func (f *File) calculatePictureScale(pic *Picture, cx, cy int) {
707712
if err != nil || imgCfg.Width <= 0 || imgCfg.Height <= 0 || cx <= 0 || cy <= 0 {
708713
return
709714
}
710-
pic.Format.ScaleX = float64(cx) / float64(EMU) / float64(imgCfg.Width)
711-
pic.Format.ScaleY = float64(cy) / float64(EMU) / float64(imgCfg.Height)
715+
pic.Format.ScaleX = math.Round(float64(cx)/float64(EMU)/float64(imgCfg.Width)*100) / 100
716+
pic.Format.ScaleY = math.Round(float64(cy)/float64(EMU)/float64(imgCfg.Height)*100) / 100
712717
}
713718

714719
// extractPictureFromDecodeAnchor extracts picture data from a decoded cell

picture_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestAddPicture(t *testing.T) {
3939
opts := []GraphicOptions{
4040
{Hyperlink: "#Sheet2!D8", HyperlinkType: "Location"},
4141
{OffsetX: 10, OffsetY: 10, ScaleX: 0.5, ScaleY: 0.5, Hyperlink: "https://github.com/xuri/excelize", HyperlinkType: "External", Positioning: "oneCell"},
42-
{OffsetX: 10, OffsetY: 10, ScaleX: 1.5, ScaleY: 1.5, Hyperlink: "https://github.com/xuri/excelize", HyperlinkType: "External"},
42+
{OffsetX: 10, OffsetY: 10, ScaleX: 0.88, ScaleY: 0.88, Hyperlink: "https://github.com/xuri/excelize", HyperlinkType: "External"},
4343
{PrintObject: boolPtr(true), Locked: boolPtr(true), OffsetX: 200, ScaleX: 1, ScaleY: 1, Positioning: "oneCell"},
4444
{PrintObject: boolPtr(true), Locked: boolPtr(true), AltText: "Excel Logo", LockAspectRatio: true, ScaleX: 1, ScaleY: 1},
4545
}

sheetpr_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ func TestSetSheetProps(t *testing.T) {
8585
ws.(*xlsxWorksheet).SheetPr = nil
8686
assert.NoError(t, f.SetSheetProps("Sheet1", &SheetPropsOptions{TabColorTint: float64Ptr(1)}))
8787

88+
// Test get column width after set base column width
89+
_, err = f.NewSheet("Sheet2")
90+
assert.NoError(t, err)
91+
assert.NoError(t, f.SetSheetProps("Sheet2", &SheetPropsOptions{BaseColWidth: &baseColWidth}))
92+
width, err := f.GetColWidth("Sheet2", "A")
93+
assert.NoError(t, err)
94+
assert.Equal(t, 8.0, width)
8895
// Test set worksheet properties on not exists worksheet
8996
assert.EqualError(t, f.SetSheetProps("SheetN", nil), "sheet SheetN does not exist")
9097
// Test set worksheet properties with invalid sheet name

0 commit comments

Comments
 (0)