col.go 18 KB

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