col.go 7.8 KB

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