Skip to content

Commit e37724c

Browse files
committed
This fix potential panic and file corrupted
- Fix the panic when set or get sheet view options on the sheet without views options - Fix generated workbook corruption caused by empty created or modified dcterms in the document core properties - Update the unit tests
1 parent a65c584 commit e37724c

File tree

4 files changed

+70
-52
lines changed

4 files changed

+70
-52
lines changed

docProps.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,12 @@ func (f *File) SetDocProps(docProperties *DocProperties) (err error) {
204204
Category: core.Category,
205205
Version: core.Version,
206206
}, nil
207-
newProps.Created.Text, newProps.Created.Type, newProps.Modified.Text, newProps.Modified.Type = core.Created.Text, core.Created.Type, core.Modified.Text, core.Modified.Type
207+
if core.Created != nil {
208+
newProps.Created = &xlsxDcTerms{Type: core.Created.Type, Text: core.Created.Text}
209+
}
210+
if core.Modified != nil {
211+
newProps.Modified = &xlsxDcTerms{Type: core.Modified.Type, Text: core.Modified.Text}
212+
}
208213
fields = []string{
209214
"Category", "ContentStatus", "Creator", "Description", "Identifier", "Keywords",
210215
"LastModifiedBy", "Revision", "Subject", "Title", "Language", "Version",
@@ -216,10 +221,10 @@ func (f *File) SetDocProps(docProperties *DocProperties) (err error) {
216221
}
217222
}
218223
if docProperties.Created != "" {
219-
newProps.Created.Text = docProperties.Created
224+
newProps.Created = &xlsxDcTerms{Type: "dcterms:W3CDTF", Text: docProperties.Created}
220225
}
221226
if docProperties.Modified != "" {
222-
newProps.Modified.Text = docProperties.Modified
227+
newProps.Modified = &xlsxDcTerms{Type: "dcterms:W3CDTF", Text: docProperties.Modified}
223228
}
224229
output, err = xml.Marshal(newProps)
225230
f.saveFileList(defaultXMLPathDocPropsCore, output)
@@ -239,19 +244,22 @@ func (f *File) GetDocProps() (ret *DocProperties, err error) {
239244
ret, err = &DocProperties{
240245
Category: core.Category,
241246
ContentStatus: core.ContentStatus,
242-
Created: core.Created.Text,
243247
Creator: core.Creator,
244248
Description: core.Description,
245249
Identifier: core.Identifier,
246250
Keywords: core.Keywords,
247251
LastModifiedBy: core.LastModifiedBy,
248-
Modified: core.Modified.Text,
249252
Revision: core.Revision,
250253
Subject: core.Subject,
251254
Title: core.Title,
252255
Language: core.Language,
253256
Version: core.Version,
254257
}, nil
255-
258+
if core.Created != nil {
259+
ret.Created = core.Created.Text
260+
}
261+
if core.Modified != nil {
262+
ret.Modified = core.Modified.Text
263+
}
256264
return
257265
}

sheetview.go

