Skip to content

Commit

Permalink
Remove internal error log print, throw XML deserialize error
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Nov 12, 2022
1 parent bd5dd17 commit ac564af
Show file tree
Hide file tree
Showing 31 changed files with 458 additions and 203 deletions.
5 changes: 4 additions & 1 deletion calcchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ func (f *File) deleteCalcChain(index int, cell string) error {
if len(calc.C) == 0 {
f.CalcChain = nil
f.Pkg.Delete(defaultXMLPathCalcChain)
content := f.contentTypesReader()
content, err := f.contentTypesReader()
if err != nil {
return err
}
content.Lock()
defer content.Unlock()
for k, v := range content.Overrides {
Expand Down
7 changes: 7 additions & 0 deletions calcchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ func TestDeleteCalcChain(t *testing.T) {

formulaType, ref := STCellFormulaTypeShared, "C1:C5"
assert.NoError(t, f.SetCellFormula("Sheet1", "C1", "=A1+B1", FormulaOpts{Ref: &ref, Type: &formulaType}))

// Test delete calculation chain with unsupported charset calculation chain.
f.CalcChain = nil
f.Pkg.Store(defaultXMLPathCalcChain, MacintoshCyrillicCharset)
assert.EqualError(t, f.SetCellValue("Sheet1", "C1", true), "XML syntax error on line 1: invalid UTF-8")

// Test delete calculation chain with unsupported charset content types.
f.ContentTypes = nil
f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset)
assert.EqualError(t, f.deleteCalcChain(1, "A1"), "XML syntax error on line 1: invalid UTF-8")
}
13 changes: 10 additions & 3 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,14 @@ func (f *File) setCellTimeFunc(sheet, cell string, value time.Time) error {
ws.Lock()
c.S = f.prepareCellStyle(ws, col, row, c.S)
ws.Unlock()
date1904, wb := false, f.workbookReader()
var date1904, isNum bool
wb, err := f.workbookReader()
if err != nil {
return err
}
if wb != nil && wb.WorkbookPr != nil {
date1904 = wb.WorkbookPr.Date1904
}
var isNum bool
if isNum, err = c.setCellTime(value, date1904); err != nil {
return err
}
Expand Down Expand Up @@ -1320,7 +1323,11 @@ func (f *File) formattedValue(s int, v string, raw bool) (string, error) {
if styleSheet.CellXfs.Xf[s].NumFmtID != nil {
numFmtID = *styleSheet.CellXfs.Xf[s].NumFmtID
}
date1904, wb := false, f.workbookReader()
date1904 := false
wb, err := f.workbookReader()
if err != nil {
return v, err
}
if wb != nil && wb.WorkbookPr != nil {
date1904 = wb.WorkbookPr.Date1904
}
Expand Down
16 changes: 13 additions & 3 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestSetCellValue(t *testing.T) {
f := NewFile()
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Duration(1e13)), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
// Test set cell value with column and row style inherit
// Test set cell value with column and row style inherit.
style1, err := f.NewStyle(&Style{NumFmt: 2})
assert.NoError(t, err)
style2, err := f.NewStyle(&Style{NumFmt: 9})
Expand All @@ -189,10 +189,14 @@ func TestSetCellValue(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "0.50", B2)

// Test set cell value with unsupported charset shared strings table
// Test set cell value with unsupported charset shared strings table.
f.SharedStrings = nil
f.Pkg.Store(defaultXMLPathSharedStrings, MacintoshCyrillicCharset)
assert.EqualError(t, f.SetCellValue("Sheet1", "A1", "A1"), "XML syntax error on line 1: invalid UTF-8")
// Test set cell value with unsupported charset workbook.
f.WorkBook = nil
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
assert.EqualError(t, f.SetCellValue("Sheet1", "A1", time.Now().UTC()), "XML syntax error on line 1: invalid UTF-8")
}

func TestSetCellValues(t *testing.T) {
Expand All @@ -204,7 +208,7 @@ func TestSetCellValues(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, v, "12/31/10 00:00")

// Test date value lower than min date supported by Excel
// Test date value lower than min date supported by Excel.
err = f.SetCellValue("Sheet1", "A1", time.Date(1600, time.December, 31, 0, 0, 0, 0, time.UTC))
assert.NoError(t, err)

Expand Down Expand Up @@ -782,6 +786,12 @@ func TestFormattedValue(t *testing.T) {
assert.Equal(t, "0_0", fn("0_0", "", false))
}

// Test format value with unsupported charset workbook.
f.WorkBook = nil
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
_, err = f.formattedValue(1, "43528", false)
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")

// Test format value with unsupported charset style sheet.
f.Styles = nil
f.Pkg.Store(defaultXMLPathStyles, MacintoshCyrillicCharset)
Expand Down
20 changes: 13 additions & 7 deletions chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,10 @@ func (f *File) AddChart(sheet, cell, opts string, combo ...string) error {
return err
}
f.addChart(options, comboCharts)
f.addContentTypePart(chartID, "chart")
f.addContentTypePart(drawingID, "drawings")
if err = f.addContentTypePart(chartID, "chart"); err != nil {
return err
}
_ = f.addContentTypePart(drawingID, "drawings")
f.addSheetNameSpace(sheet, SourceRelationship)
return err
}
Expand All @@ -952,7 +954,7 @@ func (f *File) AddChartSheet(sheet, opts string, combo ...string) error {
},
}
f.SheetCount++
wb := f.workbookReader()
wb, _ := f.workbookReader()
sheetID := 0
for _, v := range wb.Sheets.Sheet {
if v.SheetID > sheetID {
Expand All @@ -969,11 +971,15 @@ func (f *File) AddChartSheet(sheet, opts string, combo ...string) error {
f.prepareChartSheetDrawing(&cs, drawingID, sheet)
drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
f.addSheetDrawingChart(drawingXML, drawingRID, &options.Format)
if err = f.addSheetDrawingChart(drawingXML, drawingRID, &options.Format); err != nil {
return err
}
f.addChart(options, comboCharts)
f.addContentTypePart(chartID, "chart")
f.addContentTypePart(sheetID, "chartsheet")
f.addContentTypePart(drawingID, "drawings")
if err = f.addContentTypePart(chartID, "chart"); err != nil {
return err
}
_ = f.addContentTypePart(sheetID, "chartsheet")
_ = f.addContentTypePart(drawingID, "drawings")
// Update workbook.xml.rels
rID := f.addRels(f.getWorkbookRelsPath(), SourceRelationshipChartsheet, fmt.Sprintf("/xl/chartsheets/sheet%d.xml", sheetID), "")
// Update workbook.xml
Expand Down
13 changes: 13 additions & 0 deletions chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ func TestAddChart(t *testing.T) {
// Test add combo chart with unsupported chart type
assert.EqualError(t, f.AddChart("Sheet2", "BD64", `{"type":"barOfPie","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$A$30:$D$37","values":"Sheet1!$B$30:$B$37"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Bar of Pie Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero","x_axis":{"major_grid_lines":true},"y_axis":{"major_grid_lines":true}}`, `{"type":"unknown","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$A$30:$D$37","values":"Sheet1!$B$30:$B$37"}],"format":{"x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend":{"position":"left","show_legend_key":false},"title":{"name":"Bar of Pie Chart"},"plotarea":{"show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero","x_axis":{"major_grid_lines":true},"y_axis":{"major_grid_lines":true}}`), "unsupported chart type unknown")
assert.NoError(t, f.Close())

// Test add chart with unsupported charset content types.
f.ContentTypes = nil
f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset)
assert.EqualError(t, f.AddChart("Sheet1", "P1", `{"type":"col","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"}],"title":{"name":"2D Column Chart"}}`), "XML syntax error on line 1: invalid UTF-8")
}

func TestAddChartSheet(t *testing.T) {
Expand Down Expand Up @@ -259,6 +264,14 @@ func TestAddChartSheet(t *testing.T) {
assert.NoError(t, f.UpdateLinkedValue())

assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddChartSheet.xlsx")))
// Test add chart sheet with unsupported charset drawing XML.
f.Pkg.Store("xl/drawings/drawing4.xml", MacintoshCyrillicCharset)
assert.EqualError(t, f.AddChartSheet("Chart3", `{"type":"col","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"}],"title":{"name":"2D Column Chart"}}`), "XML syntax error on line 1: invalid UTF-8")
// Test add chart sheet with unsupported charset content types.
f = NewFile()
f.ContentTypes = nil
f.Pkg.Store(defaultXMLPathContentTypes, MacintoshCyrillicCharset)
assert.EqualError(t, f.AddChartSheet("Chart4", `{"type":"col","series":[{"name":"Sheet1!$A$30","categories":"Sheet1!$B$29:$D$29","values":"Sheet1!$B$30:$D$30"}],"title":{"name":"2D Column Chart"}}`), "XML syntax error on line 1: invalid UTF-8")
}

func TestDeleteChart(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func (f *File) GetComments() (map[string][]Comment, error) {
// getSheetComments provides the method to get the target comment reference by
// given worksheet file path.
func (f *File) getSheetComments(sheetFile string) string {
rels := "xl/worksheets/_rels/" + sheetFile + ".rels"
if sheetRels := f.relsReader(rels); sheetRels != nil {
rels, _ := f.relsReader("xl/worksheets/_rels/" + sheetFile + ".rels")
if sheetRels := rels; sheetRels != nil {
sheetRels.Lock()
defer sheetRels.Unlock()
for _, v := range sheetRels.Relationships {
Expand Down Expand Up @@ -135,8 +135,7 @@ func (f *File) AddComment(sheet string, comment Comment) error {
if err = f.addComment(commentsXML, comment); err != nil {
return err
}
f.addContentTypePart(commentID, "comments")
return err
return f.addContentTypePart(commentID, "comments")
}

// DeleteComment provides the method to delete comment in a sheet by given
Expand Down
3 changes: 2 additions & 1 deletion comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func TestDecodeVMLDrawingReader(t *testing.T) {
f := NewFile()
path := "xl/drawings/vmlDrawing1.xml"
f.Pkg.Store(path, MacintoshCyrillicCharset)
f.decodeVMLDrawingReader(path)
_, err := f.decodeVMLDrawingReader(path)
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
}

func TestCommentsReader(t *testing.T) {
Expand Down
36 changes: 24 additions & 12 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@ func OpenReader(r io.Reader, opts ...Options) (*File, error) {
return f, err
}
f.sheetMap = f.getSheetMap()
f.Styles, err = f.stylesReader()
f.Theme = f.themeReader()
if f.Styles, err = f.stylesReader(); err != nil {
return f, err
}
f.Theme, err = f.themeReader()
return f, err
}

Expand Down Expand Up @@ -335,7 +337,7 @@ func checkSheetR0(ws *xlsxWorksheet, sheetData *xlsxSheetData, r0 *xlsxRow) {
// setRels provides a function to set relationships by given relationship ID,
// XML path, relationship type, target and target mode.
func (f *File) setRels(rID, relPath, relType, target, targetMode string) int {
rels := f.relsReader(relPath)
rels, _ := f.relsReader(relPath)
if rels == nil || rID == "" {
return f.addRels(relPath, relType, target, targetMode)
}
Expand All @@ -360,7 +362,7 @@ func (f *File) addRels(relPath, relType, target, targetMode string) int {
uniqPart := map[string]string{
SourceRelationshipSharedStrings: "/xl/sharedStrings.xml",
}
rels := f.relsReader(relPath)
rels, _ := f.relsReader(relPath)
if rels == nil {
rels = &xlsxRelationships{}
}
Expand Down Expand Up @@ -418,7 +420,10 @@ func (f *File) addRels(relPath, relType, target, targetMode string) int {
// </c>
// </row>
func (f *File) UpdateLinkedValue() error {
wb := f.workbookReader()
wb, err := f.workbookReader()
if err != nil {
return err
}
// recalculate formulas
wb.CalcPr = nil
for _, name := range f.GetSheetList() {
Expand Down Expand Up @@ -465,12 +470,15 @@ func (f *File) AddVBAProject(bin string) error {
if path.Ext(bin) != ".bin" {
return ErrAddVBAProject
}
wb := f.relsReader(f.getWorkbookRelsPath())
wb.Lock()
defer wb.Unlock()
rels, err := f.relsReader(f.getWorkbookRelsPath())
if err != nil {
return err
}
rels.Lock()
defer rels.Unlock()
var rID int
var ok bool
for _, rel := range wb.Relationships {
for _, rel := range rels.Relationships {
if rel.Target == "vbaProject.bin" && rel.Type == SourceRelationshipVBAProject {
ok = true
continue
Expand All @@ -482,7 +490,7 @@ func (f *File) AddVBAProject(bin string) error {
}
rID++
if !ok {
wb.Relationships = append(wb.Relationships, xlsxRelationship{
rels.Relationships = append(rels.Relationships, xlsxRelationship{
ID: "rId" + strconv.Itoa(rID),
Target: "vbaProject.bin",
Type: SourceRelationshipVBAProject,
Expand All @@ -495,9 +503,12 @@ func (f *File) AddVBAProject(bin string) error {

// setContentTypePartProjectExtensions provides a function to set the content
// type for relationship parts and the main document part.
func (f *File) setContentTypePartProjectExtensions(contentType string) {
func (f *File) setContentTypePartProjectExtensions(contentType string) error {
var ok bool
content := f.contentTypesReader()
content, err := f.contentTypesReader()
if err != nil {
return err
}
content.Lock()
defer content.Unlock()
for _, v := range content.Defaults {
Expand All @@ -516,4 +527,5 @@ func (f *File) setContentTypePartProjectExtensions(contentType string) {
ContentType: ContentTypeVBA,
})
}
return err
}
Loading

0 comments on commit ac564af

Please sign in to comment.