lib.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 to
  67. // column title 36:
  68. //
  69. // excelize.TitleToNumber("AK")
  70. //
  71. func TitleToNumber(s string) int {
  72. weight := 0.0
  73. sum := 0
  74. for i := len(s) - 1; i >= 0; i-- {
  75. sum = sum + (int(s[i])-int('A')+1)*int(math.Pow(26, weight))
  76. weight++
  77. }
  78. return sum - 1
  79. }
  80. // letterOnlyMapF is used in conjunction with strings.Map to return only the
  81. // characters A-Z and a-z in a string.
  82. func letterOnlyMapF(rune rune) rune {
  83. switch {
  84. case 'A' <= rune && rune <= 'Z':
  85. return rune
  86. case 'a' <= rune && rune <= 'z':
  87. return rune - 32
  88. }
  89. return -1
  90. }
  91. // intOnlyMapF is used in conjunction with strings.Map to return only the
  92. // numeric portions of a string.
  93. func intOnlyMapF(rune rune) rune {
  94. if rune >= 48 && rune < 58 {
  95. return rune
  96. }
  97. return -1
  98. }
  99. // deepCopy provides method to creates a deep copy of whatever is passed to it
  100. // and returns the copy in an interface. The returned value will need to be
  101. // asserted to the correct type.
  102. func deepCopy(dst, src interface{}) error {
  103. var buf bytes.Buffer
  104. if err := gob.NewEncoder(&buf).Encode(src); err != nil {
  105. return err
  106. }
  107. return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
  108. }