rows.go 18 KB

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