Skip to content

Commit 3f8f4f5

Browse files
committed
This closes qax-os#1139, SetCellDefault support non-numeric value
- Add default value on getting `View` property of sheet views - Add examples and unit test for set sheet views - Re-order field on sheet view options - Fix incorrect build-in number format: 42 - Simplify code for the `stylesReader` function
1 parent 0f1fcb7 commit 3f8f4f5

File tree

4 files changed

+136
-111
lines changed

4 files changed

+136
-111
lines changed

cell.go

+3
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ func (f *File) SetCellDefault(sheet, axis, value string) error {
450450
// setCellDefault prepares cell type and string type cell value by a given
451451
// string.
452452
func setCellDefault(value string) (t string, v string) {
453+
if ok, _ := isNumeric(value); !ok {
454+
t = "str"
455+
}
453456
v = value
454457
return
455458
}

sheetview.go

+76-77
Original file line numberDiff line numberDiff line change
@@ -27,74 +27,49 @@ type SheetViewOptionPtr interface {
2727
}
2828

2929
type (
30-
// DefaultGridColor is a SheetViewOption. It specifies a flag indicating that
31-
// the consuming application should use the default grid lines color (system
32-
// dependent). Overrides any color specified in colorId.
30+
// DefaultGridColor is a SheetViewOption. It specifies a flag indicating
31+
// that the consuming application should use the default grid lines color
32+
// (system dependent). Overrides any color specified in colorId.
3333
DefaultGridColor bool
34-
// RightToLeft is a SheetViewOption. It specifies a flag indicating whether
35-
// the sheet is in 'right to left' display mode. When in this mode, Column A
36-
// is on the far right, Column B ;is one column left of Column A, and so on.
37-
// Also, information in cells is displayed in the Right to Left format.
38-
RightToLeft bool
39-
// ShowFormulas is a SheetViewOption. It specifies a flag indicating whether
40-
// this sheet should display formulas.
34+
// ShowFormulas is a SheetViewOption. It specifies a flag indicating
35+
// whether this sheet should display formulas.
4136
ShowFormulas bool
42-
// ShowGridLines is a SheetViewOption. It specifies a flag indicating whether
43-
// this sheet should display gridlines.
37+
// ShowGridLines is a SheetViewOption. It specifies a flag indicating
38+
// whether this sheet should display gridlines.
4439
ShowGridLines bool
4540
// ShowRowColHeaders is a SheetViewOption. It specifies a flag indicating
4641
// whether the sheet should display row and column headings.
4742
ShowRowColHeaders bool
48-
// ZoomScale is a SheetViewOption. It specifies a window zoom magnification
49-
// for current view representing percent values. This attribute is restricted
50-
// to values ranging from 10 to 400. Horizontal & Vertical scale together.
51-
ZoomScale float64
52-
// TopLeftCell is a SheetViewOption. It specifies a location of the top left
53-
// visible cell Location of the top left visible cell in the bottom right
54-
// pane (when in Left-to-Right mode).
55-
TopLeftCell string
56-
// ShowZeros is a SheetViewOption. It specifies a flag indicating
57-
// whether to "show a zero in cells that have zero value".
58-
// When using a formula to reference another cell which is empty, the referenced value becomes 0
43+
// ShowZeros is a SheetViewOption. It specifies a flag indicating whether
44+
// to "show a zero in cells that have zero value". When using a formula to
45+
// reference another cell which is empty, the referenced value becomes 0
5946
// when the flag is true. (Default setting is true.)
6047
ShowZeros bool
61-
// View is a SheetViewOption. It specifies a flag indicating
62-
// how sheet is displayed, by default it uses empty string
63-
// available options: normal, pageLayout, pageBreakPreview
64-
View string
65-
// ShowRuler is a SheetViewOption. It specifies a flag indicating
66-
// this sheet should display ruler.
48+
// RightToLeft is a SheetViewOption. It specifies a flag indicating whether
49+
// the sheet is in 'right to left' display mode. When in this mode, Column
50+
// A is on the far right, Column B ;is one column left of Column A, and so
51+
// on. Also, information in cells is displayed in the Right to Left format.
52+
RightToLeft bool
53+
// ShowRuler is a SheetViewOption. It specifies a flag indicating this
54+
// sheet should display ruler.
6755
ShowRuler bool
68-
69-
/* TODO
70-
// ShowWhiteSpace is a SheetViewOption. It specifies a flag indicating
71-
// whether page layout view shall display margins. False means do not display
72-
// left, right, top (header), and bottom (footer) margins (even when there is
73-
// data in the header or footer).
74-
ShowWhiteSpace bool
75-
// WindowProtection is a SheetViewOption.
76-
WindowProtection bool
77-
*/
56+
// View is a SheetViewOption. It specifies a flag indicating how sheet is
57+
// displayed, by default it uses empty string available options: normal,
58+
// pageLayout, pageBreakPreview
59+
View string
60+
// TopLeftCell is a SheetViewOption. It specifies a location of the top
61+
// left visible cell Location of the top left visible cell in the bottom
62+
// right pane (when in Left-to-Right mode).
63+
TopLeftCell string
64+
// ZoomScale is a SheetViewOption. It specifies a window zoom magnification
65+
// for current view representing percent values. This attribute is
66+
// restricted to values ranging from 10 to 400. Horizontal & Vertical
67+
// scale together.
68+
ZoomScale float64
7869
)
7970

8071
// Defaults for each option are described in XML schema for CT_SheetView
8172

82-
func (o TopLeftCell) setSheetViewOption(view *xlsxSheetView) {
83-
view.TopLeftCell = string(o)
84-
}
85-
86-
func (o *TopLeftCell) getSheetViewOption(view *xlsxSheetView) {
87-
*o = TopLeftCell(string(view.TopLeftCell))
88-
}
89-
90-
func (o View) setSheetViewOption(view *xlsxSheetView) {
91-
view.View = string(o)
92-
}
93-
94-
func (o *View) getSheetViewOption(view *xlsxSheetView) {
95-
*o = View(string(view.View))
96-
}
97-
9873
func (o DefaultGridColor) setSheetViewOption(view *xlsxSheetView) {
9974
view.DefaultGridColor = boolPtr(bool(o))
10075
}
@@ -103,14 +78,6 @@ func (o *DefaultGridColor) getSheetViewOption(view *xlsxSheetView) {
10378
*o = DefaultGridColor(defaultTrue(view.DefaultGridColor)) // Excel default: true
10479
}
10580

106-
func (o RightToLeft) setSheetViewOption(view *xlsxSheetView) {
107-
view.RightToLeft = bool(o) // Excel default: false
108-
}
109-
110-
func (o *RightToLeft) getSheetViewOption(view *xlsxSheetView) {
111-
*o = RightToLeft(view.RightToLeft)
112-
}
113-
11481
func (o ShowFormulas) setSheetViewOption(view *xlsxSheetView) {
11582
view.ShowFormulas = bool(o) // Excel default: false
11683
}
@@ -127,12 +94,12 @@ func (o *ShowGridLines) getSheetViewOption(view *xlsxSheetView) {
12794
*o = ShowGridLines(defaultTrue(view.ShowGridLines)) // Excel default: true
12895
}
12996

130-
func (o ShowRuler) setSheetViewOption(view *xlsxSheetView) {
131-
view.ShowRuler = boolPtr(bool(o))
97+
func (o ShowRowColHeaders) setSheetViewOption(view *xlsxSheetView) {
98+
view.ShowRowColHeaders = boolPtr(bool(o))
13299
}
133100

134-
func (o *ShowRuler) getSheetViewOption(view *xlsxSheetView) {
135-
*o = ShowRuler(defaultTrue(view.ShowRuler)) // Excel default: true
101+
func (o *ShowRowColHeaders) getSheetViewOption(view *xlsxSheetView) {
102+
*o = ShowRowColHeaders(defaultTrue(view.ShowRowColHeaders)) // Excel default: true
136103
}
137104

138105
func (o ShowZeros) setSheetViewOption(view *xlsxSheetView) {
@@ -143,12 +110,40 @@ func (o *ShowZeros) getSheetViewOption(view *xlsxSheetView) {
143110
*o = ShowZeros(defaultTrue(view.ShowZeros)) // Excel default: true
144111
}
145112

146-
func (o ShowRowColHeaders) setSheetViewOption(view *xlsxSheetView) {
147-
view.ShowRowColHeaders = boolPtr(bool(o))
113+
func (o RightToLeft) setSheetViewOption(view *xlsxSheetView) {
114+
view.RightToLeft = bool(o) // Excel default: false
148115
}
149116

150-
func (o *ShowRowColHeaders) getSheetViewOption(view *xlsxSheetView) {
151-
*o = ShowRowColHeaders(defaultTrue(view.ShowRowColHeaders)) // Excel default: true
117+
func (o *RightToLeft) getSheetViewOption(view *xlsxSheetView) {
118+
*o = RightToLeft(view.RightToLeft)
119+
}
120+
121+
func (o ShowRuler) setSheetViewOption(view *xlsxSheetView) {
122+
view.ShowRuler = boolPtr(bool(o))
123+
}
124+
125+
func (o *ShowRuler) getSheetViewOption(view *xlsxSheetView) {
126+
*o = ShowRuler(defaultTrue(view.ShowRuler)) // Excel default: true
127+
}
128+
129+
func (o View) setSheetViewOption(view *xlsxSheetView) {
130+
view.View = string(o)
131+
}
132+
133+
func (o *View) getSheetViewOption(view *xlsxSheetView) {
134+
if view.View != "" {
135+
*o = View(view.View)
136+
return
137+
}
138+
*o = View("normal")
139+
}
140+
141+
func (o TopLeftCell) setSheetViewOption(view *xlsxSheetView) {
142+
view.TopLeftCell = string(o)
143+
}
144+
145+
func (o *TopLeftCell) getSheetViewOption(view *xlsxSheetView) {
146+
*o = TopLeftCell(string(view.TopLeftCell))
152147
}
153148

154149
func (o ZoomScale) setSheetViewOption(view *xlsxSheetView) {
@@ -186,13 +181,15 @@ func (f *File) getSheetView(sheet string, viewIndex int) (*xlsxSheetView, error)
186181
// Available options:
187182
//
188183
// DefaultGridColor(bool)
189-
// RightToLeft(bool)
190184
// ShowFormulas(bool)
191185
// ShowGridLines(bool)
192186
// ShowRowColHeaders(bool)
193-
// ZoomScale(float64)
194-
// TopLeftCell(string)
195187
// ShowZeros(bool)
188+
// RightToLeft(bool)
189+
// ShowRuler(bool)
190+
// View(string)
191+
// TopLeftCell(string)
192+
// ZoomScale(float64)
196193
//
197194
// Example:
198195
//
@@ -216,13 +213,15 @@ func (f *File) SetSheetViewOptions(name string, viewIndex int, opts ...SheetView
216213
// Available options:
217214
//
218215
// DefaultGridColor(bool)
219-
// RightToLeft(bool)
220216
// ShowFormulas(bool)
221217
// ShowGridLines(bool)
222218
// ShowRowColHeaders(bool)
223-
// ZoomScale(float64)
224-
// TopLeftCell(string)
225219
// ShowZeros(bool)
220+
// RightToLeft(bool)
221+
// ShowRuler(bool)
222+
// View(string)
223+
// TopLeftCell(string)
224+
// ZoomScale(float64)
226225
//
227226
// Example:
228227
//

0 commit comments

Comments
 (0)