Skip to content

Commit 49257c5

Browse files
committed
support case-sensitive doc parts to improve compatibility
1 parent f7bd072 commit 49257c5

File tree

7 files changed

+50
-36
lines changed

7 files changed

+50
-36
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ go get github.com/360EntSecGroup-Skylar/excelize
2929
go get github.com/360EntSecGroup-Skylar/excelize/v2
3030
```
3131

32-
### Create XLSX file
32+
### Create spreadsheet
3333

34-
Here is a minimal example usage that will create XLSX file.
34+
Here is a minimal example usage that will create spreadsheet file.
3535

3636
```go
3737
package main
@@ -58,9 +58,9 @@ func main() {
5858
}
5959
```
6060

61-
### Reading XLSX file
61+
### Reading spreadsheet
6262

63-
The following constitutes the bare to read a XLSX document.
63+
The following constitutes the bare to read a spreadsheet document.
6464

6565
```go
6666
package main
@@ -95,7 +95,7 @@ func main() {
9595
}
9696
```
9797

98-
### Add chart to XLSX file
98+
### Add chart to spreadsheet file
9999

100100
With Excelize chart generation and management is as easy as a few lines of code. You can build charts based off data in your worksheet or generate charts without any data in your worksheet at all.
101101

@@ -131,7 +131,7 @@ func main() {
131131
}
132132
```
133133

134-
### Add picture to XLSX file
134+
### Add picture to spreadsheet file
135135

