rows.go 19 KB

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