lib.go 4.2 KB

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