136136
```go
137137
package main

excelize.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,25 @@ func checkSheet(xlsx *xlsxWorksheet) {
220220
// addRels provides a function to add relationships by given XML path,
221221
// relationship type, target and target mode.
222222
func (f *File) addRels(relPath, relType, target, targetMode string) int {
223+
var uniqPart = map[string]string{
224+
SourceRelationshipSharedStrings: "/xl/sharedStrings.xml",
225+
}
223226
rels := f.relsReader(relPath)
224227
if rels == nil {
225228
rels = &xlsxRelationships{}
226229
}
227230
var rID int
228-
for _, rel := range rels.Relationships {
231+
for idx, rel := range rels.Relationships {
229232
ID, _ := strconv.Atoi(strings.TrimPrefix(rel.ID, "rId"))
230233
if ID > rID {
231234
rID = ID
232235
}
236+
if relType == rel.Type {
237+
if partName, ok := uniqPart[rel.Type]; ok {
238+
rels.Relationships[idx].Target = partName
239+
return rID
240+
}
241+
}
233242
}
234243
rID++
235244
var ID bytes.Buffer

excelize_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
func TestOpenFile(t *testing.T) {
25-
// Test update a XLSX file.
25+
// Test update the spreadsheet file.
2626
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
2727
assert.NoError(t, err)
2828

@@ -154,11 +154,11 @@ func TestOpenFile(t *testing.T) {
154154
// Test read cell value with given axis large than exists row.
155155
_, err = f.GetCellValue("Sheet2", "E231")
156156
assert.NoError(t, err)
157-
// Test get active worksheet of XLSX and get worksheet name of XLSX by given worksheet index.
157+
// Test get active worksheet of spreadsheet and get worksheet name of spreadsheet by given worksheet index.
158158
f.GetSheetName(f.GetActiveSheetIndex())
159-
// Test get worksheet index of XLSX by given worksheet name.
159+
// Test get worksheet index of spreadsheet by given worksheet name.
160160
f.GetSheetIndex("Sheet1")
161-
// Test get worksheet name of XLSX by given invalid worksheet index.
161+
// Test get worksheet name of spreadsheet by given invalid worksheet index.
162162
f.GetSheetName(4)
163163
// Test get worksheet map of workbook.
164164
f.GetSheetMap()
@@ -261,7 +261,7 @@ func TestBrokenFile(t *testing.T) {
261261
})
262262

263263
t.Run("OpenNotExistsFile", func(t *testing.T) {
264-
// Test open a XLSX file with given illegal path.
264+
// Test open a spreadsheet file with given illegal path.
265265
_, err := OpenFile(filepath.Join("test", "NotExistsFile.xlsx"))
266266
if assert.Error(t, err) {
267267
assert.True(t, os.IsNotExist(err), "Expected os.IsNotExists(err) == true")
@@ -270,7 +270,7 @@ func TestBrokenFile(t *testing.T) {
270270
}
271271

272272
func TestNewFile(t *testing.T) {
273-
// Test create a XLSX file.
273+
// Test create a spreadsheet file.
274274
f := NewFile()
275275
f.NewSheet("Sheet1")
276276
f.NewSheet("XLSXSheet2")

lib.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@ import (
2626
// filesystem.
2727
func ReadZipReader(r *zip.Reader) (map[string][]byte, int, error) {
2828
var err error
29+
var docPart = map[string]string{
30+
"[content_types].xml": "[Content_Types].xml",
31+
"xl/sharedstrings.xml": "xl/sharedStrings.xml",
32+
}
2933
fileList := make(map[string][]byte, len(r.File))
3034
worksheets := 0
3135
for _, v := range r.File {
32-
if fileList[v.Name], err = readFile(v); err != nil {
36+
fileName := v.Name
37+
if partName, ok := docPart[strings.ToLower(v.Name)]; ok {
38+
fileName = partName
39+
}
40+
if fileList[fileName], err = readFile(v); err != nil {
3341
return nil, 0, err
3442
}
3543
if strings.HasPrefix(v.Name, "xl/worksheets/sheet") {

picture.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte, error) {
474474
return f.getPicture(row, col, drawingXML, drawingRelationships)
475475
}
476476

477-
// DeletePicture provides a function to delete charts in XLSX by given
477+
// DeletePicture provides a function to delete charts in spreadsheet by given
478478
// worksheet and cell name. Note that the image file won't be deleted from the
479479
// document currently.
480480
func (f *File) DeletePicture(sheet, cell string) (err error) {
@@ -496,7 +496,7 @@ func (f *File) DeletePicture(sheet, cell string) (err error) {
496496
}
497497

498498
// getPicture provides a function to get picture base name and raw content
499-
// embed in XLSX by given coordinates and drawing relationships.
499+
// embed in spreadsheet by given coordinates and drawing relationships.
500500
func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string) (ret string, buf []byte, err error) {
501501
var (
502502
wsDr *xlsxWsDr

rows.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,6 @@ func (f *File) sharedStringsReader() *xlsxSST {
282282
if f.SharedStrings == nil {
283283
var sharedStrings xlsxSST
284284
ss := f.readXML("xl/sharedStrings.xml")
285-
if len(ss) == 0 {
286-
ss = f.readXML("xl/SharedStrings.xml")
287-
delete(f.XLSX, "xl/SharedStrings.xml")
288-
}
289285
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(ss))).
290286
Decode(&sharedStrings); err != nil && err != io.EOF {
291287
log.Printf("xml decode error: %s", err)

sheet.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ import (
3232
)
3333

3434
// NewSheet provides function to create a new sheet by given worksheet name.
35-
// When creating a new XLSX file, the default sheet will be created. Returns
36-
// the number of sheets in the workbook (file) after appending the new sheet.
35+
// When creating a new spreadsheet file, the default worksheet will be
36+
// created. Returns the number of sheets in the workbook (file) after
37+
// appending the new sheet.
3738
func (f *File) NewSheet(name string) int {
3839
// Check if the worksheet already exists
3940
if f.GetSheetIndex(name) != -1 {
@@ -152,7 +153,7 @@ func trimCell(column []xlsxC) []xlsxC {
152153
}
153154

154155
// setContentTypes provides a function to read and update property of contents
155-
// type of XLSX.
156+
// type of the spreadsheet.
156157
func (f *File) setContentTypes(partName, contentType string) {
157158
content := f.contentTypesReader()
158159
content.Overrides = append(content.Overrides, xlsxOverride{
@@ -174,8 +175,8 @@ func (f *File) setSheet(index int, name string) {
174175
f.Sheet[path] = &xlsx
175176
}
176177

177-
// setWorkbook update workbook property of XLSX. Maximum 31 characters are
178-
// allowed in sheet title.
178+
// setWorkbook update workbook property of the spreadsheet. Maximum 31
179+
// characters are allowed in sheet title.
179180
func (f *File) setWorkbook(name string, sheetID, rid int) {
180181
content := f.workbookReader()
181182
content.Sheets.Sheet = append(content.Sheets.Sheet, xlsxSheet{
@@ -204,9 +205,9 @@ func (f *File) setAppXML() {
204205
f.saveFileList("docProps/app.xml", []byte(templateDocpropsApp))
205206
}
206207

207-
// replaceRelationshipsBytes; Some tools that read XLSX files have very strict
208-
// requirements about the structure of the input XML. This function is a
209-
// horrible hack to fix that after the XML marshalling is completed.
208+
// replaceRelationshipsBytes; Some tools that read spreadsheet files have very
209+
// strict requirements about the structure of the input XML. This function is
210+
// a horrible hack to fix that after the XML marshalling is completed.
210211
func replaceRelationshipsBytes(content []byte) []byte {
211212
oldXmlns := stringToBytes(`xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships`)
212213
newXmlns := stringToBytes("r")
@@ -263,7 +264,7 @@ func (f *File) SetActiveSheet(index int) {
263264
}
264265

265266
// GetActiveSheetIndex provides a function to get active sheet index of the
266-
// XLSX. If not found the active sheet will be return integer 0.
267+
// spreadsheet. If not found the active sheet will be return integer 0.
267268
func (f *File) GetActiveSheetIndex() (index int) {
268269
var sheetID = f.getActiveSheetID()
269270
wb := f.workbookReader()
@@ -278,7 +279,7 @@ func (f *File) GetActiveSheetIndex() (index int) {
278279
}
279280

280281
// getActiveSheetID provides a function to get active sheet index of the
281-
// XLSX. If not found the active sheet will be return integer 0.
282+
// spreadsheet. If not found the active sheet will be return integer 0.
282283
func (f *File) getActiveSheetID() int {
283284
wb := f.workbookReader()
284285
if wb != nil {
@@ -313,9 +314,9 @@ func (f *File) SetSheetName(oldName, newName string) {
313314
}
314315
}
315316

316-
// getSheetNameByID provides a function to get worksheet name of XLSX by given
317-
// worksheet ID. If given sheet ID is invalid, will return an empty
318-
// string.
317+
// getSheetNameByID provides a function to get worksheet name of the
318+
// spreadsheet by given worksheet ID. If given sheet ID is invalid, will
319+
// return an empty string.
319320
func (f *File) getSheetNameByID(ID int) string {
320321
wb := f.workbookReader()
321322
if wb == nil || ID < 1 {
@@ -341,9 +342,9 @@ func (f *File) GetSheetName(index int) (name string) {
341342
return
342343
}
343344

344-
// getSheetID provides a function to get worksheet ID of XLSX by given
345-
// sheet name. If given worksheet name is invalid, will return an integer type
346-
// value -1.
345+
// getSheetID provides a function to get worksheet ID of the spreadsheet by
346+
// given sheet name. If given worksheet name is invalid, will return an
347+
// integer type value -1.
347348
func (f *File) getSheetID(name string) int {
348349
var ID = -1
349350
for sheetID, sheet := range f.GetSheetMap() {

0 commit comments

Comments
 (0)