rows.go 19 KB

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