提交 956a4627 编写于 作者: xurime's avatar xurime

Fix issue #4 use builtin `map` instead of home-built.

上级 9c3a24d5
......@@ -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)
......
......@@ -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)
......
......@@ -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
}
......
......@@ -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, "<drawing r:id=", "<drawing rid=", -1)
}
func readXML(files map[string]string, name string) string {
if content, ok := files[name]; ok {
return strings.Replace(content, "<drawing r:id=", "<drawing rid=", -1)
}
return ``
}
// Update given file content in file list of XLSX
func saveFileList(files []FileList, name string, content string) []FileList {
for k, v := range files {
if v.Key == name {
files = files[:k+copy(files[k:], files[k+1:])]
files = append(files, FileList{
Key: name,
Value: XMLHeader + content,
})
return files
}
}
files = append(files, FileList{
Key: name,
Value: XMLHeader + content,
})
func saveFileList(files map[string]string, name string, content string) map[string]string {
files[name] = XMLHeader + content
return files
}
......
......@@ -11,7 +11,7 @@ import (
// NewSheet provice function to greate a new sheet by given index, when
// creating a new XLSX file, the default sheet will be create, when you
// create a new file, you need to ensure that the index is continuous.
func NewSheet(file []FileList, index int, name string) []FileList {
func NewSheet(file map[string]string, index int, name string) map[string]string {
// Update docProps/app.xml
file = setAppXML(file)
// Update [Content_Types].xml
......@@ -26,7 +26,7 @@ func NewSheet(file []FileList, index int, name string) []FileList {
}
// Read and update property of contents type of XLSX
func setContentTypes(file []FileList, index int) []FileList {
func setContentTypes(file map[string]string, index int) map[string]string {
var content xlsxTypes
xml.Unmarshal([]byte(readXML(file, `[Content_Types].xml`)), &content)
content.Overrides = append(content.Overrides, xlsxOverride{
......@@ -41,7 +41,7 @@ func setContentTypes(file []FileList, index int) []FileList {
}
// Update sheet property by given index
func setSheet(file []FileList, index int) []FileList {
func setSheet(file map[string]string, index int) map[string]string {
var xlsx xlsxWorksheet
xlsx.Dimension.Ref = "A1"
xlsx.SheetViews.SheetView = append(xlsx.SheetViews.SheetView, xlsxSheetView{
......@@ -56,7 +56,7 @@ func setSheet(file []FileList, index int) []FileList {
}
// Update workbook property of XLSX
func setWorkbook(file []FileList, index int, name string) []FileList {
func setWorkbook(file map[string]string, index int, name string) map[string]string {
var content xlsxWorkbook
xml.Unmarshal([]byte(readXML(file, `xl/workbook.xml`)), &content)
......@@ -75,14 +75,14 @@ func setWorkbook(file []FileList, index int, name string) []FileList {
}
// Read and unmarshal workbook relationships of XLSX
func readXlsxWorkbookRels(file []FileList) xlsxWorkbookRels {
func readXlsxWorkbookRels(file map[string]string) xlsxWorkbookRels {
var content xlsxWorkbookRels
xml.Unmarshal([]byte(readXML(file, `xl/_rels/workbook.xml.rels`)), &content)
return content
}
// Update workbook relationships property of XLSX
func addXlsxWorkbookRels(file []FileList, sheet int) []FileList {
func addXlsxWorkbookRels(file map[string]string, sheet int) map[string]string {
content := readXlsxWorkbookRels(file)
rID := len(content.Relationships) + 1
ID := bytes.Buffer{}
......@@ -105,7 +105,7 @@ func addXlsxWorkbookRels(file []FileList, sheet int) []FileList {
}
// Update docProps/app.xml file of XML
func setAppXML(file []FileList) []FileList {
func setAppXML(file map[string]string) map[string]string {
return saveFileList(file, `docProps/app.xml`, templateDocpropsApp)
}
......@@ -134,7 +134,7 @@ func replaceRelationshipsID(workbookMarshal string) string {
}
// SetActiveSheet provide function to set default active sheet of XLSX by given index
func SetActiveSheet(file []FileList, index int) []FileList {
func SetActiveSheet(file map[string]string, index int) map[string]string {
var content xlsxWorkbook
if index < 1 {
index = 1
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册