col.go 19 KB

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