col.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.8 or later.
  9. package excelize
  10. import "math"
  11. // Define the default cell size and EMU unit of measurement.
  12. const (
  13. defaultColWidthPixels float64 = 64
  14. defaultRowHeightPixels float64 = 20
  15. EMU int = 9525
  16. )
  17. // GetColVisible provides a function to get visible of a single column by given
  18. // worksheet name and column name. For example, get visible state of column D
  19. // in Sheet1:
  20. //
  21. // visiable, err := xlsx.GetColVisible("Sheet1", "D")
  22. //
  23. func (f *File) GetColVisible(sheet, col string) (bool, error) {
  24. visible := true
  25. colNum, err := ColumnNameToNumber(col)
  26. if err != nil {
  27. return visible, err
  28. }
  29. xlsx := f.workSheetReader(sheet)
  30. if xlsx.Cols == nil {
  31. return visible, err
  32. }
  33. for c := range xlsx.Cols.Col {
  34. colData := &xlsx.Cols.Col[c]
  35. if colData.Min <= colNum && colNum <= colData.Max {
  36. visible = !colData.Hidden
  37. }
  38. }
  39. return visible, err
  40. }
  41. // SetColVisible provides a function to set visible of a single column by given
  42. // worksheet name and column name. For example, hide column D in Sheet1:
  43. //
  44. // err := xlsx.SetColVisible("Sheet1", "D", false)
  45. //
  46. func (f *File) SetColVisible(sheet, col string, visible bool) error {
  47. colNum, err := ColumnNameToNumber(col)
  48. if err != nil {
  49. return err
  50. }
  51. colData := xlsxCol{
  52. Min: colNum,
  53. Max: colNum,
  54. Hidden: !visible,
  55. CustomWidth: true,
  56. }
  57. xlsx := f.workSheetReader(sheet)
  58. if xlsx.Cols == nil {
  59. cols := xlsxCols{}
  60. cols.Col = append(cols.Col, colData)
  61. xlsx.Cols = &cols
  62. return err
  63. }
  64. for v := range xlsx.Cols.Col {
  65. if xlsx.Cols.Col[v].Min <= colNum && colNum <= xlsx.Cols.Col[v].Max {
  66. colData = xlsx.Cols.Col[v]
  67. }
  68. }
  69. colData.Min = colNum
  70. colData.Max = colNum
  71. colData.Hidden = !visible
  72. colData.CustomWidth = true
  73. xlsx.Cols.Col = append(xlsx.Cols.Col, colData)
  74. return err
  75. }
  76. // GetColOutlineLevel provides a function to get outline level of a single
  77. // column by given worksheet name and column name. For example, get outline
  78. // level of column D in Sheet1:
  79. //
  80. // level, err := xlsx.GetColOutlineLevel("Sheet1", "D")
  81. //
  82. func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) {
  83. level := uint8(0)
  84. colNum, err := ColumnNameToNumber(col)
  85. if err != nil {
  86. return level, err
  87. }
  88. xlsx := f.workSheetReader(sheet)
  89. if xlsx.Cols == nil {
  90. return level, err
  91. }
  92. for c := range xlsx.Cols.Col {
  93. colData := &xlsx.Cols.Col[c]
  94. if colData.Min <= colNum && colNum <= colData.Max {
  95. level = colData.OutlineLevel
  96. }
  97. }
  98. return level, err
  99. }
  100. // SetColOutlineLevel provides a function to set outline level of a single
  101. // column by given worksheet name and column name. For example, set outline
  102. // level of column D in Sheet1 to 2:
  103. //
  104. // err := xlsx.SetColOutlineLevel("Sheet1", "D", 2)
  105. //
  106. func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
  107. colNum, err := ColumnNameToNumber(col)
  108. if err != nil {
  109. return err
  110. }
  111. colData := xlsxCol{
  112. Min: colNum,
  113. Max: colNum,
  114. OutlineLevel: level,
  115. CustomWidth: true,
  116. }
  117. xlsx := f.workSheetReader(sheet)
  118. if xlsx.Cols == nil {
  119. cols := xlsxCols{}
  120. cols.Col = append(cols.Col, colData)
  121. xlsx.Cols = &cols
  122. return err
  123. }
  124. for v := range xlsx.Cols.Col {
  125. if xlsx.Cols.Col[v].Min <= colNum && colNum <= xlsx.Cols.Col[v].Max {
  126. colData = xlsx.Cols.Col[v]
  127. }
  128. }
  129. colData.Min = colNum
  130. colData.Max = colNum
  131. colData.OutlineLevel = level
  132. colData.CustomWidth = true
  133. xlsx.Cols.Col = append(xlsx.Cols.Col, colData)
  134. return err
  135. }
  136. // SetColWidth provides a function to set the width of a single column or
  137. // multiple columns. For example:
  138. //
  139. // xlsx := excelize.NewFile()
  140. // err := xlsx.SetColWidth("Sheet1", "A", "H", 20)
  141. //
  142. func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error {
  143. min, err := ColumnNameToNumber(startcol)
  144. if err != nil {
  145. return err
  146. }
  147. max, err := ColumnNameToNumber(endcol)
  148. if err != nil {
  149. return err
  150. }
  151. if min > max {
  152. min, max = max, min
  153. }
  154. xlsx := f.workSheetReader(sheet)
  155. col := xlsxCol{
  156. Min: min,
  157. Max: max,
  158. Width: width,
  159. CustomWidth: true,
  160. }
  161. if xlsx.Cols != nil {
  162. xlsx.Cols.Col = append(xlsx.Cols.Col, col)
  163. } else {
  164. cols := xlsxCols{}
  165. cols.Col = append(cols.Col, col)
  166. xlsx.Cols = &cols
  167. }
  168. return err
  169. }
  170. // positionObjectPixels calculate the vertices that define the position of a
  171. // graphical object within the worksheet in pixels.
  172. //
  173. // +------------+------------+
  174. // | A | B |
  175. // +-----+------------+------------+
  176. // | |(x1,y1) | |
  177. // | 1 |(A1)._______|______ |
  178. // | | | | |
  179. // | | | | |
  180. // +-----+----| OBJECT |-----+
  181. // | | | | |
  182. // | 2 | |______________. |
  183. // | | | (B2)|
  184. // | | | (x2,y2)|
  185. // +-----+------------+------------+
  186. //
  187. // Example of an object that covers some of the area from cell A1 to B2.
  188. //
  189. // Based on the width and height of the object we need to calculate 8 vars:
  190. //
  191. // colStart, rowStart, colEnd, rowEnd, x1, y1, x2, y2.
  192. //
  193. // We also calculate the absolute x and y position of the top left vertex of
  194. // the object. This is required for images.
  195. //
  196. // The width and height of the cells that the object occupies can be
  197. // variable and have to be taken into account.
  198. //
  199. // The values of col_start and row_start are passed in from the calling
  200. // function. The values of col_end and row_end are calculated by
  201. // subtracting the width and height of the object from the width and
  202. // height of the underlying cells.
  203. //
  204. // colStart # Col containing upper left corner of object.
  205. // x1 # Distance to left side of object.
  206. //
  207. // rowStart # Row containing top left corner of object.
  208. // y1 # Distance to top of object.
  209. //
  210. // colEnd # Col containing lower right corner of object.
  211. // x2 # Distance to right side of object.
  212. //
  213. // rowEnd # Row containing bottom right corner of object.
  214. // y2 # Distance to bottom of object.
  215. //
  216. // width # Width of object frame.
  217. // height # Height of object frame.
  218. //
  219. // xAbs # Absolute distance to left side of object.
  220. // yAbs # Absolute distance to top side of object.
  221. //
  222. func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int, int, int) {
  223. xAbs := 0
  224. yAbs := 0
  225. // Calculate the absolute x offset of the top-left vertex.
  226. for colID := 1; colID <= col; colID++ {
  227. xAbs += f.getColWidth(sheet, colID)
  228. }
  229. xAbs += x1
  230. // Calculate the absolute y offset of the top-left vertex.
  231. // Store the column change to allow optimisations.
  232. for rowID := 1; rowID <= row; rowID++ {
  233. yAbs += f.getRowHeight(sheet, rowID)
  234. }
  235. yAbs += y1
  236. // Adjust start column for offsets that are greater than the col width.
  237. for x1 >= f.getColWidth(sheet, col) {
  238. x1 -= f.getColWidth(sheet, col)
  239. col++
  240. }
  241. // Adjust start row for offsets that are greater than the row height.
  242. for y1 >= f.getRowHeight(sheet, row) {
  243. y1 -= f.getRowHeight(sheet, row)
  244. row++
  245. }
  246. // Initialise end cell to the same as the start cell.
  247. colEnd := col
  248. rowEnd := row
  249. width += x1
  250. height += y1
  251. // Subtract the underlying cell widths to find end cell of the object.
  252. for width >= f.getColWidth(sheet, colEnd+1) {
  253. colEnd++
  254. width -= f.getColWidth(sheet, colEnd)
  255. }
  256. // Subtract the underlying cell heights to find end cell of the object.
  257. for height >= f.getRowHeight(sheet, rowEnd) {
  258. height -= f.getRowHeight(sheet, rowEnd)
  259. rowEnd++
  260. }
  261. // The end vertices are whatever is left from the width and height.
  262. x2 := width
  263. y2 := height
  264. return col, row, xAbs, yAbs, colEnd, rowEnd, x2, y2
  265. }
  266. // getColWidth provides a function to get column width in pixels by given
  267. // sheet name and column index.
  268. func (f *File) getColWidth(sheet string, col int) int {
  269. xlsx := f.workSheetReader(sheet)
  270. if xlsx.Cols != nil {
  271. var width float64
  272. for _, v := range xlsx.Cols.Col {
  273. if v.Min <= col && col <= v.Max {
  274. width = v.Width
  275. }
  276. }
  277. if width != 0 {
  278. return int(convertColWidthToPixels(width))
  279. }
  280. }
  281. // Optimisation for when the column widths haven't changed.
  282. return int(defaultColWidthPixels)
  283. }
  284. // GetColWidth provides a function to get column width by given worksheet name
  285. // and column index.
  286. func (f *File) GetColWidth(sheet, col string) (float64, error) {
  287. colNum, err := ColumnNameToNumber(col)
  288. if err != nil {
  289. return defaultColWidthPixels, err
  290. }
  291. xlsx := f.workSheetReader(sheet)
  292. if xlsx.Cols != nil {
  293. var width float64
  294. for _, v := range xlsx.Cols.Col {
  295. if v.Min <= colNum && colNum <= v.Max {
  296. width = v.Width
  297. }
  298. }
  299. if width != 0 {
  300. return width, err
  301. }
  302. }
  303. // Optimisation for when the column widths haven't changed.
  304. return defaultColWidthPixels, err
  305. }
  306. // InsertCol provides a function to insert a new column before given column
  307. // index. For example, create a new column before column C in Sheet1:
  308. //
  309. // err := xlsx.InsertCol("Sheet1", "C")
  310. //
  311. func (f *File) InsertCol(sheet, col string) error {
  312. num, err := ColumnNameToNumber(col)
  313. if err != nil {
  314. return err
  315. }
  316. return f.adjustHelper(sheet, columns, num, 1)
  317. }
  318. // RemoveCol provides a function to remove single column by given worksheet
  319. // name and column index. For example, remove column C in Sheet1:
  320. //
  321. // err := xlsx.RemoveCol("Sheet1", "C")
  322. //
  323. // Use this method with caution, which will affect changes in references such
  324. // as formulas, charts, and so on. If there is any referenced value of the
  325. // worksheet, it will cause a file error when you open it. The excelize only
  326. // partially updates these references currently.
  327. func (f *File) RemoveCol(sheet, col string) error {
  328. num, err := ColumnNameToNumber(col)
  329. if err != nil {
  330. return err
  331. }
  332. xlsx := f.workSheetReader(sheet)
  333. for rowIdx := range xlsx.SheetData.Row {
  334. rowData := &xlsx.SheetData.Row[rowIdx]
  335. for colIdx := range rowData.C {
  336. colName, _, _ := SplitCellName(rowData.C[colIdx].R)
  337. if colName == col {
  338. rowData.C = append(rowData.C[:colIdx], rowData.C[colIdx+1:]...)[:len(rowData.C)-1]
  339. break
  340. }
  341. }
  342. }
  343. return f.adjustHelper(sheet, columns, num, -1)
  344. }
  345. // convertColWidthToPixels provieds function to convert the width of a cell
  346. // from user's units to pixels. Excel rounds the column width to the nearest
  347. // pixel. If the width hasn't been set by the user we use the default value.
  348. // If the column is hidden it has a value of zero.
  349. func convertColWidthToPixels(width float64) float64 {
  350. var padding float64 = 5
  351. var pixels float64
  352. var maxDigitWidth float64 = 7
  353. if width == 0 {
  354. return pixels
  355. }
  356. if width < 1 {
  357. pixels = (width * 12) + 0.5
  358. return math.Ceil(pixels)
  359. }
  360. pixels = (width*maxDigitWidth + 0.5) + padding
  361. return math.Ceil(pixels)
  362. }