lib.go 4.0 KB

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