Parcourir la source

using POSIX directory separator in zip path with Windows

xuri il y a 5 ans
Parent
commit
5dd0b4aec2
7 fichiers modifiés avec 43 ajouts et 29 suppressions
  1. 1 4
      chart.go
  2. 1 4
      excelize.go
  3. 8 0
      excelize_test.go
  4. 1 4
      pivotTable.go
  5. 1 4
      rows.go
  6. 18 13
      sheet.go
  7. 13 0
      sheet_test.go

+ 1 - 4
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)

+ 1 - 4
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 {

+ 8 - 0
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"))

+ 1 - 4
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"

+ 1 - 4
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")

+ 18 - 13
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:]...)

+ 13 - 0
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(`<?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) {
 	b.RunParallel(func(pb *testing.PB) {
 		for pb.Next() {