Skip to content

Commit 9934bf5

Browse files
committed
This closes qax-os#2046, add new function AddIgnoredErrors support to ignored error for a range of cells
- Add new exported IgnoredErrorsType enumeration - Change the type of DataValidationType, DataValidationErrorStyle, DataValidationOperator, PictureInsertType from int to byte
1 parent 5ef4a36 commit 9934bf5

File tree

5 files changed

+97
-7
lines changed

5 files changed

+97
-7
lines changed

datavalidation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
// DataValidationType defined the type of data validation.
23-
type DataValidationType int
23+
type DataValidationType byte
2424

2525
// Data validation types.
2626
const (
@@ -36,7 +36,7 @@ const (
3636
)
3737

3838
// DataValidationErrorStyle defined the style of data validation error alert.
39-
type DataValidationErrorStyle int
39+
type DataValidationErrorStyle byte
4040

4141
// Data validation error styles.
4242
const (
@@ -54,7 +54,7 @@ const (
5454
)
5555

5656
// DataValidationOperator operator enum.
57-
type DataValidationOperator int
57+
type DataValidationOperator byte
5858

5959
// Data validation operators.
6060
const (

picture.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
// PictureInsertType defines the type of the picture has been inserted into the
2727
// worksheet.
28-
type PictureInsertType int
28+
type PictureInsertType byte
2929

3030
// Insert picture types.
3131
const (

sheet.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ import (
3030
"github.com/tiendc/go-deepcopy"
3131
)
3232

33+
// IgnoredErrorsType is the type of ignored errors.
34+
type IgnoredErrorsType byte
35+
36+
// Ignored errors types enumeration.
37+
const (
38+
IgnoredErrorsEvalError = iota
39+
IgnoredErrorsTwoDigitTextYear
40+
IgnoredErrorsNumberStoredAsText
41+
IgnoredErrorsFormula
42+
IgnoredErrorsFormulaRange
43+
IgnoredErrorsUnlockedFormula
44+
IgnoredErrorsEmptyCellReference
45+
IgnoredErrorsListDataValidation
46+
IgnoredErrorsCalculatedColumn
47+
)
48+
3349
// NewSheet provides the function to create a new sheet by given a worksheet
3450
// name and returns the index of the sheets in the workbook after it appended.
3551
// Note that when creating a new workbook, the default worksheet named
@@ -2026,7 +2042,7 @@ func (f *File) relsReader(path string) (*xlsxRelationships, error) {
20262042
// fillSheetData ensures there are enough rows, and columns in the chosen
20272043
// row to accept data. Missing rows are backfilled and given their row number
20282044
// Uses the last populated row as a hint for the size of the next row to add
2029-
func (ws *xlsxWorksheet) prepareSheetXML(col int, row int) {
2045+
func (ws *xlsxWorksheet) prepareSheetXML(col, row int) {
20302046
rowCount := len(ws.SheetData.Row)
20312047
sizeHint := 0
20322048
var ht *float64
@@ -2072,7 +2088,7 @@ func (ws *xlsxWorksheet) makeContiguousColumns(fromRow, toRow, colCount int) {
20722088
// of used cells in the worksheet. The range reference is set using the A1
20732089
// reference style(e.g., "A1:D5"). Passing an empty range reference will remove
20742090
// the used range of the worksheet.
2075-
func (f *File) SetSheetDimension(sheet string, rangeRef string) error {
2091+
func (f *File) SetSheetDimension(sheet, rangeRef string) error {
20762092
ws, err := f.workSheetReader(sheet)
20772093
if err != nil {
20782094
return err
@@ -2115,3 +2131,35 @@ func (f *File) GetSheetDimension(sheet string) (string, error) {
21152131
}
21162132
return ref, err
21172133
}
2134+
2135+
// AddIgnoredErrors provides the method to ignored error for a range of cells.
2136+
func (f *File) AddIgnoredErrors(sheet, rangeRef string, ignoredErrorsType IgnoredErrorsType) error {
2137+
ws, err := f.workSheetReader(sheet)
2138+
if err != nil {
2139+
return err
2140+
}
2141+
if rangeRef == "" {
2142+
return ErrParameterInvalid
2143+
}
2144+
if ws.IgnoredErrors == nil {
2145+
ws.IgnoredErrors = &xlsxIgnoredErrors{}
2146+
}
2147+
ie := map[IgnoredErrorsType]xlsxIgnoredError{
2148+
IgnoredErrorsEvalError: {Sqref: rangeRef, EvalError: true},
2149+
IgnoredErrorsTwoDigitTextYear: {Sqref: rangeRef, TwoDigitTextYear: true},
2150+
IgnoredErrorsNumberStoredAsText: {Sqref: rangeRef, NumberStoredAsText: true},
2151+
IgnoredErrorsFormula: {Sqref: rangeRef, Formula: true},
2152+
IgnoredErrorsFormulaRange: {Sqref: rangeRef, FormulaRange: true},
2153+
IgnoredErrorsUnlockedFormula: {Sqref: rangeRef, UnlockedFormula: true},
2154+
IgnoredErrorsEmptyCellReference: {Sqref: rangeRef, EmptyCellReference: true},
2155+
IgnoredErrorsListDataValidation: {Sqref: rangeRef, ListDataValidation: true},
2156+
IgnoredErrorsCalculatedColumn: {Sqref: rangeRef, CalculatedColumn: true},
2157+
}[ignoredErrorsType]
2158+
for _, val := range ws.IgnoredErrors.IgnoredError {
2159+
if reflect.DeepEqual(val, ie) {
2160+
return err
2161+
}
2162+
}
2163+
ws.IgnoredErrors.IgnoredError = append(ws.IgnoredErrors.IgnoredError, ie)
2164+
return err
2165+
}

sheet_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,3 +821,23 @@ func TestSheetDimension(t *testing.T) {
821821
assert.Empty(t, dimension)
822822
assert.EqualError(t, err, "sheet SheetN does not exist")
823823
}
824+
825+
func TestAddIgnoredErrors(t *testing.T) {
826+
f := NewFile()
827+
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsEvalError))
828+
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsEvalError))
829+
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsTwoDigitTextYear))
830+
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsNumberStoredAsText))
831+
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsFormula))
832+
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsFormulaRange))
833+
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsUnlockedFormula))
834+
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsEmptyCellReference))
835+
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsListDataValidation))
836+
assert.NoError(t, f.AddIgnoredErrors("Sheet1", "A1", IgnoredErrorsCalculatedColumn))
837+
838+
assert.Equal(t, ErrSheetNotExist{"SheetN"}, f.AddIgnoredErrors("SheetN", "A1", IgnoredErrorsEvalError))
839+
assert.Equal(t, ErrParameterInvalid, f.AddIgnoredErrors("Sheet1", "", IgnoredErrorsEvalError))
840+
841+
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddIgnoredErrors.xlsx")))
842+
assert.NoError(t, f.Close())
843+
}

