col.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. // Copyright 2016 - 2021 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 / XLSM / XLTM files. Supports reading and writing
  7. // spreadsheet documents generated by Microsoft Excel™ 2007 and later. Supports
  8. // complex components by high compatibility, and provided streaming API for
  9. // generating or reading data from a worksheet with huge amounts of data. This
  10. // library needs Go version 1.15 or later.
  11. package excelize
  12. import (
  13. "bytes"
  14. "encoding/xml"
  15. "math"
  16. "strconv"
  17. "strings"
  18. "github.com/mohae/deepcopy"
  19. )
  20. // Define the default cell size and EMU unit of measurement.
  21. const (
  22. defaultColWidth float64 = 9.140625
  23. defaultColWidthPixels float64 = 64
  24. defaultRowHeight float64 = 15
  25. defaultRowHeightPixels float64 = 20
  26. EMU int = 9525
  27. )
  28. // Cols defines an iterator to a sheet
  29. type Cols struct {
  30. err error
  31. curCol, totalCol, stashCol, totalRow int
  32. sheet string
  33. f *File
  34. sheetXML []byte
  35. }
  36. // GetCols return all the columns in a sheet by given worksheet name (case
  37. // sensitive). For example:
  38. //
  39. // cols, err := f.GetCols("Sheet1")
  40. // if err != nil {
  41. // fmt.Println(err)
  42. // return
  43. // }
  44. // for _, col := range cols {
  45. // for _, rowCell := range col {
  46. // fmt.Print(rowCell, "\t")
  47. // }
  48. // fmt.Println()
  49. // }
  50. //
  51. func (f *File) GetCols(sheet string) ([][]string, error) {
  52. cols, err := f.Cols(sheet)
  53. if err != nil {
  54. return nil, err
  55. }
  56. results := make([][]string, 0, 64)
  57. for cols.Next() {
  58. col, _ := cols.Rows()
  59. results = append(results, col)
  60. }
  61. return results, nil
  62. }
  63. // Next will return true if the next column is found.
  64. func (cols *Cols) Next() bool {
  65. cols.curCol++
  66. return cols.curCol <= cols.totalCol
  67. }
  68. // Error will return an error when the error occurs.
  69. func (cols *Cols) Error() error {
  70. return cols.err
  71. }
  72. // Rows return the current column's row values.
  73. func (cols *Cols) Rows() ([]string, error) {
  74. var (
  75. err error
  76. inElement string
  77. cellCol, cellRow int
  78. rows []string
  79. )
  80. if cols.stashCol >= cols.curCol {
  81. return rows, err
  82. }
  83. d := cols.f.sharedStringsReader()
  84. decoder := cols.f.xmlNewDecoder(bytes.NewReader(cols.sheetXML))
  85. for {
  86. token, _ := decoder.Token()
  87. if token == nil {
  88. break
  89. }
  90. switch xmlElement := token.(type) {
  91. case xml.StartElement:
  92. inElement = xmlElement.Name.Local
  93. if inElement == "row" {
  94. cellCol = 0
  95. cellRow++
  96. attrR, _ := attrValToInt("r", xmlElement.Attr)
  97. if attrR != 0 {
  98. cellRow = attrR
  99. }
  100. }
  101. if inElement == "c" {
  102. cellCol++
  103. for _, attr := range xmlElement.Attr {
  104. if attr.Name.Local == "r" {
  105. if cellCol, cellRow, err = CellNameToCoordinates(attr.Value); err != nil {
  106. return rows, err
  107. }
  108. }
  109. }
  110. blank := cellRow - len(rows)
  111. for i := 1; i < blank; i++ {
  112. rows = append(rows, "")
  113. }
  114. if cellCol == cols.curCol {
  115. colCell := xlsxC{}
  116. _ = decoder.DecodeElement(&colCell, &xmlElement)
  117. val, _ := colCell.getValueFrom(cols.f, d)
  118. rows = append(rows, val)
  119. }
  120. }
  121. case xml.EndElement:
  122. if xmlElement.Name.Local == "sheetData" {
  123. return rows, err
  124. }
  125. }
  126. }
  127. return rows, err
  128. }
  129. // columnXMLIterator defined runtime use field for the worksheet column SAX parser.
  130. type columnXMLIterator struct {
  131. err error
  132. cols Cols
  133. cellCol, curRow, row int
  134. }
  135. // columnXMLHandler parse the column XML element of the worksheet.
  136. func columnXMLHandler(colIterator *columnXMLIterator, xmlElement *xml.StartElement) {
  137. colIterator.err = nil
  138. inElement := xmlElement.Name.Local
  139. if inElement == "row" {
  140. colIterator.row++
  141. for _, attr := range xmlElement.Attr {
  142. if attr.Name.Local == "r" {
  143. if colIterator.curRow, colIterator.err = strconv.Atoi(attr.Value); colIterator.err != nil {
  144. return
  145. }
  146. colIterator.row = colIterator.curRow
  147. }
  148. }
  149. colIterator.cols.totalRow = colIterator.row
  150. colIterator.cellCol = 0
  151. }
  152. if inElement == "c" {
  153. colIterator.cellCol++
  154. for _, attr := range xmlElement.Attr {
  155. if attr.Name.Local == "r" {
  156. if colIterator.cellCol, _, colIterator.err = CellNameToCoordinates(attr.Value); colIterator.err != nil {
  157. return
  158. }
  159. }
  160. }
  161. if colIterator.cellCol > colIterator.cols.totalCol {
  162. colIterator.cols.totalCol = colIterator.cellCol
  163. }
  164. }
  165. }
  166. // Cols returns a columns iterator, used for streaming reading data for a
  167. // worksheet with a large data. For example:
  168. //
  169. // cols, err := f.Cols("Sheet1")
  170. // if err != nil {
  171. // fmt.Println(err)
  172. // return
  173. // }
  174. // for cols.Next() {
  175. // col, err := cols.Rows()
  176. // if err != nil {
  177. // fmt.Println(err)
  178. // }
  179. // for _, rowCell := range col {
  180. // fmt.Print(rowCell, "\t")
  181. // }
  182. // fmt.Println()
  183. // }
  184. //
  185. func (f *File) Cols(sheet string) (*Cols, error) {
  186. name, ok := f.sheetMap[trimSheetName(sheet)]
  187. if !ok {
  188. return nil, ErrSheetNotExist{sheet}
  189. }
  190. if f.Sheet[name] != nil {
  191. output, _ := xml.Marshal(f.Sheet[name])
  192. f.saveFileList(name, f.replaceNameSpaceBytes(name, output))
  193. }
  194. var colIterator columnXMLIterator
  195. colIterator.cols.sheetXML = f.readXML(name)
  196. decoder := f.xmlNewDecoder(bytes.NewReader(colIterator.cols.sheetXML))
  197. for {
  198. token, _ := decoder.Token()
  199. if token == nil {
  200. break
  201. }
  202. switch xmlElement := token.(type) {
  203. case xml.StartElement:
  204. columnXMLHandler(&colIterator, &xmlElement)
  205. if colIterator.err != nil {
  206. return &colIterator.cols, colIterator.err
  207. }
  208. case xml.EndElement:
  209. if xmlElement.Name.Local == "sheetData" {
  210. colIterator.cols.f = f
  211. colIterator.cols.sheet = trimSheetName(sheet)
  212. return &colIterator.cols, nil
  213. }
  214. }
  215. }
  216. return &colIterator.cols, nil
  217. }
  218. // GetColVisible provides a function to get visible of a single column by given
  219. // worksheet name and column name. For example, get visible state of column D
  220. // in Sheet1:
  221. //
  222. // visible, err := f.GetColVisible("Sheet1", "D")
  223. //
  224. func (f *File) GetColVisible(sheet, col string) (bool, error) {
  225. visible := true
  226. colNum, err := ColumnNameToNumber(col)
  227. if err != nil {
  228. return visible, err
  229. }
  230. ws, err := f.workSheetReader(sheet)
  231. if err != nil {
  232. return false, err
  233. }
  234. if ws.Cols == nil {
  235. return visible, err
  236. }
  237. for c := range ws.Cols.Col {
  238. colData := &ws.Cols.Col[c]
  239. if colData.Min <= colNum && colNum <= colData.Max {
  240. visible = !colData.Hidden
  241. }
  242. }
  243. return visible, err
  244. }
  245. // SetColVisible provides a function to set visible columns by given worksheet
  246. // name, columns range and visibility.
  247. //
  248. // For example hide column D on Sheet1:
  249. //
  250. // err := f.SetColVisible("Sheet1", "D", false)
  251. //
  252. // Hide the columns from D to F (included):
  253. //
  254. // err := f.SetColVisible("Sheet1", "D:F", false)
  255. //
  256. func (f *File) SetColVisible(sheet, columns string, visible bool) error {
  257. start, end, err := f.parseColRange(columns)
  258. if err != nil {
  259. return err
  260. }
  261. ws, err := f.workSheetReader(sheet)
  262. if err != nil {
  263. return err
  264. }
  265. colData := xlsxCol{
  266. Min: start,
  267. Max: end,
  268. Width: defaultColWidth, // default width
  269. Hidden: !visible,
  270. CustomWidth: true,
  271. }
  272. if ws.Cols == nil {
  273. cols := xlsxCols{}
  274. cols.Col = append(cols.Col, colData)
  275. ws.Cols = &cols
  276. return nil
  277. }
  278. ws.Cols.Col = flatCols(colData, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  279. fc.BestFit = c.BestFit
  280. fc.Collapsed = c.Collapsed
  281. fc.CustomWidth = c.CustomWidth
  282. fc.OutlineLevel = c.OutlineLevel
  283. fc.Phonetic = c.Phonetic
  284. fc.Style = c.Style
  285. fc.Width = c.Width
  286. return fc
  287. })
  288. return nil
  289. }
  290. // GetColOutlineLevel provides a function to get outline level of a single
  291. // column by given worksheet name and column name. For example, get outline
  292. // level of column D in Sheet1:
  293. //
  294. // level, err := f.GetColOutlineLevel("Sheet1", "D")
  295. //
  296. func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) {
  297. level := uint8(0)
  298. colNum, err := ColumnNameToNumber(col)
  299. if err != nil {
  300. return level, err
  301. }
  302. ws, err := f.workSheetReader(sheet)
  303. if err != nil {
  304. return 0, err
  305. }
  306. if ws.Cols == nil {
  307. return level, err
  308. }
  309. for c := range ws.Cols.Col {
  310. colData := &ws.Cols.Col[c]
  311. if colData.Min <= colNum && colNum <= colData.Max {
  312. level = colData.OutlineLevel
  313. }
  314. }
  315. return level, err
  316. }
  317. // parseColRange parse and convert column range with column name to the column number.
  318. func (f *File) parseColRange(columns string) (start, end int, err error) {
  319. colsTab := strings.Split(columns, ":")
  320. start, err = ColumnNameToNumber(colsTab[0])
  321. if err != nil {
  322. return
  323. }
  324. end = start
  325. if len(colsTab) == 2 {
  326. if end, err = ColumnNameToNumber(colsTab[1]); err != nil {
  327. return
  328. }
  329. }
  330. if end < start {
  331. start, end = end, start
  332. }
  333. return
  334. }
  335. // SetColOutlineLevel provides a function to set outline level of a single
  336. // column by given worksheet name and column name. The value of parameter
  337. // 'level' is 1-7. For example, set outline level of column D in Sheet1 to 2:
  338. //
  339. // err := f.SetColOutlineLevel("Sheet1", "D", 2)
  340. //
  341. func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
  342. if level > 7 || level < 1 {
  343. return ErrOutlineLevel
  344. }
  345. colNum, err := ColumnNameToNumber(col)
  346. if err != nil {
  347. return err
  348. }
  349. colData := xlsxCol{
  350. Min: colNum,
  351. Max: colNum,
  352. OutlineLevel: level,
  353. CustomWidth: true,
  354. }
  355. ws, err := f.workSheetReader(sheet)
  356. if err != nil {
  357. return err
  358. }
  359. if ws.Cols == nil {
  360. cols := xlsxCols{}
  361. cols.Col = append(cols.Col, colData)
  362. ws.Cols = &cols
  363. return err
  364. }
  365. ws.Cols.Col = flatCols(colData, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  366. fc.BestFit = c.BestFit
  367. fc.Collapsed = c.Collapsed
  368. fc.CustomWidth = c.CustomWidth
  369. fc.Hidden = c.Hidden
  370. fc.Phonetic = c.Phonetic
  371. fc.Style = c.Style
  372. fc.Width = c.Width
  373. return fc
  374. })
  375. return err
  376. }
  377. // SetColStyle provides a function to set style of columns by given worksheet
  378. // name, columns range and style ID.
  379. //
  380. // For example set style of column H on Sheet1:
  381. //
  382. // err = f.SetColStyle("Sheet1", "H", style)
  383. //
  384. // Set style of columns C:F on Sheet1:
  385. //
  386. // err = f.SetColStyle("Sheet1", "C:F", style)
  387. //
  388. func (f *File) SetColStyle(sheet, columns string, styleID int) error {
  389. start, end, err := f.parseColRange(columns)
  390. if err != nil {
  391. return err
  392. }
  393. ws, err := f.workSheetReader(sheet)
  394. if err != nil {
  395. return err
  396. }
  397. if ws.Cols == nil {
  398. ws.Cols = &xlsxCols{}
  399. }
  400. ws.Cols.Col = flatCols(xlsxCol{
  401. Min: start,
  402. Max: end,
  403. Width: defaultColWidth,
  404. Style: styleID,
  405. }, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  406. fc.BestFit = c.BestFit
  407. fc.Collapsed = c.Collapsed
  408. fc.CustomWidth = c.CustomWidth
  409. fc.Hidden = c.Hidden
  410. fc.OutlineLevel = c.OutlineLevel
  411. fc.Phonetic = c.Phonetic
  412. fc.Width = c.Width
  413. return fc
  414. })
  415. return nil
  416. }
  417. // SetColWidth provides a function to set the width of a single column or
  418. // multiple columns. For example:
  419. //
  420. // f := excelize.NewFile()
  421. // err := f.SetColWidth("Sheet1", "A", "H", 20)
  422. //
  423. func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error {
  424. min, err := ColumnNameToNumber(startcol)
  425. if err != nil {
  426. return err
  427. }
  428. max, err := ColumnNameToNumber(endcol)
  429. if err != nil {
  430. return err
  431. }
  432. if width > MaxColumnWidth {
  433. return ErrColumnWidth
  434. }
  435. if min > max {
  436. min, max = max, min
  437. }
  438. ws, err := f.workSheetReader(sheet)
  439. if err != nil {
  440. return err
  441. }
  442. col := xlsxCol{
  443. Min: min,
  444. Max: max,
  445. Width: width,
  446. CustomWidth: true,
  447. }
  448. if ws.Cols == nil {
  449. cols := xlsxCols{}
  450. cols.Col = append(cols.Col, col)
  451. ws.Cols = &cols
  452. return err
  453. }
  454. ws.Cols.Col = flatCols(col, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  455. fc.BestFit = c.BestFit
  456. fc.Collapsed = c.Collapsed
  457. fc.Hidden = c.Hidden
  458. fc.OutlineLevel = c.OutlineLevel
  459. fc.Phonetic = c.Phonetic
  460. fc.Style = c.Style
  461. return fc
  462. })
  463. return err
  464. }
  465. // flatCols provides a method for the column's operation functions to flatten
  466. // and check the worksheet columns.
  467. func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol) []xlsxCol {
  468. fc := []xlsxCol{}
  469. for i := col.Min; i <= col.Max; i++ {
  470. c := deepcopy.Copy(col).(xlsxCol)
  471. c.Min, c.Max = i, i
  472. fc = append(fc, c)
  473. }
  474. inFlat := func(colID int, cols []xlsxCol) (int, bool) {
  475. for idx, c := range cols {
  476. if c.Max == colID && c.Min == colID {
  477. return idx, true
  478. }
  479. }
  480. return -1, false
  481. }
  482. for _, column := range cols {
  483. for i := column.Min; i <= column.Max; i++ {
  484. if idx, ok := inFlat(i, fc); ok {
  485. fc[idx] = replacer(fc[idx], column)
  486. continue
  487. }
  488. c := deepcopy.Copy(column).(xlsxCol)
  489. c.Min, c.Max = i, i
  490. fc = append(fc, c)
  491. }
  492. }
  493. return fc
  494. }
  495. // positionObjectPixels calculate the vertices that define the position of a
  496. // graphical object within the worksheet in pixels.
  497. //
  498. // +------------+------------+
  499. // | A | B |
  500. // +-----+------------+------------+
  501. // | |(x1,y1) | |
  502. // | 1 |(A1)._______|______ |
  503. // | | | | |
  504. // | | | | |
  505. // +-----+----| OBJECT |-----+
  506. // | | | | |
  507. // | 2 | |______________. |
  508. // | | | (B2)|
  509. // | | | (x2,y2)|
  510. // +-----+------------+------------+
  511. //
  512. // Example of an object that covers some of the area from cell A1 to B2.
  513. //
  514. // Based on the width and height of the object we need to calculate 8 vars:
  515. //
  516. // colStart, rowStart, colEnd, rowEnd, x1, y1, x2, y2.
  517. //
  518. // We also calculate the absolute x and y position of the top left vertex of
  519. // the object. This is required for images.
  520. //
  521. // The width and height of the cells that the object occupies can be
  522. // variable and have to be taken into account.
  523. //
  524. // The values of col_start and row_start are passed in from the calling
  525. // function. The values of col_end and row_end are calculated by
  526. // subtracting the width and height of the object from the width and
  527. // height of the underlying cells.
  528. //
  529. // colStart # Col containing upper left corner of object.
  530. // x1 # Distance to left side of object.
  531. //
  532. // rowStart # Row containing top left corner of object.
  533. // y1 # Distance to top of object.
  534. //
  535. // colEnd # Col containing lower right corner of object.
  536. // x2 # Distance to right side of object.
  537. //
  538. // rowEnd # Row containing bottom right corner of object.
  539. // y2 # Distance to bottom of object.
  540. //
  541. // width # Width of object frame.
  542. // height # Height of object frame.
  543. //
  544. func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int) {
  545. // Adjust start column for offsets that are greater than the col width.
  546. for x1 >= f.getColWidth(sheet, col) {
  547. x1 -= f.getColWidth(sheet, col)
  548. col++
  549. }
  550. // Adjust start row for offsets that are greater than the row height.
  551. for y1 >= f.getRowHeight(sheet, row) {
  552. y1 -= f.getRowHeight(sheet, row)
  553. row++
  554. }
  555. // Initialise end cell to the same as the start cell.
  556. colEnd := col
  557. rowEnd := row
  558. width += x1
  559. height += y1
  560. // Subtract the underlying cell widths to find end cell of the object.
  561. for width >= f.getColWidth(sheet, colEnd+1) {
  562. colEnd++
  563. width -= f.getColWidth(sheet, colEnd)
  564. }
  565. // Subtract the underlying cell heights to find end cell of the object.
  566. for height >= f.getRowHeight(sheet, rowEnd) {
  567. height -= f.getRowHeight(sheet, rowEnd)
  568. rowEnd++
  569. }
  570. // The end vertices are whatever is left from the width and height.
  571. x2 := width
  572. y2 := height
  573. return col, row, colEnd, rowEnd, x2, y2
  574. }
  575. // getColWidth provides a function to get column width in pixels by given
  576. // sheet name and column index.
  577. func (f *File) getColWidth(sheet string, col int) int {
  578. xlsx, _ := f.workSheetReader(sheet)
  579. if xlsx.Cols != nil {
  580. var width float64
  581. for _, v := range xlsx.Cols.Col {
  582. if v.Min <= col && col <= v.Max {
  583. width = v.Width
  584. }
  585. }
  586. if width != 0 {
  587. return int(convertColWidthToPixels(width))
  588. }
  589. }
  590. // Optimisation for when the column widths haven't changed.
  591. return int(defaultColWidthPixels)
  592. }
  593. // GetColWidth provides a function to get column width by given worksheet name
  594. // and column index.
  595. func (f *File) GetColWidth(sheet, col string) (float64, error) {
  596. colNum, err := ColumnNameToNumber(col)
  597. if err != nil {
  598. return defaultColWidth, err
  599. }
  600. ws, err := f.workSheetReader(sheet)
  601. if err != nil {
  602. return defaultColWidth, err
  603. }
  604. if ws.Cols != nil {
  605. var width float64
  606. for _, v := range ws.Cols.Col {
  607. if v.Min <= colNum && colNum <= v.Max {
  608. width = v.Width
  609. }
  610. }
  611. if width != 0 {
  612. return width, err
  613. }
  614. }
  615. // Optimisation for when the column widths haven't changed.
  616. return defaultColWidth, err
  617. }
  618. // InsertCol provides a function to insert a new column before given column
  619. // index. For example, create a new column before column C in Sheet1:
  620. //
  621. // err := f.InsertCol("Sheet1", "C")
  622. //
  623. func (f *File) InsertCol(sheet, col string) error {
  624. num, err := ColumnNameToNumber(col)
  625. if err != nil {
  626. return err
  627. }
  628. return f.adjustHelper(sheet, columns, num, 1)
  629. }
  630. // RemoveCol provides a function to remove single column by given worksheet
  631. // name and column index. For example, remove column C in Sheet1:
  632. //
  633. // err := f.RemoveCol("Sheet1", "C")
  634. //
  635. // Use this method with caution, which will affect changes in references such
  636. // as formulas, charts, and so on. If there is any referenced value of the
  637. // worksheet, it will cause a file error when you open it. The excelize only
  638. // partially updates these references currently.
  639. func (f *File) RemoveCol(sheet, col string) error {
  640. num, err := ColumnNameToNumber(col)
  641. if err != nil {
  642. return err
  643. }
  644. ws, err := f.workSheetReader(sheet)
  645. if err != nil {
  646. return err
  647. }
  648. for rowIdx := range ws.SheetData.Row {
  649. rowData := &ws.SheetData.Row[rowIdx]
  650. for colIdx := range rowData.C {
  651. colName, _, _ := SplitCellName(rowData.C[colIdx].R)
  652. if colName == col {
  653. rowData.C = append(rowData.C[:colIdx], rowData.C[colIdx+1:]...)[:len(rowData.C)-1]
  654. break
  655. }
  656. }
  657. }
  658. return f.adjustHelper(sheet, columns, num, -1)
  659. }
  660. // convertColWidthToPixels provieds function to convert the width of a cell
  661. // from user's units to pixels. Excel rounds the column width to the nearest
  662. // pixel. If the width hasn't been set by the user we use the default value.
  663. // If the column is hidden it has a value of zero.
  664. func convertColWidthToPixels(width float64) float64 {
  665. var padding float64 = 5
  666. var pixels float64
  667. var maxDigitWidth float64 = 7
  668. if width == 0 {
  669. return pixels
  670. }
  671. if width < 1 {
  672. pixels = (width * 12) + 0.5
  673. return math.Ceil(pixels)
  674. }
  675. pixels = (width*maxDigitWidth + 0.5) + padding
  676. return math.Ceil(pixels)
  677. }