rows.go 20 KB

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