rows.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. "fmt"
  17. "io"
  18. "log"
  19. "math"
  20. "strconv"
  21. )
  22. // GetRows return all the rows in a sheet by given worksheet name (case
  23. // sensitive). For example:
  24. //
  25. // rows, err := f.GetRows("Sheet1")
  26. // if err != nil {
  27. // fmt.Println(err)
  28. // return
  29. // }
  30. // for _, row := range rows {
  31. // for _, colCell := range row {
  32. // fmt.Print(colCell, "\t")
  33. // }
  34. // fmt.Println()
  35. // }
  36. //
  37. func (f *File) GetRows(sheet string) ([][]string, error) {
  38. rows, err := f.Rows(sheet)
  39. if err != nil {
  40. return nil, err
  41. }
  42. results := make([][]string, 0, 64)
  43. for rows.Next() {
  44. row, err := rows.Columns()
  45. if err != nil {
  46. break
  47. }
  48. results = append(results, row)
  49. }
  50. return results, nil
  51. }
  52. // Rows defines an iterator to a sheet.
  53. type Rows struct {
  54. err error
  55. curRow, totalRow, stashRow int
  56. sheet string
  57. rows []xlsxRow
  58. f *File
  59. decoder *xml.Decoder
  60. }
  61. // Next will return true if find the next row element.
  62. func (rows *Rows) Next() bool {
  63. rows.curRow++
  64. return rows.curRow <= rows.totalRow
  65. }
  66. // Error will return the error when the error occurs.
  67. func (rows *Rows) Error() error {
  68. return rows.err
  69. }
  70. // Columns return the current row's column values.
  71. func (rows *Rows) Columns() ([]string, error) {
  72. var (
  73. err error
  74. inElement string
  75. row, cellCol int
  76. columns []string
  77. )
  78. if rows.stashRow >= rows.curRow {
  79. return columns, err
  80. }
  81. d := rows.f.sharedStringsReader()
  82. for {
  83. token, _ := rows.decoder.Token()
  84. if token == nil {
  85. break
  86. }
  87. switch startElement := token.(type) {
  88. case xml.StartElement:
  89. inElement = startElement.Name.Local
  90. if inElement == "row" {
  91. for _, attr := range startElement.Attr {
  92. if attr.Name.Local == "r" {
  93. row, err = strconv.Atoi(attr.Value)
  94. if err != nil {
  95. return columns, err
  96. }
  97. if row > rows.curRow {
  98. rows.stashRow = row - 1
  99. return columns, err
  100. }
  101. }
  102. }
  103. }
  104. if inElement == "c" {
  105. cellCol++
  106. colCell := xlsxC{}
  107. _ = rows.decoder.DecodeElement(&colCell, &startElement)
  108. if colCell.R != "" {
  109. cellCol, _, err = CellNameToCoordinates(colCell.R)
  110. if err != nil {
  111. return columns, err
  112. }
  113. }
  114. blank := cellCol - len(columns)
  115. val, _ := colCell.getValueFrom(rows.f, d)
  116. columns = append(appendSpace(blank, columns), val)
  117. }
  118. case xml.EndElement:
  119. inElement = startElement.Name.Local
  120. if inElement == "row" {
  121. return columns, err
  122. }
  123. }
  124. }
  125. return columns, err
  126. }
  127. // appendSpace append blank characters to slice by given length and source slice.
  128. func appendSpace(l int, s []string) []string {
  129. for i := 1; i < l; i++ {
  130. s = append(s, "")
  131. }
  132. return s
  133. }
  134. // ErrSheetNotExist defines an error of sheet is not exist
  135. type ErrSheetNotExist struct {
  136. SheetName string
  137. }
  138. func (err ErrSheetNotExist) Error() string {
  139. return fmt.Sprintf("sheet %s is not exist", string(err.SheetName))
  140. }
  141. // Rows returns a rows iterator, used for streaming reading data for a
  142. // worksheet with a large data. For example:
  143. //
  144. // rows, err := f.Rows("Sheet1")
  145. // if err != nil {
  146. // fmt.Println(err)
  147. // return
  148. // }
  149. // for rows.Next() {
  150. // row, err := rows.Columns()
  151. // if err != nil {
  152. // fmt.Println(err)
  153. // }
  154. // for _, colCell := range row {
  155. // fmt.Print(colCell, "\t")
  156. // }
  157. // fmt.Println()
  158. // }
  159. //
  160. func (f *File) Rows(sheet string) (*Rows, error) {
  161. name, ok := f.sheetMap[trimSheetName(sheet)]
  162. if !ok {
  163. return nil, ErrSheetNotExist{sheet}
  164. }
  165. if f.Sheet[name] != nil {
  166. // flush data
  167. output, _ := xml.Marshal(f.Sheet[name])
  168. f.saveFileList(name, f.replaceNameSpaceBytes(name, output))
  169. }
  170. var (
  171. err error
  172. inElement string
  173. row int
  174. rows Rows
  175. )
  176. decoder := f.xmlNewDecoder(bytes.NewReader(f.readXML(name)))
  177. for {
  178. token, _ := decoder.Token()
  179. if token == nil {
  180. break
  181. }
  182. switch startElement := token.(type) {
  183. case xml.StartElement:
  184. inElement = startElement.Name.Local
  185. if inElement == "row" {
  186. row++
  187. for _, attr := range startElement.Attr {
  188. if attr.Name.Local == "r" {
  189. row, err = strconv.Atoi(attr.Value)
  190. if err != nil {
  191. return &rows, err
  192. }
  193. }
  194. }
  195. rows.totalRow = row
  196. }
  197. default:
  198. }
  199. }
  200. rows.f = f
  201. rows.sheet = name
  202. rows.decoder = f.xmlNewDecoder(bytes.NewReader(f.readXML(name)))
  203. return &rows, nil
  204. }
  205. // SetRowHeight provides a function to set the height of a single row. For
  206. // example, set the height of the first row in Sheet1:
  207. //
  208. // err := f.SetRowHeight("Sheet1", 1, 50)
  209. //
  210. func (f *File) SetRowHeight(sheet string, row int, height float64) error {
  211. if row < 1 {
  212. return newInvalidRowNumberError(row)
  213. }
  214. if height > MaxRowHeight {
  215. return errors.New("the height of the row must be smaller than or equal to 409 points")
  216. }
  217. ws, err := f.workSheetReader(sheet)
  218. if err != nil {
  219. return err
  220. }
  221. prepareSheetXML(ws, 0, row)
  222. rowIdx := row - 1
  223. ws.SheetData.Row[rowIdx].Ht = height
  224. ws.SheetData.Row[rowIdx].CustomHeight = true
  225. return nil
  226. }
  227. // getRowHeight provides a function to get row height in pixels by given sheet
  228. // name and row index.
  229. func (f *File) getRowHeight(sheet string, row int) int {
  230. ws, _ := f.workSheetReader(sheet)
  231. for i := range ws.SheetData.Row {
  232. v := &ws.SheetData.Row[i]
  233. if v.R == row+1 && v.Ht != 0 {
  234. return int(convertRowHeightToPixels(v.Ht))
  235. }
  236. }
  237. // Optimisation for when the row heights haven't changed.
  238. return int(defaultRowHeightPixels)
  239. }
  240. // GetRowHeight provides a function to get row height by given worksheet name
  241. // and row index. For example, get the height of the first row in Sheet1:
  242. //
  243. // height, err := f.GetRowHeight("Sheet1", 1)
  244. //
  245. func (f *File) GetRowHeight(sheet string, row int) (float64, error) {
  246. if row < 1 {
  247. return defaultRowHeightPixels, newInvalidRowNumberError(row)
  248. }
  249. var ht = defaultRowHeight
  250. ws, err := f.workSheetReader(sheet)
  251. if err != nil {
  252. return ht, err
  253. }
  254. if ws.SheetFormatPr != nil {
  255. ht = ws.SheetFormatPr.DefaultRowHeight
  256. }
  257. if row > len(ws.SheetData.Row) {
  258. return ht, nil // it will be better to use 0, but we take care with BC
  259. }
  260. for _, v := range ws.SheetData.Row {
  261. if v.R == row && v.Ht != 0 {
  262. return v.Ht, nil
  263. }
  264. }
  265. // Optimisation for when the row heights haven't changed.
  266. return ht, nil
  267. }
  268. // sharedStringsReader provides a function to get the pointer to the structure
  269. // after deserialization of xl/sharedStrings.xml.
  270. func (f *File) sharedStringsReader() *xlsxSST {
  271. var err error
  272. f.Lock()
  273. defer f.Unlock()
  274. relPath := f.getWorkbookRelsPath()
  275. if f.SharedStrings == nil {
  276. var sharedStrings xlsxSST
  277. ss := f.readXML("xl/sharedStrings.xml")
  278. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(ss))).
  279. Decode(&sharedStrings); err != nil && err != io.EOF {
  280. log.Printf("xml decode error: %s", err)
  281. }
  282. if sharedStrings.UniqueCount == 0 {
  283. sharedStrings.UniqueCount = sharedStrings.Count
  284. }
  285. f.SharedStrings = &sharedStrings
  286. for i := range sharedStrings.SI {
  287. if sharedStrings.SI[i].T != nil {
  288. f.sharedStringsMap[sharedStrings.SI[i].T.Val] = i
  289. }
  290. }
  291. f.addContentTypePart(0, "sharedStrings")
  292. rels := f.relsReader(relPath)
  293. for _, rel := range rels.Relationships {
  294. if rel.Target == "/xl/sharedStrings.xml" {
  295. return f.SharedStrings
  296. }
  297. }
  298. // Update workbook.xml.rels
  299. f.addRels(relPath, SourceRelationshipSharedStrings, "/xl/sharedStrings.xml", "")
  300. }
  301. return f.SharedStrings
  302. }
  303. // getValueFrom return a value from a column/row cell, this function is
  304. // inteded to be used with for range on rows an argument with the spreadsheet
  305. // opened file.
  306. func (c *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) {
  307. f.Lock()
  308. defer f.Unlock()
  309. switch c.T {
  310. case "s":
  311. if c.V != "" {
  312. xlsxSI := 0
  313. xlsxSI, _ = strconv.Atoi(c.V)
  314. if len(d.SI) > xlsxSI {
  315. return f.formattedValue(c.S, d.SI[xlsxSI].String()), nil
  316. }
  317. }
  318. return f.formattedValue(c.S, c.V), nil
  319. case "str":
  320. return f.formattedValue(c.S, c.V), nil
  321. case "inlineStr":
  322. if c.IS != nil {
  323. return f.formattedValue(c.S, c.IS.String()), nil
  324. }
  325. return f.formattedValue(c.S, c.V), nil
  326. default:
  327. // correct numeric values as legacy Excel app
  328. // https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel
  329. // In the top figure the fraction 1/9000 in Excel is displayed.
  330. // Although this number has a decimal representation that is an infinite string of ones,
  331. // Excel displays only the leading 15 figures. In the second line, the number one is added to the fraction, and again Excel displays only 15 figures.
  332. const precision = 1000000000000000
  333. if len(c.V) > 16 {
  334. num, err := strconv.ParseFloat(c.V, 64)
  335. if err != nil {
  336. return "", err
  337. }
  338. num = math.Round(num*precision) / precision
  339. val := fmt.Sprintf("%g", num)
  340. if val != c.V {
  341. return f.formattedValue(c.S, val), nil
  342. }
  343. }
  344. return f.formattedValue(c.S, c.V), nil
  345. }
  346. }
  347. // SetRowVisible provides a function to set visible of a single row by given
  348. // worksheet name and Excel row number. For example, hide row 2 in Sheet1:
  349. //
  350. // err := f.SetRowVisible("Sheet1", 2, false)
  351. //
  352. func (f *File) SetRowVisible(sheet string, row int, visible bool) error {
  353. if row < 1 {
  354. return newInvalidRowNumberError(row)
  355. }
  356. ws, err := f.workSheetReader(sheet)
  357. if err != nil {
  358. return err
  359. }
  360. prepareSheetXML(ws, 0, row)
  361. ws.SheetData.Row[row-1].Hidden = !visible
  362. return nil
  363. }
  364. // GetRowVisible provides a function to get visible of a single row by given
  365. // worksheet name and Excel row number. For example, get visible state of row
  366. // 2 in Sheet1:
  367. //
  368. // visible, err := f.GetRowVisible("Sheet1", 2)
  369. //
  370. func (f *File) GetRowVisible(sheet string, row int) (bool, error) {
  371. if row < 1 {
  372. return false, newInvalidRowNumberError(row)
  373. }
  374. ws, err := f.workSheetReader(sheet)
  375. if err != nil {
  376. return false, err
  377. }
  378. if row > len(ws.SheetData.Row) {
  379. return false, nil
  380. }
  381. return !ws.SheetData.Row[row-1].Hidden, nil
  382. }
  383. // SetRowOutlineLevel provides a function to set outline level number of a
  384. // single row by given worksheet name and Excel row number. The value of
  385. // parameter 'level' is 1-7. For example, outline row 2 in Sheet1 to level 1:
  386. //
  387. // err := f.SetRowOutlineLevel("Sheet1", 2, 1)
  388. //
  389. func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error {
  390. if row < 1 {
  391. return newInvalidRowNumberError(row)
  392. }
  393. if level > 7 || level < 1 {
  394. return errors.New("invalid outline level")
  395. }
  396. ws, err := f.workSheetReader(sheet)
  397. if err != nil {
  398. return err
  399. }
  400. prepareSheetXML(ws, 0, row)
  401. ws.SheetData.Row[row-1].OutlineLevel = level
  402. return nil
  403. }
  404. // GetRowOutlineLevel provides a function to get outline level number of a
  405. // single row by given worksheet name and Excel row number. For example, get
  406. // outline number of row 2 in Sheet1:
  407. //
  408. // level, err := f.GetRowOutlineLevel("Sheet1", 2)
  409. //
  410. func (f *File) GetRowOutlineLevel(sheet string, row int) (uint8, error) {
  411. if row < 1 {
  412. return 0, newInvalidRowNumberError(row)
  413. }
  414. ws, err := f.workSheetReader(sheet)
  415. if err != nil {
  416. return 0, err
  417. }
  418. if row > len(ws.SheetData.Row) {
  419. return 0, nil
  420. }
  421. return ws.SheetData.Row[row-1].OutlineLevel, nil
  422. }
  423. // RemoveRow provides a function to remove single row by given worksheet name
  424. // and Excel row number. For example, remove row 3 in Sheet1:
  425. //
  426. // err := f.RemoveRow("Sheet1", 3)
  427. //
  428. // Use this method with caution, which will affect changes in references such
  429. // as formulas, charts, and so on. If there is any referenced value of the
  430. // worksheet, it will cause a file error when you open it. The excelize only
  431. // partially updates these references currently.
  432. func (f *File) RemoveRow(sheet string, row int) error {
  433. if row < 1 {
  434. return newInvalidRowNumberError(row)
  435. }
  436. ws, err := f.workSheetReader(sheet)
  437. if err != nil {
  438. return err
  439. }
  440. if row > len(ws.SheetData.Row) {
  441. return f.adjustHelper(sheet, rows, row, -1)
  442. }
  443. keep := 0
  444. for rowIdx := 0; rowIdx < len(ws.SheetData.Row); rowIdx++ {
  445. v := &ws.SheetData.Row[rowIdx]
  446. if v.R != row {
  447. ws.SheetData.Row[keep] = *v
  448. keep++
  449. }
  450. }
  451. ws.SheetData.Row = ws.SheetData.Row[:keep]
  452. return f.adjustHelper(sheet, rows, row, -1)
  453. }
  454. // InsertRow provides a function to insert a new row after given Excel row
  455. // number starting from 1. For example, create a new row before row 3 in
  456. // Sheet1:
  457. //
  458. // err := f.InsertRow("Sheet1", 3)
  459. //
  460. // Use this method with caution, which will affect changes in references such
  461. // as formulas, charts, and so on. If there is any referenced value of the
  462. // worksheet, it will cause a file error when you open it. The excelize only
  463. // partially updates these references currently.
  464. func (f *File) InsertRow(sheet string, row int) error {
  465. if row < 1 {
  466. return newInvalidRowNumberError(row)
  467. }
  468. return f.adjustHelper(sheet, rows, row, 1)
  469. }
  470. // DuplicateRow inserts a copy of specified row (by its Excel row number) below
  471. //
  472. // err := f.DuplicateRow("Sheet1", 2)
  473. //
  474. // Use this method with caution, which will affect changes in references such
  475. // as formulas, charts, and so on. If there is any referenced value of the
  476. // worksheet, it will cause a file error when you open it. The excelize only
  477. // partially updates these references currently.
  478. func (f *File) DuplicateRow(sheet string, row int) error {
  479. return f.DuplicateRowTo(sheet, row, row+1)
  480. }
  481. // DuplicateRowTo inserts a copy of specified row by it Excel number
  482. // to specified row position moving down exists rows after target position
  483. //
  484. // err := f.DuplicateRowTo("Sheet1", 2, 7)
  485. //
  486. // Use this method with caution, which will affect changes in references such
  487. // as formulas, charts, and so on. If there is any referenced value of the
  488. // worksheet, it will cause a file error when you open it. The excelize only
  489. // partially updates these references currently.
  490. func (f *File) DuplicateRowTo(sheet string, row, row2 int) error {
  491. if row < 1 {
  492. return newInvalidRowNumberError(row)
  493. }
  494. ws, err := f.workSheetReader(sheet)
  495. if err != nil {
  496. return err
  497. }
  498. if row > len(ws.SheetData.Row) || row2 < 1 || row == row2 {
  499. return nil
  500. }
  501. var ok bool
  502. var rowCopy xlsxRow
  503. for i, r := range ws.SheetData.Row {
  504. if r.R == row {
  505. rowCopy = ws.SheetData.Row[i]
  506. ok = true
  507. break
  508. }
  509. }
  510. if !ok {
  511. return nil
  512. }
  513. if err := f.adjustHelper(sheet, rows, row2, 1); err != nil {
  514. return err
  515. }
  516. idx2 := -1
  517. for i, r := range ws.SheetData.Row {
  518. if r.R == row2 {
  519. idx2 = i
  520. break
  521. }
  522. }
  523. if idx2 == -1 && len(ws.SheetData.Row) >= row2 {
  524. return nil
  525. }
  526. rowCopy.C = append(make([]xlsxC, 0, len(rowCopy.C)), rowCopy.C...)
  527. f.ajustSingleRowDimensions(&rowCopy, row2)
  528. if idx2 != -1 {
  529. ws.SheetData.Row[idx2] = rowCopy
  530. } else {
  531. ws.SheetData.Row = append(ws.SheetData.Row, rowCopy)
  532. }
  533. return f.duplicateMergeCells(sheet, ws, row, row2)
  534. }
  535. // duplicateMergeCells merge cells in the destination row if there are single
  536. // row merged cells in the copied row.
  537. func (f *File) duplicateMergeCells(sheet string, ws *xlsxWorksheet, row, row2 int) error {
  538. if ws.MergeCells == nil {
  539. return nil
  540. }
  541. if row > row2 {
  542. row++
  543. }
  544. for _, rng := range ws.MergeCells.Cells {
  545. coordinates, err := f.areaRefToCoordinates(rng.Ref)
  546. if err != nil {
  547. return err
  548. }
  549. if coordinates[1] < row2 && row2 < coordinates[3] {
  550. return nil
  551. }
  552. }
  553. for i := 0; i < len(ws.MergeCells.Cells); i++ {
  554. areaData := ws.MergeCells.Cells[i]
  555. coordinates, _ := f.areaRefToCoordinates(areaData.Ref)
  556. x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  557. if y1 == y2 && y1 == row {
  558. from, _ := CoordinatesToCellName(x1, row2)
  559. to, _ := CoordinatesToCellName(x2, row2)
  560. if err := f.MergeCell(sheet, from, to); err != nil {
  561. return err
  562. }
  563. i++
  564. }
  565. }
  566. return nil
  567. }
  568. // checkRow provides a function to check and fill each column element for all
  569. // rows and make that is continuous in a worksheet of XML. For example:
  570. //
  571. // <row r="15" spans="1:22" x14ac:dyDescent="0.2">
  572. // <c r="A15" s="2" />
  573. // <c r="B15" s="2" />
  574. // <c r="F15" s="1" />
  575. // <c r="G15" s="1" />
  576. // </row>
  577. //
  578. // in this case, we should to change it to
  579. //
  580. // <row r="15" spans="1:22" x14ac:dyDescent="0.2">
  581. // <c r="A15" s="2" />
  582. // <c r="B15" s="2" />
  583. // <c r="C15" s="2" />
  584. // <c r="D15" s="2" />
  585. // <c r="E15" s="2" />
  586. // <c r="F15" s="1" />
  587. // <c r="G15" s="1" />
  588. // </row>
  589. //
  590. // Noteice: this method could be very slow for large spreadsheets (more than
  591. // 3000 rows one sheet).
  592. func checkRow(ws *xlsxWorksheet) error {
  593. for rowIdx := range ws.SheetData.Row {
  594. rowData := &ws.SheetData.Row[rowIdx]
  595. colCount := len(rowData.C)
  596. if colCount == 0 {
  597. continue
  598. }
  599. // check and fill the cell without r attribute in a row element
  600. rCount := 0
  601. for idx, cell := range rowData.C {
  602. rCount++
  603. if cell.R != "" {
  604. lastR, _, err := CellNameToCoordinates(cell.R)
  605. if err != nil {
  606. return err
  607. }
  608. if lastR > rCount {
  609. rCount = lastR
  610. }
  611. continue
  612. }
  613. rowData.C[idx].R, _ = CoordinatesToCellName(rCount, rowIdx+1)
  614. }
  615. lastCol, _, err := CellNameToCoordinates(rowData.C[colCount-1].R)
  616. if err != nil {
  617. return err
  618. }
  619. if colCount < lastCol {
  620. oldList := rowData.C
  621. newlist := make([]xlsxC, 0, lastCol)
  622. rowData.C = ws.SheetData.Row[rowIdx].C[:0]
  623. for colIdx := 0; colIdx < lastCol; colIdx++ {
  624. cellName, err := CoordinatesToCellName(colIdx+1, rowIdx+1)
  625. if err != nil {
  626. return err
  627. }
  628. newlist = append(newlist, xlsxC{R: cellName})
  629. }
  630. rowData.C = newlist
  631. for colIdx := range oldList {
  632. colData := &oldList[colIdx]
  633. colNum, _, err := CellNameToCoordinates(colData.R)
  634. if err != nil {
  635. return err
  636. }
  637. ws.SheetData.Row[rowIdx].C[colNum-1] = *colData
  638. }
  639. }
  640. }
  641. return nil
  642. }
  643. // convertRowHeightToPixels provides a function to convert the height of a
  644. // cell from user's units to pixels. If the height hasn't been set by the user
  645. // we use the default value. If the row is hidden it has a value of zero.
  646. func convertRowHeightToPixels(height float64) float64 {
  647. var pixels float64
  648. if height == 0 {
  649. return pixels
  650. }
  651. pixels = math.Ceil(4.0 / 3.0 * height)
  652. return pixels
  653. }