rows.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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.Count == 0 {
  306. sharedStrings.Count = len(sharedStrings.SI)
  307. }
  308. if sharedStrings.UniqueCount == 0 {
  309. sharedStrings.UniqueCount = sharedStrings.Count
  310. }
  311. f.SharedStrings = &sharedStrings
  312. for i := range sharedStrings.SI {
  313. if sharedStrings.SI[i].T != nil {
  314. f.sharedStringsMap[sharedStrings.SI[i].T.Val] = i
  315. }
  316. }
  317. f.addContentTypePart(0, "sharedStrings")
  318. rels := f.relsReader(relPath)
  319. for _, rel := range rels.Relationships {
  320. if rel.Target == "/xl/sharedStrings.xml" {
  321. return f.SharedStrings
  322. }
  323. }
  324. // Update workbook.xml.rels
  325. f.addRels(relPath, SourceRelationshipSharedStrings, "/xl/sharedStrings.xml", "")
  326. }
  327. return f.SharedStrings
  328. }
  329. // getValueFrom return a value from a column/row cell, this function is
  330. // inteded to be used with for range on rows an argument with the spreadsheet
  331. // opened file.
  332. func (c *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) {
  333. f.Lock()
  334. defer f.Unlock()
  335. switch c.T {
  336. case "s":
  337. if c.V != "" {
  338. xlsxSI := 0
  339. xlsxSI, _ = strconv.Atoi(c.V)
  340. if len(d.SI) > xlsxSI {
  341. return f.formattedValue(c.S, d.SI[xlsxSI].String()), nil
  342. }
  343. }
  344. return f.formattedValue(c.S, c.V), nil
  345. case "str":
  346. return f.formattedValue(c.S, c.V), nil
  347. case "inlineStr":
  348. if c.IS != nil {
  349. return f.formattedValue(c.S, c.IS.String()), nil
  350. }
  351. return f.formattedValue(c.S, c.V), nil
  352. default:
  353. isNum, precision := isNumeric(c.V)
  354. if isNum && precision > 15 {
  355. val, _ := roundPrecision(c.V)
  356. if val != c.V {
  357. return f.formattedValue(c.S, val), nil
  358. }
  359. }
  360. return f.formattedValue(c.S, c.V), nil
  361. }
  362. }
  363. // roundPrecision round precision for numeric.
  364. func roundPrecision(value string) (result string, err error) {
  365. var num float64
  366. if num, err = strconv.ParseFloat(value, 64); err != nil {
  367. return
  368. }
  369. result = fmt.Sprintf("%g", math.Round(num*numericPrecision)/numericPrecision)
  370. return
  371. }
  372. // SetRowVisible provides a function to set visible of a single row by given
  373. // worksheet name and Excel row number. For example, hide row 2 in Sheet1:
  374. //
  375. // err := f.SetRowVisible("Sheet1", 2, false)
  376. //
  377. func (f *File) SetRowVisible(sheet string, row int, visible bool) error {
  378. if row < 1 {
  379. return newInvalidRowNumberError(row)
  380. }
  381. ws, err := f.workSheetReader(sheet)
  382. if err != nil {
  383. return err
  384. }
  385. prepareSheetXML(ws, 0, row)
  386. ws.SheetData.Row[row-1].Hidden = !visible
  387. return nil
  388. }
  389. // GetRowVisible provides a function to get visible of a single row by given
  390. // worksheet name and Excel row number. For example, get visible state of row
  391. // 2 in Sheet1:
  392. //
  393. // visible, err := f.GetRowVisible("Sheet1", 2)
  394. //
  395. func (f *File) GetRowVisible(sheet string, row int) (bool, error) {
  396. if row < 1 {
  397. return false, newInvalidRowNumberError(row)
  398. }
  399. ws, err := f.workSheetReader(sheet)
  400. if err != nil {
  401. return false, err
  402. }
  403. if row > len(ws.SheetData.Row) {
  404. return false, nil
  405. }
  406. return !ws.SheetData.Row[row-1].Hidden, nil
  407. }
  408. // SetRowOutlineLevel provides a function to set outline level number of a
  409. // single row by given worksheet name and Excel row number. The value of
  410. // parameter 'level' is 1-7. For example, outline row 2 in Sheet1 to level 1:
  411. //
  412. // err := f.SetRowOutlineLevel("Sheet1", 2, 1)
  413. //
  414. func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error {
  415. if row < 1 {
  416. return newInvalidRowNumberError(row)
  417. }
  418. if level > 7 || level < 1 {
  419. return ErrOutlineLevel
  420. }
  421. ws, err := f.workSheetReader(sheet)
  422. if err != nil {
  423. return err
  424. }
  425. prepareSheetXML(ws, 0, row)
  426. ws.SheetData.Row[row-1].OutlineLevel = level
  427. return nil
  428. }
  429. // GetRowOutlineLevel provides a function to get outline level number of a
  430. // single row by given worksheet name and Excel row number. For example, get
  431. // outline number of row 2 in Sheet1:
  432. //
  433. // level, err := f.GetRowOutlineLevel("Sheet1", 2)
  434. //
  435. func (f *File) GetRowOutlineLevel(sheet string, row int) (uint8, error) {
  436. if row < 1 {
  437. return 0, newInvalidRowNumberError(row)
  438. }
  439. ws, err := f.workSheetReader(sheet)
  440. if err != nil {
  441. return 0, err
  442. }
  443. if row > len(ws.SheetData.Row) {
  444. return 0, nil
  445. }
  446. return ws.SheetData.Row[row-1].OutlineLevel, nil
  447. }
  448. // RemoveRow provides a function to remove single row by given worksheet name
  449. // and Excel row number. For example, remove row 3 in Sheet1:
  450. //
  451. // err := f.RemoveRow("Sheet1", 3)
  452. //
  453. // Use this method with caution, which will affect changes in references such
  454. // as formulas, charts, and so on. If there is any referenced value of the
  455. // worksheet, it will cause a file error when you open it. The excelize only
  456. // partially updates these references currently.
  457. func (f *File) RemoveRow(sheet string, row int) error {
  458. if row < 1 {
  459. return newInvalidRowNumberError(row)
  460. }
  461. ws, err := f.workSheetReader(sheet)
  462. if err != nil {
  463. return err
  464. }
  465. if row > len(ws.SheetData.Row) {
  466. return f.adjustHelper(sheet, rows, row, -1)
  467. }
  468. keep := 0
  469. for rowIdx := 0; rowIdx < len(ws.SheetData.Row); rowIdx++ {
  470. v := &ws.SheetData.Row[rowIdx]
  471. if v.R != row {
  472. ws.SheetData.Row[keep] = *v
  473. keep++
  474. }
  475. }
  476. ws.SheetData.Row = ws.SheetData.Row[:keep]
  477. return f.adjustHelper(sheet, rows, row, -1)
  478. }
  479. // InsertRow provides a function to insert a new row after given Excel row
  480. // number starting from 1. For example, create a new row before row 3 in
  481. // Sheet1:
  482. //
  483. // err := f.InsertRow("Sheet1", 3)
  484. //
  485. // Use this method with caution, which will affect changes in references such
  486. // as formulas, charts, and so on. If there is any referenced value of the
  487. // worksheet, it will cause a file error when you open it. The excelize only
  488. // partially updates these references currently.
  489. func (f *File) InsertRow(sheet string, row int) error {
  490. if row < 1 {
  491. return newInvalidRowNumberError(row)
  492. }
  493. return f.adjustHelper(sheet, rows, row, 1)
  494. }
  495. // DuplicateRow inserts a copy of specified row (by its Excel row number) below
  496. //
  497. // err := f.DuplicateRow("Sheet1", 2)
  498. //
  499. // Use this method with caution, which will affect changes in references such
  500. // as formulas, charts, and so on. If there is any referenced value of the
  501. // worksheet, it will cause a file error when you open it. The excelize only
  502. // partially updates these references currently.
  503. func (f *File) DuplicateRow(sheet string, row int) error {
  504. return f.DuplicateRowTo(sheet, row, row+1)
  505. }
  506. // DuplicateRowTo inserts a copy of specified row by it Excel number
  507. // to specified row position moving down exists rows after target position
  508. //
  509. // err := f.DuplicateRowTo("Sheet1", 2, 7)
  510. //
  511. // Use this method with caution, which will affect changes in references such
  512. // as formulas, charts, and so on. If there is any referenced value of the
  513. // worksheet, it will cause a file error when you open it. The excelize only
  514. // partially updates these references currently.
  515. func (f *File) DuplicateRowTo(sheet string, row, row2 int) error {
  516. if row < 1 {
  517. return newInvalidRowNumberError(row)
  518. }
  519. ws, err := f.workSheetReader(sheet)
  520. if err != nil {
  521. return err
  522. }
  523. if row > len(ws.SheetData.Row) || row2 < 1 || row == row2 {
  524. return nil
  525. }
  526. var ok bool
  527. var rowCopy xlsxRow
  528. for i, r := range ws.SheetData.Row {
  529. if r.R == row {
  530. rowCopy = deepcopy.Copy(ws.SheetData.Row[i]).(xlsxRow)
  531. ok = true
  532. break
  533. }
  534. }
  535. if !ok {
  536. return nil
  537. }
  538. if err := f.adjustHelper(sheet, rows, row2, 1); err != nil {
  539. return err
  540. }
  541. idx2 := -1
  542. for i, r := range ws.SheetData.Row {
  543. if r.R == row2 {
  544. idx2 = i
  545. break
  546. }
  547. }
  548. if idx2 == -1 && len(ws.SheetData.Row) >= row2 {
  549. return nil
  550. }
  551. rowCopy.C = append(make([]xlsxC, 0, len(rowCopy.C)), rowCopy.C...)
  552. f.ajustSingleRowDimensions(&rowCopy, row2)
  553. if idx2 != -1 {
  554. ws.SheetData.Row[idx2] = rowCopy
  555. } else {
  556. ws.SheetData.Row = append(ws.SheetData.Row, rowCopy)
  557. }
  558. return f.duplicateMergeCells(sheet, ws, row, row2)
  559. }
  560. // duplicateMergeCells merge cells in the destination row if there are single
  561. // row merged cells in the copied row.
  562. func (f *File) duplicateMergeCells(sheet string, ws *xlsxWorksheet, row, row2 int) error {
  563. if ws.MergeCells == nil {
  564. return nil
  565. }
  566. if row > row2 {
  567. row++
  568. }
  569. for _, rng := range ws.MergeCells.Cells {
  570. coordinates, err := f.areaRefToCoordinates(rng.Ref)
  571. if err != nil {
  572. return err
  573. }
  574. if coordinates[1] < row2 && row2 < coordinates[3] {
  575. return nil
  576. }
  577. }
  578. for i := 0; i < len(ws.MergeCells.Cells); i++ {
  579. areaData := ws.MergeCells.Cells[i]
  580. coordinates, _ := f.areaRefToCoordinates(areaData.Ref)
  581. x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  582. if y1 == y2 && y1 == row {
  583. from, _ := CoordinatesToCellName(x1, row2)
  584. to, _ := CoordinatesToCellName(x2, row2)
  585. if err := f.MergeCell(sheet, from, to); err != nil {
  586. return err
  587. }
  588. }
  589. }
  590. return nil
  591. }
  592. // checkRow provides a function to check and fill each column element for all
  593. // rows and make that is continuous in a worksheet of XML. For example:
  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="F15" s="1" />
  599. // <c r="G15" s="1" />
  600. // </row>
  601. //
  602. // in this case, we should to change it to
  603. //
  604. // <row r="15" spans="1:22" x14ac:dyDescent="0.2">
  605. // <c r="A15" s="2" />
  606. // <c r="B15" s="2" />
  607. // <c r="C15" s="2" />
  608. // <c r="D15" s="2" />
  609. // <c r="E15" s="2" />
  610. // <c r="F15" s="1" />
  611. // <c r="G15" s="1" />
  612. // </row>
  613. //
  614. // Noteice: this method could be very slow for large spreadsheets (more than
  615. // 3000 rows one sheet).
  616. func checkRow(ws *xlsxWorksheet) error {
  617. for rowIdx := range ws.SheetData.Row {
  618. rowData := &ws.SheetData.Row[rowIdx]
  619. colCount := len(rowData.C)
  620. if colCount == 0 {
  621. continue
  622. }
  623. // check and fill the cell without r attribute in a row element
  624. rCount := 0
  625. for idx, cell := range rowData.C {
  626. rCount++
  627. if cell.R != "" {
  628. lastR, _, err := CellNameToCoordinates(cell.R)
  629. if err != nil {
  630. return err
  631. }
  632. if lastR > rCount {
  633. rCount = lastR
  634. }
  635. continue
  636. }
  637. rowData.C[idx].R, _ = CoordinatesToCellName(rCount, rowIdx+1)
  638. }
  639. lastCol, _, err := CellNameToCoordinates(rowData.C[colCount-1].R)
  640. if err != nil {
  641. return err
  642. }
  643. if colCount < lastCol {
  644. oldList := rowData.C
  645. newlist := make([]xlsxC, 0, lastCol)
  646. rowData.C = ws.SheetData.Row[rowIdx].C[:0]
  647. for colIdx := 0; colIdx < lastCol; colIdx++ {
  648. cellName, err := CoordinatesToCellName(colIdx+1, rowIdx+1)
  649. if err != nil {
  650. return err
  651. }
  652. newlist = append(newlist, xlsxC{R: cellName})
  653. }
  654. rowData.C = newlist
  655. for colIdx := range oldList {
  656. colData := &oldList[colIdx]
  657. colNum, _, err := CellNameToCoordinates(colData.R)
  658. if err != nil {
  659. return err
  660. }
  661. ws.SheetData.Row[rowIdx].C[colNum-1] = *colData
  662. }
  663. }
  664. }
  665. return nil
  666. }
  667. // convertRowHeightToPixels provides a function to convert the height of a
  668. // cell from user's units to pixels. If the height hasn't been set by the user
  669. // we use the default value. If the row is hidden it has a value of zero.
  670. func convertRowHeightToPixels(height float64) float64 {
  671. var pixels float64
  672. if height == 0 {
  673. return pixels
  674. }
  675. pixels = math.Ceil(4.0 / 3.0 * height)
  676. return pixels
  677. }