lib.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2016 - 2018 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.8 or later.
  9. package excelize
  10. import (
  11. "archive/zip"
  12. "bytes"
  13. "io"
  14. "log"
  15. "math"
  16. "unicode"
  17. )
  18. // ReadZipReader can be used to read an XLSX in memory without touching the
  19. // filesystem.
  20. func ReadZipReader(r *zip.Reader) (map[string][]byte, int, error) {
  21. fileList := make(map[string][]byte)
  22. worksheets := 0
  23. for _, v := range r.File {
  24. fileList[v.Name] = readFile(v)
  25. if len(v.Name) > 18 {
  26. if v.Name[0:19] == "xl/worksheets/sheet" {
  27. worksheets++
  28. }
  29. }
  30. }
  31. return fileList, worksheets, nil
  32. }
  33. // readXML provides a function to read XML content as string.
  34. func (f *File) readXML(name string) []byte {
  35. if content, ok := f.XLSX[name]; ok {
  36. return content
  37. }
  38. return []byte{}
  39. }
  40. // saveFileList provides a function to update given file content in file list
  41. // of XLSX.
  42. func (f *File) saveFileList(name string, content []byte) {
  43. newContent := make([]byte, 0, len(XMLHeader)+len(content))
  44. newContent = append(newContent, []byte(XMLHeader)...)
  45. newContent = append(newContent, content...)
  46. f.XLSX[name] = newContent
  47. }
  48. // Read file content as string in a archive file.
  49. func readFile(file *zip.File) []byte {
  50. rc, err := file.Open()
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. buff := bytes.NewBuffer(nil)
  55. _, _ = io.Copy(buff, rc)
  56. rc.Close()
  57. return buff.Bytes()
  58. }
  59. // ToAlphaString provides a function to convert integer to Excel sheet column
  60. // title. For example convert 36 to column title AK:
  61. //
  62. // excelize.ToAlphaString(36)
  63. //
  64. func ToAlphaString(value int) string {
  65. if value < 0 {
  66. return ""
  67. }
  68. var ans string
  69. i := value + 1
  70. for i > 0 {
  71. ans = string((i-1)%26+65) + ans
  72. i = (i - 1) / 26
  73. }
  74. return ans
  75. }
  76. // TitleToNumber provides a function to convert Excel sheet column title to
  77. // int (this function doesn't do value check currently). For example convert
  78. // AK and ak to column title 36:
  79. //
  80. // excelize.TitleToNumber("AK")
  81. // excelize.TitleToNumber("ak")
  82. //
  83. func TitleToNumber(s string) int {
  84. weight := 0.0
  85. sum := 0
  86. for i := len(s) - 1; i >= 0; i-- {
  87. ch := int(s[i])
  88. if int(s[i]) >= int('a') && int(s[i]) <= int('z') {
  89. ch = int(s[i]) - 32
  90. }
  91. sum = sum + (ch-int('A')+1)*int(math.Pow(26, weight))
  92. weight++
  93. }
  94. return sum - 1
  95. }
  96. // letterOnlyMapF is used in conjunction with strings.Map to return only the
  97. // characters A-Z and a-z in a string.
  98. func letterOnlyMapF(rune rune) rune {
  99. switch {
  100. case 'A' <= rune && rune <= 'Z':
  101. return rune
  102. case 'a' <= rune && rune <= 'z':
  103. return rune - 32
  104. }
  105. return -1
  106. }
  107. // intOnlyMapF is used in conjunction with strings.Map to return only the
  108. // numeric portions of a string.
  109. func intOnlyMapF(rune rune) rune {
  110. if rune >= 48 && rune < 58 {
  111. return rune
  112. }
  113. return -1
  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 axis1/axis2 can be
  125. // 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)
  146. // as strings
  147. //
  148. // For instance:
  149. //
  150. // "C220" => "C", "220"
  151. // "aaef42" => "aaef", "42"
  152. // "" => "", ""
  153. func getCellColRow(cell string) (col, row string) {
  154. for index, rune := range cell {
  155. if unicode.IsDigit(rune) {
  156. return cell[:index], cell[index:]
  157. }
  158. }
  159. return cell, ""
  160. }
  161. // parseFormatSet provides a method to convert format string to []byte and
  162. // handle empty string.
  163. func parseFormatSet(formatSet string) []byte {
  164. if formatSet != "" {
  165. return []byte(formatSet)
  166. }
  167. return []byte("{}")
  168. }