lib.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package excelize
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "io"
  6. "log"
  7. "math"
  8. "unicode"
  9. )
  10. // ReadZipReader can be used to read an XLSX in memory without touching the
  11. // filesystem.
  12. func ReadZipReader(r *zip.Reader) (map[string][]byte, int, error) {
  13. fileList := make(map[string][]byte)
  14. worksheets := 0
  15. for _, v := range r.File {
  16. fileList[v.Name] = readFile(v)
  17. if len(v.Name) > 18 {
  18. if v.Name[0:19] == "xl/worksheets/sheet" {
  19. worksheets++
  20. }
  21. }
  22. }
  23. return fileList, worksheets, nil
  24. }
  25. // readXML provides a function to read XML content as string.
  26. func (f *File) readXML(name string) []byte {
  27. if content, ok := f.XLSX[name]; ok {
  28. return content
  29. }
  30. return []byte{}
  31. }
  32. // saveFileList provides a function to update given file content in file list
  33. // of XLSX.
  34. func (f *File) saveFileList(name string, content []byte) {
  35. newContent := make([]byte, 0, len(XMLHeader)+len(content))
  36. newContent = append(newContent, []byte(XMLHeader)...)
  37. newContent = append(newContent, content...)
  38. f.XLSX[name] = newContent
  39. }
  40. // Read file content as string in a archive file.
  41. func readFile(file *zip.File) []byte {
  42. rc, err := file.Open()
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. buff := bytes.NewBuffer(nil)
  47. _, _ = io.Copy(buff, rc)
  48. rc.Close()
  49. return buff.Bytes()
  50. }
  51. // ToAlphaString provides a function to convert integer to Excel sheet column
  52. // title. For example convert 36 to column title AK:
  53. //
  54. // excelize.ToAlphaString(36)
  55. //
  56. func ToAlphaString(value int) string {
  57. if value < 0 {
  58. return ""
  59. }
  60. var ans string
  61. i := value + 1
  62. for i > 0 {
  63. ans = string((i-1)%26+65) + ans
  64. i = (i - 1) / 26
  65. }
  66. return ans
  67. }
  68. // TitleToNumber provides a function to convert Excel sheet column title to
  69. // int (this function doesn't do value check currently). For example convert
  70. // AK and ak to column title 36:
  71. //
  72. // excelize.TitleToNumber("AK")
  73. // excelize.TitleToNumber("ak")
  74. //
  75. func TitleToNumber(s string) int {
  76. weight := 0.0
  77. sum := 0
  78. for i := len(s) - 1; i >= 0; i-- {
  79. ch := int(s[i])
  80. if int(s[i]) >= int('a') && int(s[i]) <= int('z') {
  81. ch = int(s[i]) - 32
  82. }
  83. sum = sum + (ch-int('A')+1)*int(math.Pow(26, weight))
  84. weight++
  85. }
  86. return sum - 1
  87. }
  88. // letterOnlyMapF is used in conjunction with strings.Map to return only the
  89. // characters A-Z and a-z in a string.
  90. func letterOnlyMapF(rune rune) rune {
  91. switch {
  92. case 'A' <= rune && rune <= 'Z':
  93. return rune
  94. case 'a' <= rune && rune <= 'z':
  95. return rune - 32
  96. }
  97. return -1
  98. }
  99. // intOnlyMapF is used in conjunction with strings.Map to return only the
  100. // numeric portions of a string.
  101. func intOnlyMapF(rune rune) rune {
  102. if rune >= 48 && rune < 58 {
  103. return rune
  104. }
  105. return -1
  106. }
  107. // boolPtr returns a pointer to a bool with the given value.
  108. func boolPtr(b bool) *bool { return &b }
  109. // defaultTrue returns true if b is nil, or the pointed value.
  110. func defaultTrue(b *bool) bool {
  111. if b == nil {
  112. return true
  113. }
  114. return *b
  115. }
  116. // axisLowerOrEqualThan returns true if axis1 <= axis2 axis1/axis2 can be
  117. // either a column or a row axis, e.g. "A", "AAE", "42", "1", etc.
  118. //
  119. // For instance, the following comparisons are all true:
  120. //
  121. // "A" <= "B"
  122. // "A" <= "AA"
  123. // "B" <= "AA"
  124. // "BC" <= "ABCD" (in a XLSX sheet, the BC col comes before the ABCD col)
  125. // "1" <= "2"
  126. // "2" <= "11" (in a XLSX sheet, the row 2 comes before the row 11)
  127. // and so on
  128. func axisLowerOrEqualThan(axis1, axis2 string) bool {
  129. if len(axis1) < len(axis2) {
  130. return true
  131. } else if len(axis1) > len(axis2) {
  132. return false
  133. } else {
  134. return axis1 <= axis2
  135. }
  136. }
  137. // getCellColRow returns the two parts of a cell identifier (its col and row)
  138. // as strings
  139. //
  140. // For instance:
  141. //
  142. // "C220" => "C", "220"
  143. // "aaef42" => "aaef", "42"
  144. // "" => "", ""
  145. func getCellColRow(cell string) (col, row string) {
  146. for index, rune := range cell {
  147. if unicode.IsDigit(rune) {
  148. return cell[:index], cell[index:]
  149. }
  150. }
  151. return cell, ""
  152. }
  153. // parseFormatSet provides a method to convert format string to []byte and
  154. // handle empty string.
  155. func parseFormatSet(formatSet string) []byte {
  156. if formatSet != "" {
  157. return []byte(formatSet)
  158. }
  159. return []byte("{}")
  160. }