lib.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package excelize
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "encoding/gob"
  6. "io"
  7. "log"
  8. "math"
  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]string, int, error) {
  13. fileList := make(map[string]string)
  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 function to read XML content as string.
  26. func (f *File) readXML(name string) string {
  27. if content, ok := f.XLSX[name]; ok {
  28. return content
  29. }
  30. return ""
  31. }
  32. // saveFileList provides function to update given file content in file list of
  33. // XLSX.
  34. func (f *File) saveFileList(name, content string) {
  35. f.XLSX[name] = XMLHeader + content
  36. }
  37. // Read file content as string in a archive file.
  38. func readFile(file *zip.File) string {
  39. rc, err := file.Open()
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. buff := bytes.NewBuffer(nil)
  44. io.Copy(buff, rc)
  45. rc.Close()
  46. return string(buff.Bytes())
  47. }
  48. // ToAlphaString provides function to convert integer to Excel sheet column
  49. // title. For example convert 36 to column title AK:
  50. //
  51. // excelize.ToAlphaString(36)
  52. //
  53. func ToAlphaString(value int) string {
  54. if value < 0 {
  55. return ""
  56. }
  57. var ans string
  58. i := value + 1
  59. for i > 0 {
  60. ans = string((i-1)%26+65) + ans
  61. i = (i - 1) / 26
  62. }
  63. return ans
  64. }
  65. // TitleToNumber provides function to convert Excel sheet column title to int
  66. // (this function doesn't do value check currently). For example convert AK
  67. // and ak to column title 36:
  68. //
  69. // excelize.TitleToNumber("AK")
  70. // excelize.TitleToNumber("ak")
  71. //
  72. func TitleToNumber(s string) int {
  73. weight := 0.0
  74. sum := 0
  75. for i := len(s) - 1; i >= 0; i-- {
  76. ch := int(s[i])
  77. if int(s[i]) >= int('a') && int(s[i]) <= int('z') {
  78. ch = int(s[i]) - 32
  79. }
  80. sum = sum + (ch-int('A')+1)*int(math.Pow(26, weight))
  81. weight++
  82. }
  83. return sum - 1
  84. }
  85. // letterOnlyMapF is used in conjunction with strings.Map to return only the
  86. // characters A-Z and a-z in a string.
  87. func letterOnlyMapF(rune rune) rune {
  88. switch {
  89. case 'A' <= rune && rune <= 'Z':
  90. return rune
  91. case 'a' <= rune && rune <= 'z':
  92. return rune - 32
  93. }
  94. return -1
  95. }
  96. // intOnlyMapF is used in conjunction with strings.Map to return only the
  97. // numeric portions of a string.
  98. func intOnlyMapF(rune rune) rune {
  99. if rune >= 48 && rune < 58 {
  100. return rune
  101. }
  102. return -1
  103. }
  104. // deepCopy provides method to creates a deep copy of whatever is passed to it
  105. // and returns the copy in an interface. The returned value will need to be
  106. // asserted to the correct type.
  107. func deepCopy(dst, src interface{}) error {
  108. var buf bytes.Buffer
  109. if err := gob.NewEncoder(&buf).Encode(src); err != nil {
  110. return err
  111. }
  112. return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
  113. }