col.go 19 KB

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