col.go 19 KB

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