col.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. "math"
  16. "strconv"
  17. "strings"
  18. "github.com/mohae/deepcopy"
  19. )
  20. // Define the default cell size and EMU unit of measurement.
  21. const (
  22. defaultColWidth float64 = 9.140625
  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 ws, ok := f.Sheet.Load(name); ok && ws != nil {
  191. worksheet := ws.(*xlsxWorksheet)
  192. worksheet.Lock()
  193. defer worksheet.Unlock()
  194. output, _ := xml.Marshal(worksheet)
  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. start, end, err := f.parseColRange(columns)
  261. if err != nil {
  262. return err
  263. }
  264. ws, err := f.workSheetReader(sheet)
  265. if err != nil {
  266. return err
  267. }
  268. colData := xlsxCol{
  269. Min: start,
  270. Max: end,
  271. Width: defaultColWidth, // default width
  272. Hidden: !visible,
  273. CustomWidth: true,
  274. }
  275. if ws.Cols == nil {
  276. cols := xlsxCols{}
  277. cols.Col = append(cols.Col, colData)
  278. ws.Cols = &cols
  279. return nil
  280. }
  281. ws.Cols.Col = flatCols(colData, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  282. fc.BestFit = c.BestFit
  283. fc.Collapsed = c.Collapsed
  284. fc.CustomWidth = c.CustomWidth
  285. fc.OutlineLevel = c.OutlineLevel
  286. fc.Phonetic = c.Phonetic
  287. fc.Style = c.Style
  288. fc.Width = c.Width
  289. return fc
  290. })
  291. return nil
  292. }
  293. // GetColOutlineLevel provides a function to get outline level of a single
  294. // column by given worksheet name and column name. For example, get outline
  295. // level of column D in Sheet1:
  296. //
  297. // level, err := f.GetColOutlineLevel("Sheet1", "D")
  298. //
  299. func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) {
  300. level := uint8(0)
  301. colNum, err := ColumnNameToNumber(col)
  302. if err != nil {
  303. return level, err
  304. }
  305. ws, err := f.workSheetReader(sheet)
  306. if err != nil {
  307. return 0, err
  308. }
  309. if ws.Cols == nil {
  310. return level, err
  311. }
  312. for c := range ws.Cols.Col {
  313. colData := &ws.Cols.Col[c]
  314. if colData.Min <= colNum && colNum <= colData.Max {
  315. level = colData.OutlineLevel
  316. }
  317. }
  318. return level, err
  319. }
  320. // parseColRange parse and convert column range with column name to the column number.
  321. func (f *File) parseColRange(columns string) (start, end int, err error) {
  322. colsTab := strings.Split(columns, ":")
  323. start, err = ColumnNameToNumber(colsTab[0])
  324. if err != nil {
  325. return
  326. }
  327. end = start
  328. if len(colsTab) == 2 {
  329. if end, err = ColumnNameToNumber(colsTab[1]); err != nil {
  330. return
  331. }
  332. }
  333. if end < start {
  334. start, end = end, start
  335. }
  336. return
  337. }
  338. // SetColOutlineLevel provides a function to set outline level of a single
  339. // column by given worksheet name and column name. The value of parameter
  340. // 'level' is 1-7. For example, set outline level of column D in Sheet1 to 2:
  341. //
  342. // err := f.SetColOutlineLevel("Sheet1", "D", 2)
  343. //
  344. func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
  345. if level > 7 || level < 1 {
  346. return ErrOutlineLevel
  347. }
  348. colNum, err := ColumnNameToNumber(col)
  349. if err != nil {
  350. return err
  351. }
  352. colData := xlsxCol{
  353. Min: colNum,
  354. Max: colNum,
  355. OutlineLevel: level,
  356. CustomWidth: true,
  357. }
  358. ws, err := f.workSheetReader(sheet)
  359. if err != nil {
  360. return err
  361. }
  362. if ws.Cols == nil {
  363. cols := xlsxCols{}
  364. cols.Col = append(cols.Col, colData)
  365. ws.Cols = &cols
  366. return err
  367. }
  368. ws.Cols.Col = flatCols(colData, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  369. fc.BestFit = c.BestFit
  370. fc.Collapsed = c.Collapsed
  371. fc.CustomWidth = c.CustomWidth
  372. fc.Hidden = c.Hidden
  373. fc.Phonetic = c.Phonetic
  374. fc.Style = c.Style
  375. fc.Width = c.Width
  376. return fc
  377. })
  378. return err
  379. }
  380. // SetColStyle provides a function to set style of columns by given worksheet
  381. // name, columns range and style ID.
  382. //
  383. // For example set style of column H on Sheet1:
  384. //
  385. // err = f.SetColStyle("Sheet1", "H", style)
  386. //
  387. // Set style of columns C:F on Sheet1:
  388. //
  389. // err = f.SetColStyle("Sheet1", "C:F", style)
  390. //
  391. func (f *File) SetColStyle(sheet, columns string, styleID int) error {
  392. start, end, err := f.parseColRange(columns)
  393. if err != nil {
  394. return err
  395. }
  396. ws, err := f.workSheetReader(sheet)
  397. if err != nil {
  398. return err
  399. }
  400. if ws.Cols == nil {
  401. ws.Cols = &xlsxCols{}
  402. }
  403. ws.Cols.Col = flatCols(xlsxCol{
  404. Min: start,
  405. Max: end,
  406. Width: defaultColWidth,
  407. Style: styleID,
  408. }, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  409. fc.BestFit = c.BestFit
  410. fc.Collapsed = c.Collapsed
  411. fc.CustomWidth = c.CustomWidth
  412. fc.Hidden = c.Hidden
  413. fc.OutlineLevel = c.OutlineLevel
  414. fc.Phonetic = c.Phonetic
  415. fc.Width = c.Width
  416. return fc
  417. })
  418. if rows := len(ws.SheetData.Row); rows > 0 {
  419. for col := start; col <= end; col++ {
  420. from, _ := CoordinatesToCellName(col, 1)
  421. to, _ := CoordinatesToCellName(col, rows)
  422. err = f.SetCellStyle(sheet, from, to, styleID)
  423. }
  424. }
  425. return err
  426. }
  427. // SetColWidth provides a function to set the width of a single column or
  428. // multiple columns. For example:
  429. //
  430. // f := excelize.NewFile()
  431. // err := f.SetColWidth("Sheet1", "A", "H", 20)
  432. //
  433. func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error {
  434. min, err := ColumnNameToNumber(startcol)
  435. if err != nil {
  436. return err
  437. }
  438. max, err := ColumnNameToNumber(endcol)
  439. if err != nil {
  440. return err
  441. }
  442. if width > MaxColumnWidth {
  443. return ErrColumnWidth
  444. }
  445. if min > max {
  446. min, max = max, min
  447. }
  448. ws, err := f.workSheetReader(sheet)
  449. if err != nil {
  450. return err
  451. }
  452. col := xlsxCol{
  453. Min: min,
  454. Max: max,
  455. Width: width,
  456. CustomWidth: true,
  457. }
  458. if ws.Cols == nil {
  459. cols := xlsxCols{}
  460. cols.Col = append(cols.Col, col)
  461. ws.Cols = &cols
  462. return err
  463. }
  464. ws.Cols.Col = flatCols(col, ws.Cols.Col, func(fc, c xlsxCol) xlsxCol {
  465. fc.BestFit = c.BestFit
  466. fc.Collapsed = c.Collapsed
  467. fc.Hidden = c.Hidden
  468. fc.OutlineLevel = c.OutlineLevel
  469. fc.Phonetic = c.Phonetic
  470. fc.Style = c.Style
  471. return fc
  472. })
  473. return err
  474. }
  475. // flatCols provides a method for the column's operation functions to flatten
  476. // and check the worksheet columns.
  477. func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol) []xlsxCol {
  478. fc := []xlsxCol{}
  479. for i := col.Min; i <= col.Max; i++ {
  480. c := deepcopy.Copy(col).(xlsxCol)
  481. c.Min, c.Max = i, i
  482. fc = append(fc, c)
  483. }
  484. inFlat := func(colID int, cols []xlsxCol) (int, bool) {
  485. for idx, c := range cols {
  486. if c.Max == colID && c.Min == colID {
  487. return idx, true
  488. }
  489. }
  490. return -1, false
  491. }
  492. for _, column := range cols {
  493. for i := column.Min; i <= column.Max; i++ {
  494. if idx, ok := inFlat(i, fc); ok {
  495. fc[idx] = replacer(fc[idx], column)
  496. continue
  497. }
  498. c := deepcopy.Copy(column).(xlsxCol)
  499. c.Min, c.Max = i, i
  500. fc = append(fc, c)
  501. }
  502. }
  503. return fc
  504. }
  505. // positionObjectPixels calculate the vertices that define the position of a
  506. // graphical object within the worksheet in pixels.
  507. //
  508. // +------------+------------+
  509. // | A | B |
  510. // +-----+------------+------------+
  511. // | |(x1,y1) | |
  512. // | 1 |(A1)._______|______ |
  513. // | | | | |
  514. // | | | | |
  515. // +-----+----| OBJECT |-----+
  516. // | | | | |
  517. // | 2 | |______________. |
  518. // | | | (B2)|
  519. // | | | (x2,y2)|
  520. // +-----+------------+------------+
  521. //
  522. // Example of an object that covers some of the area from cell A1 to B2.
  523. //
  524. // Based on the width and height of the object we need to calculate 8 vars:
  525. //
  526. // colStart, rowStart, colEnd, rowEnd, x1, y1, x2, y2.
  527. //
  528. // We also calculate the absolute x and y position of the top left vertex of
  529. // the object. This is required for images.
  530. //
  531. // The width and height of the cells that the object occupies can be
  532. // variable and have to be taken into account.
  533. //
  534. // The values of col_start and row_start are passed in from the calling
  535. // function. The values of col_end and row_end are calculated by
  536. // subtracting the width and height of the object from the width and
  537. // height of the underlying cells.
  538. //
  539. // colStart # Col containing upper left corner of object.
  540. // x1 # Distance to left side of object.
  541. //
  542. // rowStart # Row containing top left corner of object.
  543. // y1 # Distance to top of object.
  544. //
  545. // colEnd # Col containing lower right corner of object.
  546. // x2 # Distance to right side of object.
  547. //
  548. // rowEnd # Row containing bottom right corner of object.
  549. // y2 # Distance to bottom of object.
  550. //
  551. // width # Width of object frame.
  552. // height # Height of object frame.
  553. //
  554. func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, height int) (int, int, int, int, int, int) {
  555. // Adjust start column for offsets that are greater than the col width.
  556. for x1 >= f.getColWidth(sheet, col) {
  557. x1 -= f.getColWidth(sheet, col)
  558. col++
  559. }
  560. // Adjust start row for offsets that are greater than the row height.
  561. for y1 >= f.getRowHeight(sheet, row) {
  562. y1 -= f.getRowHeight(sheet, row)
  563. row++
  564. }
  565. // Initialise end cell to the same as the start cell.
  566. colEnd := col
  567. rowEnd := row
  568. width += x1
  569. height += y1
  570. // Subtract the underlying cell widths to find end cell of the object.
  571. for width >= f.getColWidth(sheet, colEnd+1) {
  572. colEnd++
  573. width -= f.getColWidth(sheet, colEnd)
  574. }
  575. // Subtract the underlying cell heights to find end cell of the object.
  576. for height >= f.getRowHeight(sheet, rowEnd+1) {
  577. rowEnd++
  578. height -= f.getRowHeight(sheet, rowEnd)
  579. }
  580. // The end vertices are whatever is left from the width and height.
  581. x2 := width
  582. y2 := height
  583. return col, row, colEnd, rowEnd, x2, y2
  584. }
  585. // getColWidth provides a function to get column width in pixels by given
  586. // sheet name and column number.
  587. func (f *File) getColWidth(sheet string, col int) int {
  588. xlsx, _ := f.workSheetReader(sheet)
  589. if xlsx.Cols != nil {
  590. var width float64
  591. for _, v := range xlsx.Cols.Col {
  592. if v.Min <= col && col <= v.Max {
  593. width = v.Width
  594. }
  595. }
  596. if width != 0 {
  597. return int(convertColWidthToPixels(width))
  598. }
  599. }
  600. // Optimisation for when the column widths haven't changed.
  601. return int(defaultColWidthPixels)
  602. }
  603. // GetColWidth provides a function to get column width by given worksheet name
  604. // and column name.
  605. func (f *File) GetColWidth(sheet, col string) (float64, error) {
  606. colNum, err := ColumnNameToNumber(col)
  607. if err != nil {
  608. return defaultColWidth, err
  609. }
  610. ws, err := f.workSheetReader(sheet)
  611. if err != nil {
  612. return defaultColWidth, err
  613. }
  614. if ws.Cols != nil {
  615. var width float64
  616. for _, v := range ws.Cols.Col {
  617. if v.Min <= colNum && colNum <= v.Max {
  618. width = v.Width
  619. }
  620. }
  621. if width != 0 {
  622. return width, err
  623. }
  624. }
  625. // Optimisation for when the column widths haven't changed.
  626. return defaultColWidth, err
  627. }
  628. // InsertCol provides a function to insert a new column before given column
  629. // index. For example, create a new column before column C in Sheet1:
  630. //
  631. // err := f.InsertCol("Sheet1", "C")
  632. //
  633. func (f *File) InsertCol(sheet, col string) error {
  634. num, err := ColumnNameToNumber(col)
  635. if err != nil {
  636. return err
  637. }
  638. return f.adjustHelper(sheet, columns, num, 1)
  639. }
  640. // RemoveCol provides a function to remove single column by given worksheet
  641. // name and column index. For example, remove column C in Sheet1:
  642. //
  643. // err := f.RemoveCol("Sheet1", "C")
  644. //
  645. // Use this method with caution, which will affect changes in references such
  646. // as formulas, charts, and so on. If there is any referenced value of the
  647. // worksheet, it will cause a file error when you open it. The excelize only
  648. // partially updates these references currently.
  649. func (f *File) RemoveCol(sheet, col string) error {
  650. num, err := ColumnNameToNumber(col)
  651. if err != nil {
  652. return err
  653. }
  654. ws, err := f.workSheetReader(sheet)
  655. if err != nil {
  656. return err
  657. }
  658. for rowIdx := range ws.SheetData.Row {
  659. rowData := &ws.SheetData.Row[rowIdx]
  660. for colIdx := range rowData.C {
  661. colName, _, _ := SplitCellName(rowData.C[colIdx].R)
  662. if colName == col {
  663. rowData.C = append(rowData.C[:colIdx], rowData.C[colIdx+1:]...)[:len(rowData.C)-1]
  664. break
  665. }
  666. }
  667. }
  668. return f.adjustHelper(sheet, columns, num, -1)
  669. }
  670. // convertColWidthToPixels provieds function to convert the width of a cell
  671. // from user's units to pixels. Excel rounds the column width to the nearest
  672. // pixel. If the width hasn't been set by the user we use the default value.
  673. // If the column is hidden it has a value of zero.
  674. func convertColWidthToPixels(width float64) float64 {
  675. var padding float64 = 5
  676. var pixels float64
  677. var maxDigitWidth float64 = 7
  678. if width == 0 {
  679. return pixels
  680. }
  681. if width < 1 {
  682. pixels = (width * 12) + 0.5
  683. return math.Ceil(pixels)
  684. }
  685. pixels = (width*maxDigitWidth + 0.5) + padding
  686. return math.Ceil(pixels)
  687. }