diff --git a/cell.go b/cell.go index 6788daf2c6164e6f7da01416789582cf84b493c7..ecfc5cae74e6d8a6361774c82979853150629d2c 100644 --- a/cell.go +++ b/cell.go @@ -7,7 +7,7 @@ import ( ) // GetCellValue provide function get value from cell by given sheet index and axis in XLSX file -func GetCellValue(file []FileList, sheet string, axis string) string { +func GetCellValue(file map[string]string, sheet string, axis string) string { axis = strings.ToUpper(axis) var xlsx xlsxWorksheet row := getRowIndex(axis) diff --git a/excelize.go b/excelize.go index 6da86395b1644f0613de6f7b96157715100deeb8..2dd62d3a66c8ff4242f03200a7c6412769abd11e 100644 --- a/excelize.go +++ b/excelize.go @@ -17,7 +17,7 @@ type FileList struct { // OpenFile take the name of an XLSX file and returns a populated // xlsx.File struct for it. -func OpenFile(filename string) (file []FileList, err error) { +func OpenFile(filename string) (file map[string]string, err error) { var f *zip.ReadCloser f, err = zip.OpenReader(filename) if err != nil { @@ -28,7 +28,7 @@ func OpenFile(filename string) (file []FileList, err error) { } // SetCellInt provide function to set int type value of a cell -func SetCellInt(file []FileList, sheet string, axis string, value int) []FileList { +func SetCellInt(file map[string]string, sheet string, axis string, value int) map[string]string { axis = strings.ToUpper(axis) var xlsx xlsxWorksheet col := getColIndex(axis) @@ -58,7 +58,7 @@ func SetCellInt(file []FileList, sheet string, axis string, value int) []FileLis } // SetCellStr provide function to set string type value of a cell -func SetCellStr(file []FileList, sheet string, axis string, value string) []FileList { +func SetCellStr(file map[string]string, sheet string, axis string, value string) map[string]string { axis = strings.ToUpper(axis) var xlsx xlsxWorksheet col := getColIndex(axis) diff --git a/file.go b/file.go index d30ffa215dde1f013db77a5a9354e198cb2b43a6..a25d66dec1147f05b0560bc1e1fa6aa7bb385242 100644 --- a/file.go +++ b/file.go @@ -9,8 +9,8 @@ import ( // CreateFile provide function to create new file by default template // For example: // xlsx := CreateFile() -func CreateFile() []FileList { - var file []FileList +func CreateFile() map[string]string { + file := make(map[string]string) file = saveFileList(file, `_rels/.rels`, templateRels) file = saveFileList(file, `docProps/app.xml`, templateDocpropsApp) file = saveFileList(file, `docProps/core.xml`, templateDocpropsCore) @@ -24,15 +24,15 @@ func CreateFile() []FileList { } // Save after create or update to an xlsx file at the provided path. -func Save(files []FileList, name string) error { +func Save(files map[string]string, name string) error { buf := new(bytes.Buffer) w := zip.NewWriter(buf) - for _, file := range files { - f, err := w.Create(file.Key) + for path, content := range files { + f, err := w.Create(path) if err != nil { return err } - _, err = f.Write([]byte(file.Value)) + _, err = f.Write([]byte(content)) if err != nil { return err } diff --git a/lib.go b/lib.go index 93234118d2d26396793847fa43ef010431680444..7147424f42f94c0189f126d1a0614cd7dbac760e 100644 --- a/lib.go +++ b/lib.go @@ -14,51 +14,32 @@ import ( // ReadZip takes a pointer to a zip.ReadCloser and returns a // xlsx.File struct populated with its contents. In most cases // ReadZip is not used directly, but is called internally by OpenFile. -func ReadZip(f *zip.ReadCloser) ([]FileList, error) { +func ReadZip(f *zip.ReadCloser) (map[string]string, error) { defer f.Close() return ReadZipReader(&f.Reader) } // ReadZipReader can be used to read an XLSX in memory without // touching the filesystem. -func ReadZipReader(r *zip.Reader) ([]FileList, error) { - var fileList []FileList +func ReadZipReader(r *zip.Reader) (map[string]string, error) { + fileList := make(map[string]string) for _, v := range r.File { - singleFile := FileList{ - Key: v.Name, - Value: readFile(v), - } - fileList = append(fileList, singleFile) + fileList[v.Name] = readFile(v) } return fileList, nil } // Read XML content as string and replace drawing property in XML namespace of sheet -func readXML(files []FileList, name string) string { - for _, file := range files { - if file.Key == name { - return strings.Replace(file.Value, "