col.go 8.8 KB

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