col.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package excelize
  2. import (
  3. "math"
  4. "strconv"
  5. "strings"
  6. )
  7. // Define the default cell size and EMU unit of measurement.
  8. const (
  9. defaultColWidthPixels int = 64
  10. defaultRowHeightPixels int = 20
  11. EMU int = 9525
  12. )
  13. // SetColWidth provides function to set the width of a single column or multiple
  14. // columns. For example:
  15. //
  16. // xlsx := excelize.CreateFile()
  17. // xlsx.SetColWidth("Sheet1", "A", "H", 20)
  18. // err := xlsx.Save()
  19. // if err != nil {
  20. // fmt.Println(err)
  21. // os.Exit(1)
  22. // }
  23. //
  24. func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) {
  25. min := titleToNumber(strings.ToUpper(startcol)) + 1
  26. max := titleToNumber(strings.ToUpper(endcol)) + 1
  27. if min > max {
  28. min, max = max, min
  29. }
  30. xlsx := f.workSheetReader(sheet)
  31. col := xlsxCol{
  32. Min: min,
  33. Max: max,
  34. Width: width,
  35. CustomWidth: true,
  36. }
  37. if xlsx.Cols != nil {
  38. xlsx.Cols.Col = append(xlsx.Cols.Col, col)
  39. } else {
  40. cols := xlsxCols{}
  41. cols.Col = append(cols.Col, col)
  42. xlsx.Cols = &cols
  43. }
  44. }
  45. // positionObjectPixels calculate the vertices that define the position of a
  46. // graphical object within the worksheet in pixels.
  47. //
  48. // +------------+------------+
  49. // | A | B |
  50. // +-----+------------+------------+
  51. // | |(x1,y1) | |
  52. // | 1 |(A1)._______|______ |
  53. // | | | | |
  54. // | | | | |
  55. // +-----+----| OBJECT |-----+
  56. // | | | | |
  57. // | 2 | |______________. |
  58. // | | | (B2)|
  59. // | | | (x2,y2)|
  60. // +-----+------------+------------+
  61. //
  62. // Example of an object that covers some of the area from cell A1 to B2.
  63. //
  64. // Based on the width and height of the object we need to calculate 8 vars:
  65. //
  66. // colStart, rowStart, colEnd, rowEnd, x1, y1, x2, y2.
  67. //
  68. // We also calculate the absolute x and y position of the top left vertex of
  69. // the object. This is required for images.
  70. //
  71. // The width and height of the cells that the object occupies can be
  72. // variable and have to be taken into account.
  73. //
  74. // The values of col_start and row_start are passed in from the calling
  75. // function. The values of col_end and row_end are calculated by
  76. // subtracting the width and height of the object from the width and
  77. // height of the underlying cells.
  78. //
  79. // colStart # Col containing upper left corner of object.
  80. // x1 # Distance to left side of object.
  81. //
  82. // rowStart # Row containing top left corner of object.
  83. // y1 # Distance to top of object.
  84. //
  85. // colEnd # Col containing lower right corner of object.
  86. // x2 # Distance to right side of object.
  87. //
  88. // rowEnd # Row containing bottom right corner of object.
  89. // y2 # Distance to bottom of object.
  90. //
  91. // width # Width of object frame.
  92. // height # Height of object frame.
  93. //
  94. // xAbs # Absolute distance to left side of object.
  95. // yAbs # Absolute distance to top side of object.
  96. //
  97. func (f *File) positionObjectPixels(sheet string, colStart, rowStart, x1, y1, width, height int) (int, int, int, int, int, int, int, int) {
  98. xAbs := 0
  99. yAbs := 0
  100. // Calculate the absolute x offset of the top-left vertex.
  101. for colID := 1; colID <= colStart; colID++ {
  102. xAbs += f.getColWidth(sheet, colID)
  103. }
  104. xAbs += x1
  105. // Calculate the absolute y offset of the top-left vertex.
  106. // Store the column change to allow optimisations.
  107. for rowID := 1; rowID <= rowStart; rowID++ {
  108. yAbs += f.getRowHeight(sheet, rowID)
  109. }
  110. yAbs += y1
  111. // Adjust start column for offsets that are greater than the col width.
  112. for x1 >= f.getColWidth(sheet, colStart) {
  113. x1 -= f.getColWidth(sheet, colStart)
  114. colStart++
  115. }
  116. // Adjust start row for offsets that are greater than the row height.
  117. for y1 >= f.getRowHeight(sheet, rowStart) {
  118. y1 -= f.getRowHeight(sheet, rowStart)
  119. rowStart++
  120. }
  121. // Initialise end cell to the same as the start cell.
  122. colEnd := colStart
  123. rowEnd := rowStart
  124. width += x1
  125. height += y1
  126. // Subtract the underlying cell widths to find end cell of the object.
  127. for width >= f.getColWidth(sheet, colEnd) {
  128. colEnd++
  129. width -= f.getColWidth(sheet, colEnd)
  130. }
  131. // Subtract the underlying cell heights to find end cell of the object.
  132. for height >= f.getRowHeight(sheet, rowEnd) {
  133. rowEnd++
  134. height -= f.getRowHeight(sheet, rowEnd)
  135. }
  136. // The end vertices are whatever is left from the width and height.
  137. x2 := width
  138. y2 := height
  139. return colStart, rowStart, xAbs, yAbs, colEnd, rowEnd, x2, y2
  140. }
  141. // getColWidth provides function to get column width in pixels by given sheet
  142. // name and column index.
  143. func (f *File) getColWidth(sheet string, col int) int {
  144. xlsx := f.workSheetReader(sheet)
  145. if xlsx.Cols != nil {
  146. var width float64
  147. for _, v := range xlsx.Cols.Col {
  148. if v.Min <= col && col <= v.Max {
  149. width = v.Width
  150. }
  151. }
  152. if width != 0 {
  153. return int(convertColWidthToPixels(width))
  154. }
  155. }
  156. // Optimisation for when the column widths haven't changed.
  157. return defaultColWidthPixels
  158. }
  159. // getRowHeight provides function to get row height in pixels by given sheet
  160. // name and row index.
  161. func (f *File) getRowHeight(sheet string, row int) int {
  162. xlsx := f.workSheetReader(sheet)
  163. for _, v := range xlsx.SheetData.Row {
  164. if v.R == row && v.Ht != "" {
  165. ht, _ := strconv.ParseFloat(v.Ht, 64)
  166. return int(convertRowHeightToPixels(ht))
  167. }
  168. }
  169. // Optimisation for when the row heights haven't changed.
  170. return defaultRowHeightPixels
  171. }
  172. // convertColWidthToPixels provieds function to convert the width of a cell from
  173. // user's units to pixels. Excel rounds the column width to the nearest pixel.
  174. // If the width hasn't been set by the user we use the default value. If the
  175. // column is hidden it has a value of zero.
  176. func convertColWidthToPixels(width float64) float64 {
  177. var padding float64 = 5
  178. var pixels float64
  179. var maxDigitWidth float64 = 7
  180. if width == 0 {
  181. return pixels
  182. }
  183. if width < 1 {
  184. pixels = (width * 12) + 0.5
  185. return math.Ceil(pixels)
  186. }
  187. pixels = (width*maxDigitWidth + 0.5) + padding
  188. return math.Ceil(pixels)
  189. }
  190. // convertRowHeightToPixels provides function to convert the height of a cell
  191. // from user's units to pixels. If the height hasn't been set by the user we use
  192. // the default value. If the row is hidden it has a value of zero.
  193. func convertRowHeightToPixels(height float64) float64 {
  194. var pixels float64
  195. if height == 0 {
  196. return pixels
  197. }
  198. pixels = math.Ceil(4.0 / 3.0 * height)
  199. return pixels
  200. }