xmlWorksheet.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type xlsxWorksheet struct {
4848
ColBreaks *xlsxColBreaks `xml:"colBreaks"`
4949
CustomProperties *xlsxInnerXML `xml:"customProperties"`
5050
CellWatches *xlsxInnerXML `xml:"cellWatches"`
51-
IgnoredErrors *xlsxInnerXML `xml:"ignoredErrors"`
51+
IgnoredErrors *xlsxIgnoredErrors `xml:"ignoredErrors"`
5252
SmartTags *xlsxInnerXML `xml:"smartTags"`
5353
Drawing *xlsxDrawing `xml:"drawing"`
5454
LegacyDrawing *xlsxLegacyDrawing `xml:"legacyDrawing"`
@@ -679,6 +679,28 @@ type xlsxPicture struct {
679679
RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
680680
}
681681

682+
// xlsxIgnoredError specifies a single ignored error for a range of cells.
683+
type xlsxIgnoredError struct {
684+
XMLName xml.Name `xml:"ignoredError"`
685+
Sqref string `xml:"sqref,attr"`
686+
EvalError bool `xml:"evalError,attr,omitempty"`
687+
TwoDigitTextYear bool `xml:"twoDigitTextYear,attr,omitempty"`
688+
NumberStoredAsText bool `xml:"numberStoredAsText,attr,omitempty"`
689+
Formula bool `xml:"formula,attr,omitempty"`
690+
FormulaRange bool `xml:"formulaRange,attr,omitempty"`
691+
UnlockedFormula bool `xml:"unlockedFormula,attr,omitempty"`
692+
EmptyCellReference bool `xml:"emptyCellReference,attr,omitempty"`
693+
ListDataValidation bool `xml:"listDataValidation,attr,omitempty"`
694+
CalculatedColumn bool `xml:"calculatedColumn,attr,omitempty"`
695+
}
696+
697+
// xlsxIgnoredErrors specifies a collection of ignored errors, by cell range.
698+
type xlsxIgnoredErrors struct {
699+
XMLName xml.Name `xml:"ignoredErrors"`
700+
IgnoredError []xlsxIgnoredError `xml:"ignoredError"`
701+
ExtLst *xlsxExtLst `xml:"extLst"`
702+
}
703+
682704
// xlsxLegacyDrawing directly maps the legacyDrawing element in the namespace
683705
// http://schemas.openxmlformats.org/spreadsheetml/2006/main - A comment is a
684706
// rich text note that is attached to, and associated with, a cell, separate

0 commit comments

Comments
 (0)