col.go 7.7 KB

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