Skip to content

Commit

Permalink
This closes #2046, add new function AddIgnoredErrors support to ignor…
Browse files Browse the repository at this point in the history
…ed error for a range of cells

- Add new exported IgnoredErrorsType enumeration
- Change the type of DataValidationType, DataValidationErrorStyle, DataValidationOperator, PictureInsertType from int to byte
  • Loading branch information
xuri committed Dec 21, 2024
1 parent 5ef4a36 commit 9934bf5
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 7 deletions.
6 changes: 3 additions & 3 deletions datavalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

// DataValidationType defined the type of data validation.
type DataValidationType int
type DataValidationType byte

// Data validation types.
const (
Expand All @@ -36,7 +36,7 @@ const (
)

// DataValidationErrorStyle defined the style of data validation error alert.
type DataValidationErrorStyle int
type DataValidationErrorStyle byte

// Data validation error styles.
const (
Expand All @@ -54,7 +54,7 @@ const (
)

// DataValidationOperator operator enum.
type DataValidationOperator int
type DataValidationOperator byte

// Data validation operators.
const (
Expand Down
2 changes: 1 addition & 1 deletion picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// PictureInsertType defines the type of the picture has been inserted into the
// worksheet.
type PictureInsertType int
type PictureInsertType byte

// Insert picture types.
const (
Expand Down
52 changes: 50 additions & 2 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ import (
"github.com/tiendc/go-deepcopy"
)

// IgnoredErrorsType is the type of ignored errors.
type IgnoredErrorsType byte

// Ignored errors types enumeration.
const (
IgnoredErrorsEvalError = iota
IgnoredErrorsTwoDigitTextYear
IgnoredErrorsNumberStoredAsText
IgnoredErrorsFormula
IgnoredErrorsFormulaRange
IgnoredErrorsUnlockedFormula
IgnoredErrorsEmptyCellReference
IgnoredErrorsListDataValidation
IgnoredErrorsCalculatedColumn
)

// NewSheet provides the function to create a new sheet by given a worksheet
// name and returns the index of the sheets in the workbook after it appended.
// Note that when creating a new workbook, the default worksheet named
Expand Down Expand Up @@ -2026,7 +2042,7 @@ func (f *File) relsReader(path string) (*xlsxRelationships, error) {
// fillSheetData ensures there are enough rows, and columns in the chosen
// row to accept data. Missing rows are backfilled and given their row number
// Uses the last populated row as a hint for the size of the next row to add
func (ws *xlsxWorksheet) prepareSheetXML(col int, row int) {
func (ws *xlsxWorksheet) prepareSheetXML(col, row int) {
rowCount := len(ws.SheetData.Row)
sizeHint := 0
var ht *float64
Expand Down Expand Up @@ -2072,7 +2088,7 @@ func (ws *xlsxWorksheet) makeContiguousColumns(fromRow, toRow, colCount int) {
// of used cells in the worksheet. The range reference is set using the A1
// reference style(e.g., "A1:D5"). Passing an empty range reference will remove
// the used range of the worksheet.
func (f *File) SetSheetDimension(sheet string, rangeRef string) error {
func (f *File) SetSheetDimension(sheet, rangeRef string) error {
ws, err := f.workSheetReader(sheet)
if err != nil {
return err
Expand Down Expand Up @@ -2115,3 +2131,35 @@ func (f *File) GetSheetDimension(sheet string) (string, error) {
}
return ref, err
}

// AddIgnoredErrors provides the method to ignored error for a range of cells.
func (f *File) AddIgnoredErrors(sheet, rangeRef string, ignoredErrorsType IgnoredErrorsType) error {
ws, err := f.workSheetReader(sheet)
if err != nil {
return err
}
if rangeRef == "" {
return ErrParameterInvalid
}
if ws.IgnoredErrors == nil {
ws.IgnoredErrors = &xlsxIgnoredErrors{}
}
ie := map[IgnoredErrorsType]xlsxIgnoredError{
IgnoredErrorsEvalError: {Sqref: rangeRef, EvalError: true},
IgnoredErrorsTwoDigitTextYear: {Sqref: rangeRef, TwoDigitTextYear: true},
IgnoredErrorsNumberStoredAsText: {Sqref: rangeRef, NumberStoredAsText: true},
IgnoredErrorsFormula: {Sqref: rangeRef, Formula: true},
IgnoredErrorsFormulaRange: {Sqref: rangeRef, FormulaRange: true},
IgnoredErrorsUnlockedFormula: {Sqref: rangeRef, UnlockedFormula: true},
IgnoredErrorsEmptyCellReference: {Sqref: rangeRef, EmptyCellReference: true},
IgnoredErrorsListDataValidation: {Sqref: rangeRef, ListDataValidation: true},
IgnoredErrorsCalculatedColumn: {Sqref: rangeRef, CalculatedColumn: true},
}[ignoredErrorsType]
for _, val := range ws.IgnoredErrors.IgnoredError {
if reflect.DeepEqual(val, ie) {
return err
}
}
ws.IgnoredErrors.IgnoredError = append(ws.IgnoredErrors.IgnoredError, ie)
return err
}
20 changes: 20 additions & 0 deletions sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,3 +821,23 @@ func TestSheetDimension(t *testing.T) {
assert.Empty(t, dimension)
assert.EqualError(t, err, "sheet SheetN does not exist")
}

func TestAddIgnoredErrors(t *testing.T) {
f := NewFile()
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsEvalError))
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsEvalError))
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsTwoDigitTextYear))
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsNumberStoredAsText))
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsFormula))
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsFormulaRange))
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsUnlockedFormula))
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsEmptyCellReference))
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsListDataValidation))
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsCalculatedColumn))

