rows.go 19 KB

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