rows.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package excelize
  2. import (
  3. "encoding/xml"
  4. "strconv"
  5. "strings"
  6. )
  7. // GetRows return all the rows in a sheet by given "sheet" + index. For now you
  8. // should use sheet_name like "sheet3" where "sheet" is a constant part and "3"
  9. // is a sheet number. For example, if sheet named as "SomeUniqueData" and it is
  10. // second if spreadsheet program interface - you should use "sheet2" here. For
  11. // example:
  12. //
  13. // index := xlsx.GetSheetIndex("Sheet2")
  14. // rows := xlsx.GetRows("sheet" + strconv.Itoa(index))
  15. // for _, row := range rows {
  16. // for _, colCell := range row {
  17. // fmt.Print(colCell, "\t")
  18. // }
  19. // fmt.Println()
  20. // }
  21. //
  22. func (f *File) GetRows(sheet string) [][]string {
  23. xlsx := f.workSheetReader(sheet)
  24. rows := [][]string{}
  25. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  26. if xlsx != nil {
  27. output, _ := xml.Marshal(f.Sheet[name])
  28. f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
  29. }
  30. decoder := xml.NewDecoder(strings.NewReader(f.readXML(name)))
  31. d, _ := readXMLSST(f)
  32. var inElement string
  33. var r xlsxRow
  34. var row []string
  35. tr, tc := f.getTotalRowsCols(sheet)
  36. for i := 0; i < tr; i++ {
  37. row = []string{}
  38. for j := 0; j <= tc; j++ {
  39. row = append(row, "")
  40. }
  41. rows = append(rows, row)
  42. }
  43. decoder = xml.NewDecoder(strings.NewReader(f.readXML(name)))
  44. for {
  45. token, _ := decoder.Token()
  46. if token == nil {
  47. break
  48. }
  49. switch startElement := token.(type) {
  50. case xml.StartElement:
  51. inElement = startElement.Name.Local
  52. if inElement == "row" {
  53. r = xlsxRow{}
  54. decoder.DecodeElement(&r, &startElement)
  55. cr := r.R - 1
  56. for _, colCell := range r.C {
  57. c := titleToNumber(strings.Map(letterOnlyMapF, colCell.R))
  58. val, _ := colCell.getValueFrom(f, d)
  59. rows[cr][c] = val
  60. }
  61. }
  62. default:
  63. }
  64. }
  65. return rows
  66. }
  67. // getTotalRowsCols provides a function to get total columns and rows in a
  68. // sheet.
  69. func (f *File) getTotalRowsCols(sheet string) (int, int) {
  70. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  71. decoder := xml.NewDecoder(strings.NewReader(f.readXML(name)))
  72. var inElement string
  73. var r xlsxRow
  74. var tr, tc int
  75. for {
  76. token, _ := decoder.Token()
  77. if token == nil {
  78. break
  79. }
  80. switch startElement := token.(type) {
  81. case xml.StartElement:
  82. inElement = startElement.Name.Local
  83. if inElement == "row" {
  84. r = xlsxRow{}
  85. decoder.DecodeElement(&r, &startElement)
  86. tr = r.R
  87. for _, colCell := range r.C {
  88. col := titleToNumber(strings.Map(letterOnlyMapF, colCell.R))
  89. if col > tc {
  90. tc = col
  91. }
  92. }
  93. }
  94. default:
  95. }
  96. }
  97. return tr, tc
  98. }
  99. // SetRowHeight provides a function to set the height of a single row.
  100. // For example:
  101. //
  102. // xlsx := excelize.CreateFile()
  103. // xlsx.SetRowHeight("Sheet1", 0, 50)
  104. // err := xlsx.Save()
  105. // if err != nil {
  106. // fmt.Println(err)
  107. // os.Exit(1)
  108. // }
  109. //
  110. func (f *File) SetRowHeight(sheet string, rowIndex int, height float64) {
  111. xlsx := f.workSheetReader(sheet)
  112. rows := rowIndex + 1
  113. cells := 0
  114. completeRow(xlsx, rows, cells)
  115. xlsx.SheetData.Row[rowIndex].Ht = strconv.FormatFloat(height, 'f', -1, 64)
  116. xlsx.SheetData.Row[rowIndex].CustomHeight = true
  117. }
  118. // readXMLSST read xmlSST simple function.
  119. func readXMLSST(f *File) (*xlsxSST, error) {
  120. shardStrings := xlsxSST{}
  121. err := xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
  122. return &shardStrings, err
  123. }
  124. // getValueFrom return a value from a column/row cell, this function is inteded
  125. // to be used with for range on rows an argument with the xlsx opened file.
  126. func (xlsx *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) {
  127. switch xlsx.T {
  128. case "s":
  129. xlsxSI := 0
  130. xlsxSI, _ = strconv.Atoi(xlsx.V)
  131. if len(d.SI[xlsxSI].R) > 0 {
  132. value := ""
  133. for _, v := range d.SI[xlsxSI].R {
  134. value += v.T
  135. }
  136. return value, nil
  137. }
  138. return f.formattedValue(xlsx.S, d.SI[xlsxSI].T), nil
  139. case "str":
  140. return f.formattedValue(xlsx.S, xlsx.V), nil
  141. default:
  142. return f.formattedValue(xlsx.S, xlsx.V), nil
  143. }
  144. }
  145. // SetRowVisible provides a function to set visible of a single row by given
  146. // worksheet index and row index. For example, hide row 3 in Sheet1:
  147. //
  148. // xlsx.SetRowVisible("Sheet1", 2, false)
  149. //
  150. func (f *File) SetRowVisible(sheet string, rowIndex int, visible bool) {
  151. xlsx := f.workSheetReader(sheet)
  152. rows := rowIndex + 1
  153. cells := 0
  154. completeRow(xlsx, rows, cells)
  155. if visible {
  156. xlsx.SheetData.Row[rowIndex].Hidden = false
  157. return
  158. }
  159. xlsx.SheetData.Row[rowIndex].Hidden = true
  160. }
  161. // GetRowVisible provides a function to get visible of a single row by given
  162. // worksheet index and row index. For example, get visible state of row 3 in
  163. // Sheet1:
  164. //
  165. // xlsx.GetRowVisible("Sheet1", 2)
  166. //
  167. func (f *File) GetRowVisible(sheet string, rowIndex int) bool {
  168. xlsx := f.workSheetReader(sheet)
  169. rows := rowIndex + 1
  170. cells := 0
  171. completeRow(xlsx, rows, cells)
  172. return !xlsx.SheetData.Row[rowIndex].Hidden
  173. }