col.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. // GetColOutlineLevel provides a function to get outline level of a single
  66. // column by given worksheet name and column name. For example, get outline
  67. // level of column D in Sheet1:
  68. //
  69. // xlsx.GetColOutlineLevel("Sheet1", "D")
  70. //
  71. func (f *File) GetColOutlineLevel(sheet, column string) uint8 {
  72. xlsx := f.workSheetReader(sheet)
  73. col := TitleToNumber(strings.ToUpper(column)) + 1
  74. level := uint8(0)
  75. if xlsx.Cols == nil {
  76. return level
  77. }
  78. for c := range xlsx.Cols.Col {
  79. if xlsx.Cols.Col[c].Min <= col && col <= xlsx.Cols.Col[c].Max {
  80. level = xlsx.Cols.Col[c].OutlineLevel
  81. }
  82. }
  83. return level
  84. }
  85. // SetColOutlineLevel provides a function to set outline level of a single
  86. // column by given worksheet name and column name. For example, set outline
  87. // level of column D in Sheet1 to 2:
  88. //
  89. // xlsx.SetColOutlineLevel("Sheet1", "D", 2)
  90. //
  91. func (f *File) SetColOutlineLevel(sheet, column string, level uint8) {
  92. xlsx := f.workSheetReader(sheet)
  93. c := TitleToNumber(strings.ToUpper(column)) + 1
  94. col := xlsxCol{
  95. Min: c,
  96. Max: c,
  97. OutlineLevel: level,
  98. CustomWidth: true,
  99. }
  100. if xlsx.Cols == nil {
  101. cols := xlsxCols{}
  102. cols.Col = append(cols.Col, col)
  103. xlsx.Cols = &cols
  104. return
  105. }
  106. for v := range xlsx.Cols.Col {
  107. if xlsx.Cols.Col[v].Min <= c && c <= xlsx.Cols.Col[v].Max {
  108. col = xlsx.Cols.Col[v]
  109. }
  110. }
  111. col.Min = c
  112. col.Max = c
  113. col.OutlineLevel = level
  114. col.CustomWidth = true
  115. xlsx.Cols.Col = append(xlsx.Cols.Col, col)
  116. }
  117. // SetColWidth provides a function to set the width of a single column or
  118. // multiple columns. For example:
  119. //
  120. // xlsx := excelize.NewFile()
  121. // xlsx.SetColWidth("Sheet1", "A", "H", 20)
  122. // err := xlsx.Save()
  123. // if err != nil {
  124. // fmt.Println(err)
  125. // }
  126. //
  127. func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) {
  128. min := TitleToNumber(strings.ToUpper(startcol)) + 1
  129. max := TitleToNumber(strings.ToUpper(endcol)) + 1
  130. if min > max {
  131. min, max = max, min
  132. }
  133. xlsx := f.workSheetReader(sheet)
  134. col := xlsxCol{
  135. Min: min,
  136. Max: max,
  137. Width: width,
  138. CustomWidth: true,
  139. }
  140. if xlsx.Cols != nil {
  141. xlsx.Cols.Col = append(xlsx.Cols.Col, col)
  142. } else {
  143. cols := xlsxCols{}
  144. cols.Col = append(cols.Col, col)
  145. xlsx.Cols = &cols
  146. }
  147. }
  148. // positionObjectPixels calculate the vertices that define the position of a
  149. // graphical object within the worksheet in pixels.
  150. //
  151. // +------------+------------+
  152. // | A | B |
  153. // +-----+------------+------------+
  154. // | |(x1,y1) | |
  155. // | 1 |(A1)._______|______ |
  156. // | | | | |
  157. // | | | | |
  158. // +-----+----| OBJECT |-----+
  159. // | | | | |
  160. // | 2 | |______________. |
  161. // | | | (B2)|
  162. // | | | (x2,y2)|
  163. // +-----+------------+------------+
  164. //
  165. // Example of an object that covers some of the area from cell A1 to B2.
  166. //
  167. // Based on the width and height of the object we need to calculate 8 vars:
  168. //
  169. // colStart, rowStart, colEnd, rowEnd, x1, y1, x2, y2.
  170. //
  171. // We also calculate the absolute x and y position of the top left vertex of
  172. // the object. This is required for images.
  173. //
  174. // The width and height of the cells that the object occupies can be
  175. // variable and have to be taken into account.
  176. //
  177. // The values of col_start and row_start are passed in from the calling
  178. // function. The values of col_end and row_end are calculated by
  179. // subtracting the width and height of the object from the width and
  180. // height of the underlying cells.
  181. //
  182. // colStart # Col containing upper left corner of object.
  183. // x1 # Distance to left side of object.
  184. //
  185. // rowStart # Row containing top left corner of object.
  186. // y1 # Distance to top of object.
  187. //
  188. // colEnd # Col containing lower right corner of object.
  189. // x2 # Distance to right side of object.
  190. //
  191. // rowEnd # Row containing bottom right corner of object.
  192. // y2 # Distance to bottom of object.
  193. //
  194. // width # Width of object frame.
  195. // height # Height of object frame.
  196. //
  197. // xAbs # Absolute distance to left side of object.
  198. // yAbs # Absolute distance to top side of object.
  199. //
  200. func (f *File) positionObjectPixels(sheet string, colStart, rowStart, x1, y1, width, height int) (int, int, int, int, int, int, int, int) {
  201. xAbs := 0
  202. yAbs := 0
  203. // Calculate the absolute x offset of the top-left vertex.
  204. for colID := 1; colID <= colStart; colID++ {
  205. xAbs += f.getColWidth(sheet, colID)
  206. }
  207. xAbs += x1
  208. // Calculate the absolute y offset of the top-left vertex.
  209. // Store the column change to allow optimisations.
  210. for rowID := 1; rowID <= rowStart; rowID++ {
  211. yAbs += f.getRowHeight(sheet, rowID)
  212. }
  213. yAbs += y1
  214. // Adjust start column for offsets that are greater than the col width.
  215. for x1 >= f.getColWidth(sheet, colStart) {
  216. x1 -= f.getColWidth(sheet, colStart)
  217. colStart++
  218. }
  219. // Adjust start row for offsets that are greater than the row height.
  220. for y1 >= f.getRowHeight(sheet, rowStart) {
  221. y1 -= f.getRowHeight(sheet, rowStart)
  222. rowStart++
  223. }
  224. // Initialise end cell to the same as the start cell.
  225. colEnd := colStart
  226. rowEnd := rowStart
  227. width += x1
  228. height += y1
  229. // Subtract the underlying cell widths to find end cell of the object.
  230. for width >= f.getColWidth(sheet, colEnd) {
  231. colEnd++
  232. width -= f.getColWidth(sheet, colEnd)
  233. }
  234. // Subtract the underlying cell heights to find end cell of the object.
  235. for height >= f.getRowHeight(sheet, rowEnd) {
  236. rowEnd++
  237. height -= f.getRowHeight(sheet, rowEnd)
  238. }
  239. // The end vertices are whatever is left from the width and height.
  240. x2 := width
  241. y2 := height
  242. return colStart, rowStart, xAbs, yAbs, colEnd, rowEnd, x2, y2
  243. }
  244. // getColWidth provides a function to get column width in pixels by given
  245. // sheet name and column index.
  246. func (f *File) getColWidth(sheet string, col int) int {
  247. xlsx := f.workSheetReader(sheet)
  248. if xlsx.Cols != nil {
  249. var width float64
  250. for _, v := range xlsx.Cols.Col {
  251. if v.Min <= col && col <= v.Max {
  252. width = v.Width
  253. }
  254. }
  255. if width != 0 {
  256. return int(convertColWidthToPixels(width))
  257. }
  258. }
  259. // Optimisation for when the column widths haven't changed.
  260. return int(defaultColWidthPixels)
  261. }
  262. // GetColWidth provides a function to get column width by given worksheet name
  263. // and column index.
  264. func (f *File) GetColWidth(sheet, column string) float64 {
  265. col := TitleToNumber(strings.ToUpper(column)) + 1
  266. xlsx := f.workSheetReader(sheet)
  267. if xlsx.Cols != nil {
  268. var width float64
  269. for _, v := range xlsx.Cols.Col {
  270. if v.Min <= col && col <= v.Max {
  271. width = v.Width
  272. }
  273. }
  274. if width != 0 {
  275. return width
  276. }
  277. }
  278. // Optimisation for when the column widths haven't changed.
  279. return defaultColWidthPixels
  280. }
  281. // InsertCol provides a function to insert a new column before given column
  282. // index. For example, create a new column before column C in Sheet1:
  283. //
  284. // xlsx.InsertCol("Sheet1", "C")
  285. //
  286. func (f *File) InsertCol(sheet, column string) {
  287. col := TitleToNumber(strings.ToUpper(column))
  288. f.adjustHelper(sheet, col, -1, 1)
  289. }
  290. // RemoveCol provides a function to remove single column by given worksheet
  291. // name and column index. For example, remove column C in Sheet1:
  292. //
  293. // xlsx.RemoveCol("Sheet1", "C")
  294. //
  295. func (f *File) RemoveCol(sheet, column string) {
  296. xlsx := f.workSheetReader(sheet)
  297. for r := range xlsx.SheetData.Row {
  298. for k, v := range xlsx.SheetData.Row[r].C {
  299. axis := v.R
  300. col := string(strings.Map(letterOnlyMapF, axis))
  301. if col == column {
  302. xlsx.SheetData.Row[r].C = append(xlsx.SheetData.Row[r].C[:k], xlsx.SheetData.Row[r].C[k+1:]...)
  303. }
  304. }
  305. }
  306. col := TitleToNumber(strings.ToUpper(column))
  307. f.adjustHelper(sheet, col, -1, -1)
  308. }
  309. // Completion column element tags of XML in a sheet.
  310. func completeCol(xlsx *xlsxWorksheet, row, cell int) {
  311. buffer := bytes.Buffer{}
  312. for r := range xlsx.SheetData.Row {
  313. if len(xlsx.SheetData.Row[r].C) < cell {
  314. start := len(xlsx.SheetData.Row[r].C)
  315. for iii := start; iii < cell; iii++ {
  316. buffer.WriteString(ToAlphaString(iii))
  317. buffer.WriteString(strconv.Itoa(r + 1))
  318. xlsx.SheetData.Row[r].C = append(xlsx.SheetData.Row[r].C, xlsxC{
  319. R: buffer.String(),
  320. })
  321. buffer.Reset()
  322. }
  323. }
  324. }
  325. }
  326. // convertColWidthToPixels provieds function to convert the width of a cell
  327. // from user's units to pixels. Excel rounds the column width to the nearest
  328. // pixel. If the width hasn't been set by the user we use the default value.
  329. // If the column is hidden it has a value of zero.
  330. func convertColWidthToPixels(width float64) float64 {
  331. var padding float64 = 5
  332. var pixels float64
  333. var maxDigitWidth float64 = 7
  334. if width == 0 {
  335. return pixels
  336. }
  337. if width < 1 {
  338. pixels = (width * 12) + 0.5
  339. return math.Ceil(pixels)
  340. }
  341. pixels = (width*maxDigitWidth + 0.5) + padding
  342. return math.Ceil(pixels)
  343. }