rows.go 19 KB

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