rows.go 19 KB

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