rows.go 19 KB

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