From 5dd0b4aec2931079e064f1fb393b034ce4934540 Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 6 Nov 2020 20:03:13 +0800 Subject: [PATCH] using POSIX directory separator in zip path with Windows --- chart.go | 5 +---- excelize.go | 5 +---- excelize_test.go | 8 ++++++++ pivotTable.go | 5 +---- rows.go | 5 +---- sheet.go | 31 ++++++++++++++++++------------- sheet_test.go | 13 +++++++++++++ 7 files changed, 43 insertions(+), 29 deletions(-) diff --git a/chart.go b/chart.go index 978a2f9..c5b8fc8 100644 --- a/chart.go +++ b/chart.go @@ -16,7 +16,6 @@ import ( "encoding/xml" "errors" "fmt" - "path/filepath" "strconv" "strings" ) @@ -802,9 +801,7 @@ func (f *File) AddChartSheet(sheet, format string, combo ...string) error { f.addContentTypePart(sheetID, "chartsheet") f.addContentTypePart(drawingID, "drawings") // Update workbook.xml.rels - wbPath := f.getWorkbookPath() - 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), "") + rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipChartsheet, fmt.Sprintf("/xl/chartsheets/sheet%d.xml", sheetID), "") // Update workbook.xml f.setWorkbook(sheet, sheetID, rID) chartsheet, _ := xml.Marshal(cs) diff --git a/excelize.go b/excelize.go index 3a511d1..2cbf54d 100644 --- a/excelize.go +++ b/excelize.go @@ -22,7 +22,6 @@ import ( "io/ioutil" "os" "path" - "path/filepath" "strconv" "strings" "sync" @@ -346,9 +345,7 @@ func (f *File) AddVBAProject(bin string) error { return errors.New("unsupported VBA project extension") } f.setContentTypePartVBAProjectExtensions() - wbPath := f.getWorkbookPath() - wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator)) - wb := f.relsReader(wbRelsPath) + wb := f.relsReader(f.getWorkbookRelsPath()) var rID int var ok bool for _, rel := range wb.Relationships { diff --git a/excelize_test.go b/excelize_test.go index 1b48872..d838a1f 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -21,6 +21,14 @@ import ( "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) { // Test update the spreadsheet file. f, err := OpenFile(filepath.Join("test", "Book1.xlsx")) diff --git a/pivotTable.go b/pivotTable.go index 3a277c3..96e3627 100644 --- a/pivotTable.go +++ b/pivotTable.go @@ -15,7 +15,6 @@ import ( "encoding/xml" "errors" "fmt" - "path/filepath" "strconv" "strings" ) @@ -139,9 +138,7 @@ func (f *File) AddPivotTable(opt *PivotTableOption) error { } // workbook pivot cache - wbPath := f.getWorkbookPath() - 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), "") + workBookPivotCacheRID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipPivotCache, fmt.Sprintf("/xl/pivotCache/pivotCacheDefinition%d.xml", pivotCacheID), "") cacheID := f.addWorkbookPivotCache(workBookPivotCacheRID) pivotCacheRels := "xl/pivotTables/_rels/pivotTable" + strconv.Itoa(pivotTableID) + ".xml.rels" diff --git a/rows.go b/rows.go index 12d5ddf..63b3947 100644 --- a/rows.go +++ b/rows.go @@ -19,9 +19,7 @@ import ( "io" "log" "math" - "path/filepath" "strconv" - "strings" ) // 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 f.Lock() defer f.Unlock() - wbPath := f.getWorkbookPath() - relPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator)) + relPath := f.getWorkbookRelsPath() if f.SharedStrings == nil { var sharedStrings xlsxSST ss := f.readXML("xl/sharedStrings.xml") diff --git a/sheet.go b/sheet.go index 9c9def7..f493aac 100644 --- a/sheet.go +++ b/sheet.go @@ -59,9 +59,7 @@ func (f *File) NewSheet(name string) int { // Create new sheet /xl/worksheets/sheet%d.xml f.setSheet(sheetID, name) // Update workbook.xml.rels - wbPath := f.getWorkbookPath() - 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), "") + rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipWorkSheet, fmt.Sprintf("/xl/worksheets/sheet%d.xml", sheetID), "") // Update workbook.xml f.setWorkbook(name, sheetID, rID) return f.GetSheetIndex(name) @@ -98,7 +96,7 @@ func (f *File) getWorkbookPath() (path string) { if rels := f.relsReader("_rels/.rels"); rels != nil { for _, rel := range rels.Relationships { if rel.Type == SourceRelationshipOfficeDocument { - path = strings.TrimPrefix(rel.Target, string(filepath.Separator)) + path = strings.TrimPrefix(rel.Target, "/") return } } @@ -106,6 +104,19 @@ func (f *File) getWorkbookPath() (path string) { 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 // structure after deserialization. func (f *File) workbookReader() *xlsxWorkbook { @@ -437,9 +448,7 @@ func (f *File) GetSheetList() (list []string) { // of the spreadsheet. func (f *File) getSheetMap() map[string]string { content := f.workbookReader() - wbPath := f.getWorkbookPath() - wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator)) - rels := f.relsReader(wbRelsPath) + rels := f.relsReader(f.getWorkbookRelsPath()) maps := map[string]string{} for _, v := range content.Sheets.Sheet { for _, rel := range rels.Relationships { @@ -489,9 +498,7 @@ func (f *File) DeleteSheet(name string) { } sheetName := trimSheetName(name) wb := f.workbookReader() - wbPath := f.getWorkbookPath() - wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator)) - wbRels := f.relsReader(wbRelsPath) + wbRels := f.relsReader(f.getWorkbookRelsPath()) for idx, sheet := range wb.Sheets.Sheet { if sheet.Name == sheetName { 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 // relationships by given relationships ID in the file workbook.xml.rels. func (f *File) deleteSheetFromWorkbookRels(rID string) string { - wbPath := f.getWorkbookPath() - wbRelsPath := strings.TrimPrefix(filepath.Join(filepath.Dir(wbPath), "_rels", filepath.Base(wbPath)+".rels"), string(filepath.Separator)) - content := f.relsReader(wbRelsPath) + content := f.relsReader(f.getWorkbookRelsPath()) for k, v := range content.Relationships { if v.ID == rID { content.Relationships = append(content.Relationships[:k], content.Relationships[k+1:]...) diff --git a/sheet_test.go b/sheet_test.go index 4626003..bfe0ce3 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -346,6 +346,19 @@ func TestSetSheetName(t *testing.T) { 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(``) + assert.Equal(t, "_rels/workbook.xml.rels", f.getWorkbookRelsPath()) +} + func BenchmarkNewSheet(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() {