col.go 6.9 KB

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