using POSIX directory separator in zip path with Windows

This commit is contained in:
xuri 2020-11-06 20:03:13 +08:00
parent cdc57db3b3
commit 5dd0b4aec2
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
7 changed files with 43 additions and 29 deletions

View File

@ -16,7 +16,6 @@ import (
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt" "fmt"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
) )
@ -802,9 +801,7 @@ func (f *File) AddChartSheet(sheet, format string, combo ...string) error {
f.addContentTypePart(sheetID, "chartsheet") f.addContentTypePart(sheetID, "chartsheet")
f.addContentTypePart(drawingID, "drawings") f.addContentTypePart(drawingID, "drawings")
// Update workbook.xml.rels // Update workbook.xml.rels
wbPath := f.getWorkbookPath() rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipChartsheet, fmt.Sprintf("/xl/chartsheets/sheet%d.xml", sheetID), "")
wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator))
rID := f.addRels(wbRelsPath, SourceRelationshipChartsheet, fmt.Sprintf("/xl/chartsheets/sheet%d.xml", sheetID), "")
// Update workbook.xml // Update workbook.xml
f.setWorkbook(sheet, sheetID, rID) f.setWorkbook(sheet, sheetID, rID)
chartsheet, _ := xml.Marshal(cs) chartsheet, _ := xml.Marshal(cs)

View File

@ -22,7 +22,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -346,9 +345,7 @@ func (f *File) AddVBAProject(bin string) error {
return errors.New("unsupported VBA project extension") return errors.New("unsupported VBA project extension")
} }
f.setContentTypePartVBAProjectExtensions() f.setContentTypePartVBAProjectExtensions()
wbPath := f.getWorkbookPath() wb := f.relsReader(f.getWorkbookRelsPath())
wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator))
wb := f.relsReader(wbRelsPath)
var rID int var rID int
var ok bool var ok bool
for _, rel := range wb.Relationships { for _, rel := range wb.Relationships {

View File

@ -21,6 +21,14 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestCurrency(t *testing.T) {
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
assert.NoError(t, err)
// f.NewSheet("Sheet3")
go f.SetCellValue("Sheet1", "A1", "value")
go f.SetCellValue("Sheet2", "A1", "value")
}
func TestOpenFile(t *testing.T) { func TestOpenFile(t *testing.T) {
// Test update the spreadsheet file. // Test update the spreadsheet file.
f, err := OpenFile(filepath.Join("test", "Book1.xlsx")) f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))

View File

@ -15,7 +15,6 @@ import (
"encoding/xml" "encoding/xml"
"errors" "errors"
"fmt" "fmt"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
) )
@ -139,9 +138,7 @@ func (f *File) AddPivotTable(opt *PivotTableOption) error {
} }
// workbook pivot cache // workbook pivot cache
wbPath := f.getWorkbookPath() workBookPivotCacheRID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipPivotCache, fmt.Sprintf("/xl/pivotCache/pivotCacheDefinition%d.xml", pivotCacheID), "")
wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator))
workBookPivotCacheRID := f.addRels(wbRelsPath, SourceRelationshipPivotCache, fmt.Sprintf("/xl/pivotCache/pivotCacheDefinition%d.xml", pivotCacheID), "")
cacheID := f.addWorkbookPivotCache(workBookPivotCacheRID) cacheID := f.addWorkbookPivotCache(workBookPivotCacheRID)
pivotCacheRels := "xl/pivotTables/_rels/pivotTable" + strconv.Itoa(pivotTableID) + ".xml.rels" pivotCacheRels := "xl/pivotTables/_rels/pivotTable" + strconv.Itoa(pivotTableID) + ".xml.rels"

View File

