Skip to content

Commit d6931a8

Browse files
committed
Add optional value checking for CalcMode and RefMode calculation properties
- Add valid optional value prompt in error message for the functions: `AddFormControl`, `AddPicture` and `AddPictureFromBytes` - Rename the internal error message function `newInvalidPageLayoutValueError` to `newInvalidOptionalValue` - Update unit tests
1 parent aef20e2 commit d6931a8

9 files changed

+36
-16
lines changed

errors.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package excelize
1414
import (
1515
"errors"
1616
"fmt"
17+
"strings"
1718
)
1819

1920
var (
@@ -255,10 +256,10 @@ func newInvalidNameError(name string) error {
255256
return fmt.Errorf("invalid name %q, the name should be starts with a letter or underscore, can not include a space or character, and can not conflict with an existing name in the workbook", name)
256257
}
257258

258-
// newInvalidPageLayoutValueError defined the error message on receiving the invalid
259-
// page layout options value.
260-
func newInvalidPageLayoutValueError(name, value, msg string) error {
261-
return fmt.Errorf("invalid %s value %q, acceptable value should be one of %s", name, value, msg)
259+
// newInvalidOptionalValue defined the error message on receiving the invalid
260+
// optional value.
261+
func newInvalidOptionalValue(name, value string, values []string) error {
262+
return fmt.Errorf("invalid %s value %q, acceptable value should be one of %s", name, value, strings.Join(values, ", "))
262263
}
263264

264265
// newInvalidRowNumberError defined the error message on receiving the invalid

picture.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, ext string, rID, hyper
355355
return err
356356
}
357357
if opts.Positioning != "" && inStrSlice(supportedPositioning, opts.Positioning, true) == -1 {
358-
return ErrParameterInvalid
358+
return newInvalidOptionalValue("Positioning", opts.Positioning, supportedPositioning)
359359
}
360360
width, height := img.Width, img.Height
361361
if opts.AutoFit {

picture_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ func TestAddDrawingPicture(t *testing.T) {
290290
opts := &GraphicOptions{PrintObject: boolPtr(true), Locked: boolPtr(false)}
291291
assert.EqualError(t, f.addDrawingPicture("sheet1", "", "A", "", 0, 0, image.Config{}, opts), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
292292
// Test addDrawingPicture with invalid positioning types
293-
assert.Equal(t, f.addDrawingPicture("sheet1", "", "A1", "", 0, 0, image.Config{}, &GraphicOptions{Positioning: "x"}), ErrParameterInvalid)
293+
assert.Equal(t, newInvalidOptionalValue("Positioning", "x", supportedPositioning),
294+
f.addDrawingPicture("sheet1", "", "A1", "", 0, 0, image.Config{}, &GraphicOptions{Positioning: "x"}))
294295

295296
path := "xl/drawings/drawing1.xml"
296297
f.Pkg.Store(path, MacintoshCyrillicCharset)

sheet.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ func (ws *xlsxWorksheet) setPageSetUp(opts *PageLayoutOptions) error {
16461646
}
16471647
if opts.Orientation != nil {
16481648
if inStrSlice(supportedPageOrientation, *opts.Orientation, true) == -1 {
1649-
return newInvalidPageLayoutValueError("Orientation", *opts.Orientation, strings.Join(supportedPageOrientation, ", "))
1649+
return newInvalidOptionalValue("Orientation", *opts.Orientation, supportedPageOrientation)
16501650
}
16511651
ws.newPageSetUp()
16521652
ws.PageSetUp.Orientation = *opts.Orientation
@@ -1677,7 +1677,7 @@ func (ws *xlsxWorksheet) setPageSetUp(opts *PageLayoutOptions) error {
16771677
}
16781678
if opts.PageOrder != nil {
16791679
if inStrSlice(supportedPageOrder, *opts.PageOrder, true) == -1 {
1680-
return newInvalidPageLayoutValueError("PageOrder", *opts.PageOrder, strings.Join(supportedPageOrder, ", "))
1680+
return newInvalidOptionalValue("PageOrder", *opts.PageOrder, supportedPageOrder)
16811681
}
16821682
ws.newPageSetUp()
16831683
ws.PageSetUp.PageOrder = *opts.PageOrder

templates.go

+6
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,12 @@ var supportedImageTypes = map[string]string{
476476
".tif": ".tiff", ".tiff": ".tiff", ".wmf": ".wmf", ".wmz": ".wmz",
477477
}
478478

479+
// supportedCalcMode defined supported formula calculate mode.
480+
var supportedCalcMode = []string{"manual", "auto", "autoNoTable"}
481+
482+
// supportedRefMode defined supported formula calculate mode.
483+
var supportedRefMode = []string{"A1", "R1C1"}
484+
479485
// supportedContentTypes defined supported file format types.
480486
var supportedContentTypes = map[string]string{
481487
".xlam": ContentTypeAddinMacro,

vml.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ func (f *File) addFormCtrlShape(preset formCtrlPreset, col, row int, anchor stri
809809
if opts.Format.Positioning != "" {
810810
idx := inStrSlice(supportedPositioning, opts.Format.Positioning, true)
811811
if idx == -1 {
812-
return &sp, ErrParameterInvalid
812+
return &sp, newInvalidOptionalValue("Positioning", opts.Format.Positioning, supportedPositioning)
813813
}
814814
sp.ClientData.MoveWithCells = []*string{stringPtr(""), nil, nil}[idx]
815815
sp.ClientData.SizeWithCells = []*string{stringPtr(""), stringPtr(""), nil}[idx]

vml_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,11 @@ func TestFormControl(t *testing.T) {
271271
Cell: "A1", Type: FormControlButton, Macro: "Button1_Click",
272272
}))
273273
// Test add form control with invalid positioning types
274-
assert.Equal(t, f.AddFormControl("Sheet1", FormControl{
275-
Cell: "A1", Type: FormControlButton,
276-
Format: GraphicOptions{Positioning: "x"},
277-
}), ErrParameterInvalid)
274+
assert.Equal(t, newInvalidOptionalValue("Positioning", "x", supportedPositioning),
275+
f.AddFormControl("Sheet1", FormControl{
276+
Cell: "A1", Type: FormControlButton,
277+
Format: GraphicOptions{Positioning: "x"},
278+
}))
278279
// Test add spin form control with illegal cell link reference
279280
assert.Equal(t, f.AddFormControl("Sheet1", FormControl{
280281
Cell: "C5", Type: FormControlSpinButton, CellLink: "*",

workbook.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error) {
5353
return opts, err
5454
}
5555

56-
// SetCalcProps provides a function to sets calculation properties.
56+
// SetCalcProps provides a function to sets calculation properties. Optional
57+
// value of "CalcMode" property is: "manual", "auto" or "autoNoTable". Optional
58+
// value of "RefMode" property is: "A1" or "R1C1".
5759
func (f *File) SetCalcProps(opts *CalcPropsOptions) error {
5860
if opts == nil {
5961
return nil
@@ -65,6 +67,12 @@ func (f *File) SetCalcProps(opts *CalcPropsOptions) error {
6567
if wb.CalcPr == nil {
6668
wb.CalcPr = new(xlsxCalcPr)
6769
}
70+
if opts.CalcMode != nil && inStrSlice(supportedCalcMode, *opts.CalcMode, true) == -1 {
71+
return newInvalidOptionalValue("CalcMode", *opts.CalcMode, supportedCalcMode)
72+
}
73+
if opts.RefMode != nil && inStrSlice(supportedRefMode, *opts.RefMode, true) == -1 {
74+
return newInvalidOptionalValue("RefMode", *opts.RefMode, supportedRefMode)
75+
}
6876
setNoPtrFieldsVal([]string{
6977
"CalcCompleted", "CalcOnSave", "ForceFullCalc", "FullCalcOnLoad", "FullPrecision", "Iterate",
7078
"IterateDelta",

workbook_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ func TestCalcProps(t *testing.T) {
5757
opts, err = f.GetCalcProps()
5858
assert.NoError(t, err)
5959
assert.Equal(t, CalcPropsOptions{}, opts)
60-
// Test set workbook properties with unsupported charset workbook
60+
// Test set calculation properties with unsupported optional value
61+
assert.Equal(t, newInvalidOptionalValue("CalcMode", "AUTO", supportedCalcMode), f.SetCalcProps(&CalcPropsOptions{CalcMode: stringPtr("AUTO")}))
62+
assert.Equal(t, newInvalidOptionalValue("RefMode", "a1", supportedRefMode), f.SetCalcProps(&CalcPropsOptions{RefMode: stringPtr("a1")}))
63+
// Test set calculation properties with unsupported charset workbook
6164
f.WorkBook = nil
6265
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
6366
assert.EqualError(t, f.SetCalcProps(&expected), "XML syntax error on line 1: invalid UTF-8")
64-
// Test get workbook properties with unsupported charset workbook
67+
// Test get calculation properties with unsupported charset workbook
6568
f.WorkBook = nil
6669
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
6770
_, err = f.GetCalcProps()

0 commit comments

Comments
 (0)