Skip to content

Commit

Permalink
Fix potential file corrupted and change worksheet name case-insensitive
Browse files Browse the repository at this point in the history
- Using sheet ID instead of sheet index when delete the cell in calculation chain
- Update documentation for exported functions
- Using `sheet` represent the sheet name in the function parameters
  • Loading branch information
xuri committed Jul 17, 2022
1 parent 0d4c97c commit ebea684
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 110 deletions.
2 changes: 1 addition & 1 deletion calcchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (f *File) deleteCalcChain(index int, axis string) {
calc := f.calcChainReader()
if calc != nil {
calc.C = xlsxCalcChainCollection(calc.C).Filter(func(c xlsxCalcChainC) bool {
return !((c.I == index && c.R == axis) || (c.I == index && axis == ""))
return !((c.I == index && c.R == axis) || (c.I == index && axis == "") || (c.I == 0 && c.R == axis))
})
}
if len(calc.C) == 0 {
Expand Down
55 changes: 29 additions & 26 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,21 @@ func (c *xlsxC) hasValue() bool {
}

// removeFormula delete formula for the cell.
func (c *xlsxC) removeFormula(ws *xlsxWorksheet) {
if c.F != nil && c.F.T == STCellFormulaTypeShared && c.F.Ref != "" {
si := c.F.Si
for r, row := range ws.SheetData.Row {
for col, cell := range row.C {
if cell.F != nil && cell.F.Si != nil && *cell.F.Si == *si {
ws.SheetData.Row[r].C[col].F = nil
func (f *File) removeFormula(c *xlsxC, ws *xlsxWorksheet, sheet string) {
if c.F != nil && c.Vm == nil {
f.deleteCalcChain(f.getSheetID(sheet), c.R)
if c.F.T == STCellFormulaTypeShared && c.F.Ref != "" {
si := c.F.Si
for r, row := range ws.SheetData.Row {
for col, cell := range row.C {
if cell.F != nil && cell.F.Si != nil && *cell.F.Si == *si {
ws.SheetData.Row[r].C[col].F = nil
}
}
}
}
c.F = nil
}
c.F = nil
}

// setCellIntFunc is a wrapper of SetCellInt.
Expand Down Expand Up @@ -281,8 +284,8 @@ func (f *File) SetCellInt(sheet, axis string, value int) error {
defer ws.Unlock()
cellData.S = f.prepareCellStyle(ws, col, row, cellData.S)
cellData.T, cellData.V = setCellInt(value)
cellData.removeFormula(ws)
cellData.IS = nil
f.removeFormula(cellData, ws, sheet)
return err
}

Expand All @@ -308,8 +311,8 @@ func (f *File) SetCellBool(sheet, axis string, value bool) error {
defer ws.Unlock()
cellData.S = f.prepareCellStyle(ws, col, row, cellData.S)
cellData.T, cellData.V = setCellBool(value)
cellData.removeFormula(ws)
cellData.IS = nil
f.removeFormula(cellData, ws, sheet)
return err
}

Expand Down Expand Up @@ -347,8 +350,8 @@ func (f *File) SetCellFloat(sheet, axis string, value float64, precision, bitSiz
defer ws.Unlock()
cellData.S = f.prepareCellStyle(ws, col, row, cellData.S)
cellData.T, cellData.V = setCellFloat(value, precision, bitSize)
cellData.removeFormula(ws)
cellData.IS = nil
f.removeFormula(cellData, ws, sheet)
return err
}

Expand All @@ -374,8 +377,8 @@ func (f *File) SetCellStr(sheet, axis, value string) error {
defer ws.Unlock()
cellData.S = f.prepareCellStyle(ws, col, row, cellData.S)
cellData.T, cellData.V, err = f.setCellString(value)
cellData.removeFormula(ws)
cellData.IS = nil
f.removeFormula(cellData, ws, sheet)
return err
}

Expand Down Expand Up @@ -474,8 +477,8 @@ func (f *File) SetCellDefault(sheet, axis, value string) error {
defer ws.Unlock()
cellData.S = f.prepareCellStyle(ws, col, row, cellData.S)
cellData.T, cellData.V = setCellDefault(value)
cellData.removeFormula(ws)
cellData.IS = nil
f.removeFormula(cellData, ws, sheet)
return err
}

Expand Down Expand Up @@ -510,13 +513,12 @@ type FormulaOpts struct {
}

// SetCellFormula provides a function to set formula on the cell is taken
// according to the given worksheet name (case-sensitive) and cell formula
// settings. The result of the formula cell can be calculated when the
// worksheet is opened by the Office Excel application or can be using
// the "CalcCellValue" function also can get the calculated cell value. If
// the Excel application doesn't calculate the formula automatically when the
// workbook has been opened, please call "UpdateLinkedValue" after setting
// the cell formula functions.
// according to the given worksheet name and cell formula settings. The result
// of the formula cell can be calculated when the worksheet is opened by the
// Office Excel application or can be using the "CalcCellValue" function also
// can get the calculated cell value. If the Excel application doesn't
// calculate the formula automatically when the workbook has been opened,
// please call "UpdateLinkedValue" after setting the cell formula functions.
//
// Example 1, set normal formula "=SUM(A1,B1)" for the cell "A3" on "Sheet1":
//
Expand Down Expand Up @@ -662,11 +664,12 @@ func (ws *xlsxWorksheet) countSharedFormula() (count int) {
return
}

// GetCellHyperLink provides a function to get cell hyperlink by given
// worksheet name and axis. Boolean type value link will be true if the cell
// has a hyperlink and the target is the address of the hyperlink. Otherwise,
// the value of link will be false and the value of the target will be a blank
// string. For example get hyperlink of Sheet1!H6:
// GetCellHyperLink gets a cell hyperlink based on the given worksheet name and
// cell coordinates. If the cell has a hyperlink, it will return 'true' and
// the link address, otherwise it will return 'false' and an empty link
// address.
//
// For example, get a hyperlink to a 'H6' cell on a worksheet named 'Sheet1':
//
// link, target, err := f.GetCellHyperLink("Sheet1", "H6")
//
Expand Down Expand Up @@ -765,7 +768,7 @@ func (f *File) SetCellHyperLink(sheet, axis, link, linkType string, opts ...Hype

switch linkType {
case "External":
sheetPath := f.sheetMap[trimSheetName(sheet)]
sheetPath, _ := f.getSheetXMLPath(sheet)
sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetPath, "xl/worksheets/") + ".rels"
rID := f.setRels(linkData.RID, sheetRels, SourceRelationshipHyperLink, link, linkType)
linkData = xlsxHyperlink{
Expand Down
12 changes: 10 additions & 2 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ type Cols struct {
sheetXML []byte
}

// GetCols return all the columns in a sheet by given worksheet name (case-sensitive). For example:
// GetCols gets the value of all cells by columns on the worksheet based on the
// given worksheet name, returned as a two-dimensional array, where the value
// of the cell is converted to the `string` type. If the cell format can be
// applied to the value of the cell, the applied value will be used, otherwise
// the original value will be used.
//
// For example, get and traverse the value of all cells by columns on a
// worksheet named
// 'Sheet1':
//
// cols, err := f.GetCols("Sheet1")
// if err != nil {
Expand Down Expand Up @@ -196,7 +204,7 @@ func columnXMLHandler(colIterator *columnXMLIterator, xmlElement *xml.StartEleme
// }
//
func (f *File) Cols(sheet string) (*Cols, error) {
name, ok := f.sheetMap[trimSheetName(sheet)]
name, ok := f.getSheetXMLPath(sheet)
if !ok {
return nil, ErrSheetNotExist{sheet}
}
Expand Down
3 changes: 2 additions & 1 deletion comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func (f *File) AddComment(sheet, cell, format string) error {
drawingVML = strings.ReplaceAll(sheetRelationshipsDrawingVML, "..", "xl")
} else {
// Add first comment for given sheet.
sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels"
sheetXMLPath, _ := f.getSheetXMLPath(sheet)
sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetXMLPath, "xl/worksheets/") + ".rels"
rID := f.addRels(sheetRels, SourceRelationshipDrawingVML, sheetRelationshipsDrawingVML, "")
f.addRels(sheetRels, SourceRelationshipComments, sheetRelationshipsComments, "")
f.addSheetNameSpace(sheet, SourceRelationship)
Expand Down
6 changes: 4 additions & 2 deletions drawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func (f *File) prepareDrawing(ws *xlsxWorksheet, drawingID int, sheet, drawingXM
drawingXML = strings.ReplaceAll(sheetRelationshipsDrawingXML, "..", "xl")
} else {
// Add first picture for given sheet.
sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels"
sheetXMLPath, _ := f.getSheetXMLPath(sheet)
sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetXMLPath, "xl/worksheets/") + ".rels"
rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
f.addSheetDrawing(sheet, rID)
}
Expand All @@ -45,7 +46,8 @@ func (f *File) prepareDrawing(ws *xlsxWorksheet, drawingID int, sheet, drawingXM
func (f *File) prepareChartSheetDrawing(cs *xlsxChartsheet, drawingID int, sheet string) {
sheetRelationshipsDrawingXML := "../drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
// Only allow one chart in a chartsheet.
sheetRels := "xl/chartsheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/chartsheets/") + ".rels"
sheetXMLPath, _ := f.getSheetXMLPath(sheet)
sheetRels := "xl/chartsheets/_rels/" + strings.TrimPrefix(sheetXMLPath, "xl/chartsheets/") + ".rels"
rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
f.addSheetNameSpace(sheet, SourceRelationship)
cs.Drawing = &xlsxDrawing{
Expand Down
2 changes: 1 addition & 1 deletion excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (f *File) workSheetReader(sheet string) (ws *xlsxWorksheet, err error) {
name string
ok bool
)
if name, ok = f.sheetMap[trimSheetName(sheet)]; !ok {
if name, ok = f.getSheetXMLPath(sheet); !ok {
err = fmt.Errorf("sheet %s is not exist", sheet)
return
}
Expand Down
6 changes: 3 additions & 3 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func JoinCellName(col string, row int) (string, error) {
}

// ColumnNameToNumber provides a function to convert Excel sheet column name
// to int. Column name case-insensitive. The function returns an error if
// column name incorrect.
// (case-insensitive) to int. The function returns an error if column name
// incorrect.
//
// Example:
//
Expand Down Expand Up @@ -690,7 +690,7 @@ func (f *File) setIgnorableNameSpace(path string, index int, ns xml.Attr) {

// addSheetNameSpace add XML attribute for worksheet.
func (f *File) addSheetNameSpace(sheet string, ns xml.Attr) {
name := f.sheetMap[trimSheetName(sheet)]
name, _ := f.getSheetXMLPath(sheet)
f.addNameSpaces(name, ns)
}

Expand Down
4 changes: 2 additions & 2 deletions picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (f *File) AddPictureFromBytes(sheet, cell, format, name, extension string,
// xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and
// relationship index.
func (f *File) deleteSheetRelationships(sheet, rID string) {
name, ok := f.sheetMap[trimSheetName(sheet)]
name, ok := f.getSheetXMLPath(sheet)
if !ok {
name = strings.ToLower(sheet) + ".xml"
}
Expand Down Expand Up @@ -450,7 +450,7 @@ func (f *File) addContentTypePart(index int, contentType string) {
// value in xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and
// relationship index.
func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string {
name, ok := f.sheetMap[trimSheetName(sheet)]
name, ok := f.getSheetXMLPath(sheet)
if !ok {
name = strings.ToLower(sheet) + ".xml"
}
Expand Down
2 changes: 1 addition & 1 deletion pivotTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (f *File) parseFormatPivotTableSet(opt *PivotTableOption) (*xlsxWorksheet,
if err != nil {
return dataSheet, "", err
}
pivotTableSheetPath, ok := f.sheetMap[trimSheetName(pivotTableSheetName)]
pivotTableSheetPath, ok := f.getSheetXMLPath(pivotTableSheetName)
if !ok {
return dataSheet, pivotTableSheetPath, fmt.Errorf("sheet %s is not exist", pivotTableSheetName)
}
Expand Down
19 changes: 11 additions & 8 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ import (
"github.com/mohae/deepcopy"
)

// GetRows return all the rows in a sheet by given worksheet name
// (case sensitive), returned as a two-dimensional array, where the value of
// the cell is converted to the string type. If the cell format can be applied
// to the value of the cell, the applied value will be used, otherwise the
// original value will be used. GetRows fetched the rows with value or formula
// cells, the continually blank cells in the tail of each row will be skipped,
// so the length of each row may be inconsistent. For example:
// GetRows return all the rows in a sheet by given worksheet name, returned as
// a two-dimensional array, where the value of the cell is converted to the
// string type. If the cell format can be applied to the value of the cell,
// the applied value will be used, otherwise the original value will be used.
// GetRows fetched the rows with value or formula cells, the continually blank
// cells in the tail of each row will be skipped, so the length of each row
// may be inconsistent.
//
// For example, get and traverse the value of all cells by rows on a worksheet
// named 'Sheet1':
//
// rows, err := f.GetRows("Sheet1")
// if err != nil {
Expand Down Expand Up @@ -233,7 +236,7 @@ func (rows *Rows) rowXMLHandler(rowIterator *rowXMLIterator, xmlElement *xml.Sta
// }
//
func (f *File) Rows(sheet string) (*Rows, error) {
name, ok := f.sheetMap[trimSheetName(sheet)]
name, ok := f.getSheetXMLPath(sheet)
if !ok {
return nil, ErrSheetNotExist{sheet}
}
Expand Down
3 changes: 2 additions & 1 deletion shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ func (f *File) AddShape(sheet, cell, format string) error {
drawingXML = strings.ReplaceAll(sheetRelationshipsDrawingXML, "..", "xl")
} else {
// Add first shape for given sheet.
sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(f.sheetMap[trimSheetName(sheet)], "xl/worksheets/") + ".rels"
sheetXMLPath, _ := f.getSheetXMLPath(sheet)
sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetXMLPath, "xl/worksheets/") + ".rels"
rID := f.addRels(sheetRels, SourceRelationshipDrawingML, sheetRelationshipsDrawingXML, "")
f.addSheetDrawing(sheet, rID)
f.addSheetNameSpace(sheet, SourceRelationship)
Expand Down
Loading

0 comments on commit ebea684

Please sign in to comment.