@ -19,9 +19,7 @@ import (
"io" "io"
"log" "log"
"math" "math"
"path/filepath"
"strconv" "strconv"
"strings"
) )
// GetRows return all the rows in a sheet by given worksheet name (case // GetRows return all the rows in a sheet by given worksheet name (case
@ -292,8 +290,7 @@ func (f *File) sharedStringsReader() *xlsxSST {
var err error var err error
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
wbPath := f.getWorkbookPath() relPath := f.getWorkbookRelsPath()
relPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator))
if f.SharedStrings == nil { if f.SharedStrings == nil {
var sharedStrings xlsxSST var sharedStrings xlsxSST
ss := f.readXML("xl/sharedStrings.xml") ss := f.readXML("xl/sharedStrings.xml")

View File

@ -59,9 +59,7 @@ func (f *File) NewSheet(name string) int {
// Create new sheet /xl/worksheets/sheet%d.xml // Create new sheet /xl/worksheets/sheet%d.xml
f.setSheet(sheetID, name) f.setSheet(sheetID, name)
// Update workbook.xml.rels // Update workbook.xml.rels
wbPath := f.getWorkbookPath() rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipWorkSheet, fmt.Sprintf("/xl/worksheets/sheet%d.xml", sheetID), "")
wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator))
rID := f.addRels(wbRelsPath, SourceRelationshipWorkSheet, fmt.Sprintf("/xl/worksheets/sheet%d.xml", sheetID), "")
// Update workbook.xml // Update workbook.xml
f.setWorkbook(name, sheetID, rID) f.setWorkbook(name, sheetID, rID)
return f.GetSheetIndex(name) return f.GetSheetIndex(name)
@ -98,7 +96,7 @@ func (f *File) getWorkbookPath() (path string) {
if rels := f.relsReader("_rels/.rels"); rels != nil { if rels := f.relsReader("_rels/.rels"); rels != nil {
for _, rel := range rels.Relationships { for _, rel := range rels.Relationships {
if rel.Type == SourceRelationshipOfficeDocument { if rel.Type == SourceRelationshipOfficeDocument {
path = strings.TrimPrefix(rel.Target, string(filepath.Separator)) path = strings.TrimPrefix(rel.Target, "/")
return return
} }
} }
@ -106,6 +104,19 @@ func (f *File) getWorkbookPath() (path string) {
return return
} }
// getWorkbookRelsPath provides a function to get the path of the workbook.xml.rels
// in the spreadsheet.
func (f *File) getWorkbookRelsPath() (path string) {
wbPath := f.getWorkbookPath()
wbDir := filepath.Dir(wbPath)
if wbDir == "." {
path = "_rels/" + filepath.Base(wbPath) + ".rels"
return
}
path = strings.TrimPrefix(filepath.Dir(wbPath)+"/_rels/"+filepath.Base(wbPath)+".rels", "/")
return
}
// workbookReader provides a function to get the pointer to the workbook.xml // workbookReader provides a function to get the pointer to the workbook.xml
// structure after deserialization. // structure after deserialization.
func (f *File) workbookReader() *xlsxWorkbook { func (f *File) workbookReader() *xlsxWorkbook {
@ -437,9 +448,7 @@ func (f *File) GetSheetList() (list []string) {
// of the spreadsheet. // of the spreadsheet.
func (f *File) getSheetMap() map[string]string { func (f *File) getSheetMap() map[string]string {
content := f.workbookReader() content := f.workbookReader()
wbPath := f.getWorkbookPath() rels := f.relsReader(f.getWorkbookRelsPath())
wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator))
rels := f.relsReader(wbRelsPath)
maps := map[string]string{} maps := map[string]string{}
for _, v := range content.Sheets.Sheet { for _, v := range content.Sheets.Sheet {
for _, rel := range rels.Relationships { for _, rel := range rels.Relationships {
@ -489,9 +498,7 @@ func (f *File) DeleteSheet(name string) {
} }
sheetName := trimSheetName(name) sheetName := trimSheetName(name)
wb := f.workbookReader() wb := f.workbookReader()
wbPath := f.getWorkbookPath() wbRels := f.relsReader(f.getWorkbookRelsPath())
wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator))
wbRels := f.relsReader(wbRelsPath)
for idx, sheet := range wb.Sheets.Sheet { for idx, sheet := range wb.Sheets.Sheet {
if sheet.Name == sheetName { if sheet.Name == sheetName {
wb.Sheets.Sheet = append(wb.Sheets.Sheet[:idx], wb.Sheets.Sheet[idx+1:]...) wb.Sheets.Sheet = append(wb.Sheets.Sheet[:idx], wb.Sheets.Sheet[idx+1:]...)
@ -532,9 +539,7 @@ func (f *File) DeleteSheet(name string) {
// deleteSheetFromWorkbookRels provides a function to remove worksheet // deleteSheetFromWorkbookRels provides a function to remove worksheet
// relationships by given relationships ID in the file workbook.xml.rels. // relationships by given relationships ID in the file workbook.xml.rels.
func (f *File) deleteSheetFromWorkbookRels(rID string) string { func (f *File) deleteSheetFromWorkbookRels(rID string) string {
wbPath := f.getWorkbookPath() content := f.relsReader(f.getWorkbookRelsPath())
wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator))
content := f.relsReader(wbRelsPath)
for k, v := range content.Relationships { for k, v := range content.Relationships {
if v.ID == rID { if v.ID == rID {
content.Relationships = append(content.Relationships[:k], content.Relationships[k+1:]...) content.Relationships = append(content.Relationships[:k], content.Relationships[k+1:]...)

View File

@ -346,6 +346,19 @@ func TestSetSheetName(t *testing.T) {
assert.Equal(t, "Sheet1", f.GetSheetName(0)) assert.Equal(t, "Sheet1", f.GetSheetName(0))
} }
func TestGetWorkbookPath(t *testing.T) {
f := NewFile()
delete(f.XLSX, "_rels/.rels")
assert.Equal(t, "", f.getWorkbookPath())
}
func TestGetWorkbookRelsPath(t *testing.T) {
f := NewFile()
delete(f.XLSX, "xl/_rels/.rels")
f.XLSX["_rels/.rels"] = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument" Target="/workbook.xml"/></Relationships>`)
assert.Equal(t, "_rels/workbook.xml.rels", f.getWorkbookRelsPath())
}
func BenchmarkNewSheet(b *testing.B) { func BenchmarkNewSheet(b *testing.B) {
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {