col.go 19 KB

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