You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
excelize-formula/lib.go

111 lines
2.5 KiB

9 years ago
package excelize
import (
"archive/zip"
"bytes"
"encoding/xml"
9 years ago
"io"
"log"
"math"
)
// ReadZip takes a pointer to a zip.ReadCloser and returns a
// xlsx.File struct populated with its contents. In most cases
9 years ago
// ReadZip is not used directly, but is called internally by OpenFile.
func ReadZip(f *zip.ReadCloser) (map[string]string, int, error) {
9 years ago
defer f.Close()
return ReadZipReader(&f.Reader)
}
// ReadZipReader can be used to read an XLSX in memory without
9 years ago
// touching the filesystem.
func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
fileList := make(map[string]string)
worksheets := 0
9 years ago
for _, v := range r.File {
fileList[v.Name] = readFile(v)
if len(v.Name) > 18 {
if v.Name[0:19] == "xl/worksheets/sheet" {
var xlsx xlsxWorksheet
xml.Unmarshal([]byte(fileList[v.Name]), &xlsx)
xlsx = checkRow(xlsx)
output, _ := xml.Marshal(xlsx)
fileList[v.Name] = replaceWorkSheetsRelationshipsNameSpace(string(output))
worksheets++
}
}
9 years ago
}
return fileList, worksheets, nil
9 years ago
}
// Read XML content as string.
func (f *File) readXML(name string) string {
if content, ok := f.XLSX[name]; ok {
return content
9 years ago
}
return ``
}
// Update given file content in file list of XLSX.
func (f *File) saveFileList(name string, content string) {
f.XLSX[name] = XMLHeader + content
9 years ago
}
// Read file content as string in a archive file.
9 years ago
func readFile(file *zip.File) string {
rc, err := file.Open()
if err != nil {
log.Fatal(err)
}
buff := bytes.NewBuffer(nil)
io.Copy(buff, rc)
rc.Close()
return string(buff.Bytes())
}
// Convert integer to Excel sheet column title.
9 years ago
func toAlphaString(value int) string {
if value < 0 {
return ``
}
var ans string
i := value
for i > 0 {
ans = string((i-1)%26+65) + ans
i = (i - 1) / 26
}
return ans
}
// Convert Excel sheet column title to int.
9 years ago
func titleToNumber(s string) int {
weight := 0.0
sum := 0
for i := len(s) - 1; i >= 0; i-- {
sum = sum + (int(s[i])-int('A')+1)*int(math.Pow(26, weight))
weight++
}
return sum - 1
}
// letterOnlyMapF is used in conjunction with strings.Map to return
// only the characters A-Z and a-z in a string.
func letterOnlyMapF(rune rune) rune {
switch {
case 'A' <= rune && rune <= 'Z':
return rune
case 'a' <= rune && rune <= 'z':
return rune - 32
9 years ago
}
return -1
9 years ago
}
// intOnlyMapF is used in conjunction with strings.Map to return only
// the numeric portions of a string.
func intOnlyMapF(rune rune) rune {
if rune >= 48 && rune < 58 {
return rune
9 years ago
}
return -1
9 years ago
}