col.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 name 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 xlsx.Cols.Col[c].Min <= col && col <= xlsx.Cols.Col[c].Max {
  29. visible = !xlsx.Cols.Col[c].Hidden
  30. }
  31. }
  32. return visible
  33. }
  34. // SetColVisible provides a function to set visible of a single column by given
  35. // worksheet name 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 xlsx.Cols.Col[v].Min <= c && c <= xlsx.Cols.Col[v].Max {
  56. col = xlsx.Cols.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. // }
  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 int(defaultColWidthPixels)
  209. }
  210. // GetColWidth provides function to get column width by given worksheet name and
  211. // column index.
  212. func (f *File) GetColWidth(sheet, column string) float64 {
  213. col := TitleToNumber(strings.ToUpper(column)) + 1
  214. xlsx := f.workSheetReader(sheet)
  215. if xlsx.Cols != nil {
  216. var width float64
  217. for _, v := range xlsx.Cols.Col {
  218. if v.Min <= col && col <= v.Max {
  219. width = v.Width
  220. }
  221. }
  222. if width != 0 {
  223. return width
  224. }
  225. }
  226. // Optimisation for when the column widths haven't changed.
  227. return defaultColWidthPixels
  228. }
  229. // InsertCol provides function to insert a new column before given column index.
  230. // For example, create a new column before column C in Sheet1:
  231. //
  232. // xlsx.InsertCol("Sheet1", "C")
  233. //
  234. func (f *File) InsertCol(sheet, column string) {
  235. col := TitleToNumber(strings.ToUpper(column))
  236. f.adjustHelper(sheet, col, -1, 1)
  237. }
  238. // RemoveCol provides function to remove single column by given worksheet name
  239. // and column index. For example, remove column C in Sheet1:
  240. //
  241. // xlsx.RemoveCol("Sheet1", "C")
  242. //
  243. func (f *File) RemoveCol(sheet, column string) {
  244. xlsx := f.workSheetReader(sheet)
  245. for r := range xlsx.SheetData.Row {
  246. for k, v := range xlsx.SheetData.Row[r].C {
  247. axis := v.R
  248. col := string(strings.Map(letterOnlyMapF, axis))
  249. if col == column {
  250. xlsx.SheetData.Row[r].C = append(xlsx.SheetData.Row[r].C[:k], xlsx.SheetData.Row[r].C[k+1:]...)
  251. }
  252. }
  253. }
  254. col := TitleToNumber(strings.ToUpper(column))
  255. f.adjustHelper(sheet, col, -1, -1)
  256. }
  257. // Completion column element tags of XML in a sheet.
  258. func completeCol(xlsx *xlsxWorksheet, row, cell int) {
  259. buffer := bytes.Buffer{}
  260. for r := range xlsx.SheetData.Row {
  261. if len(xlsx.SheetData.Row[r].C) < cell {
  262. start := len(xlsx.SheetData.Row[r].C)
  263. for iii := start; iii < cell; iii++ {
  264. buffer.WriteString(ToAlphaString(iii))
  265. buffer.WriteString(strconv.Itoa(r + 1))
  266. xlsx.SheetData.Row[r].C = append(xlsx.SheetData.Row[r].C, xlsxC{
  267. R: buffer.String(),
  268. })
  269. buffer.Reset()
  270. }
  271. }
  272. }
  273. }
  274. // convertColWidthToPixels provieds function to convert the width of a cell from
  275. // user's units to pixels. Excel rounds the column width to the nearest pixel.
  276. // If the width hasn't been set by the user we use the default value. If the
  277. // column is hidden it has a value of zero.
  278. func convertColWidthToPixels(width float64) float64 {
  279. var padding float64 = 5
  280. var pixels float64
  281. var maxDigitWidth float64 = 7
  282. if width == 0 {
  283. return pixels
  284. }
  285. if width < 1 {
  286. pixels = (width * 12) + 0.5
  287. return math.Ceil(pixels)
  288. }
  289. pixels = (width*maxDigitWidth + 0.5) + padding
  290. return math.Ceil(pixels)
  291. }