+5
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ func (f *File) getSheetView(sheet string, viewIndex int) (*xlsxSheetView, error)
163163
if err != nil {
164164
return nil, err
165165
}
166+
if ws.SheetViews == nil {
167+
ws.SheetViews = &xlsxSheetViews{
168+
SheetView: []xlsxSheetView{{WorkbookViewID: 0}},
169+
}
170+
}
166171
if viewIndex < 0 {
167172
if viewIndex < -len(ws.SheetViews.SheetView) {
168173
return nil, fmt.Errorf("view index %d out of range", viewIndex)

sheetview_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,9 @@ func TestSheetViewOptionsErrors(t *testing.T) {
210210
assert.NoError(t, f.SetSheetViewOptions(sheet, -1))
211211
assert.Error(t, f.SetSheetViewOptions(sheet, 1))
212212
assert.Error(t, f.SetSheetViewOptions(sheet, -2))
213+
214+
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
215+
assert.True(t, ok)
216+
ws.(*xlsxWorksheet).SheetViews = nil
217+
assert.NoError(t, f.GetSheetViewOptions(sheet, 0))
213218
}

xmlCore.go

+46-46
Original file line numberDiff line numberDiff line change
@@ -31,61 +31,61 @@ type DocProperties struct {
3131
Version string
3232
}
3333

34+
// decodeDcTerms directly maps the DCMI metadata terms for the coreProperties.
35+
type decodeDcTerms struct {
36+
Text string `xml:",chardata"`
37+
Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
38+
}
39+
3440
// decodeCoreProperties directly maps the root element for a part of this
3541
// content type shall coreProperties. In order to solve the problem that the
3642
// label structure is changed after serialization and deserialization, two
3743
// different structures are defined. decodeCoreProperties just for
3844
// deserialization.
3945
type decodeCoreProperties struct {
40-
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/metadata/core-properties coreProperties"`
41-
Title string `xml:"http://purl.org/dc/elements/1.1/ title,omitempty"`
42-
Subject string `xml:"http://purl.org/dc/elements/1.1/ subject,omitempty"`
43-
Creator string `xml:"http://purl.org/dc/elements/1.1/ creator"`
44-
Keywords string `xml:"keywords,omitempty"`
45-
Description string `xml:"http://purl.org/dc/elements/1.1/ description,omitempty"`
46-
LastModifiedBy string `xml:"lastModifiedBy"`
47-
Language string `xml:"http://purl.org/dc/elements/1.1/ language,omitempty"`
48-
Identifier string `xml:"http://purl.org/dc/elements/1.1/ identifier,omitempty"`
49-
Revision string `xml:"revision,omitempty"`
50-
Created struct {
51-
Text string `xml:",chardata"`
52-
Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
53-
} `xml:"http://purl.org/dc/terms/ created"`
54-
Modified struct {
55-
Text string `xml:",chardata"`
56-
Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
57-
} `xml:"http://purl.org/dc/terms/ modified"`
58-
ContentStatus string `xml:"contentStatus,omitempty"`
59-
Category string `xml:"category,omitempty"`
60-
Version string `xml:"version,omitempty"`
46+
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/metadata/core-properties coreProperties"`
47+
Title string `xml:"http://purl.org/dc/elements/1.1/ title,omitempty"`
48+
Subject string `xml:"http://purl.org/dc/elements/1.1/ subject,omitempty"`
49+
Creator string `xml:"http://purl.org/dc/elements/1.1/ creator"`
50+
Keywords string `xml:"keywords,omitempty"`
51+
Description string `xml:"http://purl.org/dc/elements/1.1/ description,omitempty"`
52+
LastModifiedBy string `xml:"lastModifiedBy"`
53+
Language string `xml:"http://purl.org/dc/elements/1.1/ language,omitempty"`
54+
Identifier string `xml:"http://purl.org/dc/elements/1.1/ identifier,omitempty"`
55+
Revision string `xml:"revision,omitempty"`
56+
Created *decodeDcTerms `xml:"http://purl.org/dc/terms/ created"`
57+
Modified *decodeDcTerms `xml:"http://purl.org/dc/terms/ modified"`
58+
ContentStatus string `xml:"contentStatus,omitempty"`
59+
Category string `xml:"category,omitempty"`
60+
Version string `xml:"version,omitempty"`
61+
}
62+
63+
// xlsxDcTerms directly maps the DCMI metadata terms for the coreProperties.
64+
type xlsxDcTerms struct {
65+
Text string `xml:",chardata"`
66+
Type string `xml:"xsi:type,attr"`
6167
}
6268

6369
// xlsxCoreProperties directly maps the root element for a part of this
6470
// content type shall coreProperties.
6571
type xlsxCoreProperties struct {
66-
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/metadata/core-properties coreProperties"`
67-
Dc string `xml:"xmlns:dc,attr"`
68-
Dcterms string `xml:"xmlns:dcterms,attr"`
69-
Dcmitype string `xml:"xmlns:dcmitype,attr"`
70-
XSI string `xml:"xmlns:xsi,attr"`
71-
Title string `xml:"dc:title,omitempty"`
72-
Subject string `xml:"dc:subject,omitempty"`
73-
Creator string `xml:"dc:creator"`
74-
Keywords string `xml:"keywords,omitempty"`
75-
Description string `xml:"dc:description,omitempty"`
76-
LastModifiedBy string `xml:"lastModifiedBy"`
77-
Language string `xml:"dc:language,omitempty"`
78-
Identifier string `xml:"dc:identifier,omitempty"`
79-
Revision string `xml:"revision,omitempty"`
80-
Created struct {
81-
Text string `xml:",chardata"`
82-
Type string `xml:"xsi:type,attr"`
83-
} `xml:"dcterms:created"`
84-
Modified struct {
85-
Text string `xml:",chardata"`
86-
Type string `xml:"xsi:type,attr"`
87-
} `xml:"dcterms:modified"`
88-
ContentStatus string `xml:"contentStatus,omitempty"`
89-
Category string `xml:"category,omitempty"`
90-
Version string `xml:"version,omitempty"`
72+
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/metadata/core-properties coreProperties"`
73+
Dc string `xml:"xmlns:dc,attr"`
74+
Dcterms string `xml:"xmlns:dcterms,attr"`
75+
Dcmitype string `xml:"xmlns:dcmitype,attr"`
76+
XSI string `xml:"xmlns:xsi,attr"`
77+
Title string `xml:"dc:title,omitempty"`
78+
Subject string `xml:"dc:subject,omitempty"`
79+
Creator string `xml:"dc:creator"`
80+
Keywords string `xml:"keywords,omitempty"`
81+
Description string `xml:"dc:description,omitempty"`
82+
LastModifiedBy string `xml:"lastModifiedBy"`
83+
Language string `xml:"dc:language,omitempty"`
84+
Identifier string `xml:"dc:identifier,omitempty"`
85+
Revision string `xml:"revision,omitempty"`
86+
Created *xlsxDcTerms `xml:"dcterms:created"`
87+
Modified *xlsxDcTerms `xml:"dcterms:modified"`
88+
ContentStatus string `xml:"contentStatus,omitempty"`
89+
Category string `xml:"category,omitempty"`
90+
Version string `xml:"version,omitempty"`
9191
}

0 commit comments

Comments
 (0)