@@ -14,7 +14,6 @@ package excelize
14
14
import (
15
15
"bytes"
16
16
"encoding/xml"
17
- "math"
18
17
"strconv"
19
18
"strings"
20
19
@@ -23,10 +22,10 @@ import (
23
22
24
23
// Define the default cell size and EMU unit of measurement.
25
24
const (
26
- defaultColWidth float64 = 9.140625
27
- defaultColWidthPixels float64 = 64
28
- defaultRowHeight float64 = 15
29
- defaultRowHeightPixels float64 = 20
25
+ defaultColWidth float64 = 10.5
26
+ defaultColWidthPixels float64 = 84.0
27
+ defaultRowHeight float64 = 15.6
28
+ defaultRowHeightPixels float64 = 20.8
30
29
EMU int = 9525
31
30
)
32
31
@@ -621,40 +620,35 @@ func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol)
621
620
//
622
621
// width # Width of object frame.
623
622
// height # Height of object frame.
624
- func (f * File ) positionObjectPixels (sheet string , col , row , x1 , y1 , width , height int ) (int , int , int , int , int , int ) {
623
+ func (f * File ) positionObjectPixels (sheet string , col , row , width , height int , opts * GraphicOptions ) (int , int , int , int , int , int , int , int ) {
625
624
colIdx , rowIdx := col - 1 , row - 1
626
- // Adjust start column for offsets that are greater than the col width.
627
- for x1 >= f .getColWidth (sheet , colIdx + 1 ) {
628
- colIdx ++
629
- x1 -= f .getColWidth (sheet , colIdx )
630
- }
631
-
632
- // Adjust start row for offsets that are greater than the row height.
633
- for y1 >= f .getRowHeight (sheet , rowIdx + 1 ) {
634
- rowIdx ++
635
- y1 -= f .getRowHeight (sheet , rowIdx )
636
- }
637
-
638
625
// Initialized end cell to the same as the start cell.
639
626
colEnd , rowEnd := colIdx , rowIdx
627
+ x1 , y1 , x2 , y2 := opts .OffsetX , opts .OffsetY , width , height
628
+ if opts .Positioning == "" || opts .Positioning == "twoCell" {
629
+ // Using a twoCellAnchor, the maximum possible offset is limited by the
630
+ // "from" cell dimensions. If these were to be exceeded the "toPoint" would
631
+ // be calculated incorrectly, since the requested "fromPoint" is not possible
632
+
633
+ x1 = min (x1 , f .getColWidth (sheet , col ))
634
+ y1 = min (y1 , f .getRowHeight (sheet , row ))
635
+
636
+ x2 += x1
637
+ y2 += y1
638
+ // Subtract the underlying cell widths to find end cell of the object.
639
+ for x2 >= f .getColWidth (sheet , colEnd + 1 ) {
640
+ colEnd ++
641
+ x2 -= f .getColWidth (sheet , colEnd )
642
+ }
640
643
641
- width += x1
642
- height += y1
643
-
644
- // Subtract the underlying cell widths to find end cell of the object.
645
- for width >= f .getColWidth (sheet , colEnd + 1 ) {
646
- colEnd ++
647
- width -= f .getColWidth (sheet , colEnd )
648
- }
649
-
650
- // Subtract the underlying cell heights to find end cell of the object.
651
- for height >= f .getRowHeight (sheet , rowEnd + 1 ) {
652
- rowEnd ++
653
- height -= f .getRowHeight (sheet , rowEnd )
644
+ // Subtract the underlying cell heights to find end cell of the object.
645
+ for y2 >= f .getRowHeight (sheet , rowEnd + 1 ) {
646
+ rowEnd ++
647
+ y2 -= f .getRowHeight (sheet , rowEnd )
648
+ }
654
649
}
655
-
656
650
// The end vertices are whatever is left from the width and height.
657
- return colIdx , rowIdx , colEnd , rowEnd , width , height
651
+ return colIdx , rowIdx , colEnd , rowEnd , x1 , y1 , x2 , y2
658
652
}
659
653
660
654
// getColWidth provides a function to get column width in pixels by given
@@ -664,13 +658,14 @@ func (f *File) getColWidth(sheet string, col int) int {
664
658
ws .mu .Lock ()
665
659
defer ws .mu .Unlock ()
666
660
if ws .Cols != nil {
667
- var width float64
661
+ width := - 1.0
668
662
for _ , v := range ws .Cols .Col {
669
663
if v .Min <= col && col <= v .Max && v .Width != nil {
670
664
width = * v .Width
665
+ break
671
666
}
672
667
}
673
- if width != 0 {
668
+ if width != - 1. 0 {
674
669
return int (convertColWidthToPixels (width ))
675
670
}
676
671
}
@@ -801,16 +796,15 @@ func (f *File) RemoveCol(sheet, col string) error {
801
796
// pixel. If the width hasn't been set by the user we use the default value.
802
797
// If the column is hidden it has a value of zero.
803
798
func convertColWidthToPixels (width float64 ) float64 {
804
- var padding float64 = 5
805
799
var pixels float64
806
- var maxDigitWidth float64 = 7
800
+ var maxDigitWidth float64 = 8
807
801
if width == 0 {
808
802
return pixels
809
803
}
810
804
if width < 1 {
811
805
pixels = (width * 12 ) + 0.5
812
- return math . Ceil ( pixels )
806
+ return float64 ( int ( pixels ) )
813
807
}
814
- pixels = (width * maxDigitWidth + 0.5 ) + padding
815
- return math . Ceil ( pixels )
808
+ pixels = (width * maxDigitWidth + 0.5 )
809
+ return float64 ( int ( pixels ) )
816
810
}
0 commit comments