assert.Equal(t, ErrSheetNotExist{"SheetN"}, f.AddIgnoredErrors("SheetN", "A1", IgnoredErrorsEvalError))
assert.Equal(t, ErrParameterInvalid, f.AddIgnoredErrors("Sheet1", "", IgnoredErrorsEvalError))

assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddIgnoredErrors.xlsx")))
assert.NoError(t, f.Close())
}
24 changes: 23 additions & 1 deletion xmlWorksheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type xlsxWorksheet struct {
ColBreaks *xlsxColBreaks `xml:"colBreaks"`
CustomProperties *xlsxInnerXML `xml:"customProperties"`
CellWatches *xlsxInnerXML `xml:"cellWatches"`
IgnoredErrors *xlsxInnerXML `xml:"ignoredErrors"`
IgnoredErrors *xlsxIgnoredErrors `xml:"ignoredErrors"`
SmartTags *xlsxInnerXML `xml:"smartTags"`
Drawing *xlsxDrawing `xml:"drawing"`
LegacyDrawing *xlsxLegacyDrawing `xml:"legacyDrawing"`
Expand Down Expand Up @@ -679,6 +679,28 @@ type xlsxPicture struct {
RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
}

// xlsxIgnoredError specifies a single ignored error for a range of cells.
type xlsxIgnoredError struct {
XMLName xml.Name `xml:"ignoredError"`
Sqref string `xml:"sqref,attr"`
EvalError bool `xml:"evalError,attr,omitempty"`
TwoDigitTextYear bool `xml:"twoDigitTextYear,attr,omitempty"`
NumberStoredAsText bool `xml:"numberStoredAsText,attr,omitempty"`
Formula bool `xml:"formula,attr,omitempty"`
FormulaRange bool `xml:"formulaRange,attr,omitempty"`
UnlockedFormula bool `xml:"unlockedFormula,attr,omitempty"`
EmptyCellReference bool `xml:"emptyCellReference,attr,omitempty"`
ListDataValidation bool `xml:"listDataValidation,attr,omitempty"`
CalculatedColumn bool `xml:"calculatedColumn,attr,omitempty"`
}

// xlsxIgnoredErrors specifies a collection of ignored errors, by cell range.
type xlsxIgnoredErrors struct {
XMLName xml.Name `xml:"ignoredErrors"`
IgnoredError []xlsxIgnoredError `xml:"ignoredError"`
ExtLst *xlsxExtLst `xml:"extLst"`
}

// xlsxLegacyDrawing directly maps the legacyDrawing element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main - A comment is a
// rich text note that is attached to, and associated with, a cell, separate
Expand Down

0 comments on commit 9934bf5

Please sign in to comment.