forked from duanlizhi/gooxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fntext.go
370 lines (340 loc) · 10.3 KB
/
fntext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package formula
import (
"bytes"
"fmt"
"strings"
"unicode"
)
func init() {
// RegisterFunction("ASC") Need to figure out how to test
// RegisterFunction("BAHTTEXT")
RegisterFunction("CHAR", Char)
RegisterFunction("CLEAN", Clean)
RegisterFunction("CODE", Code)
RegisterFunction("CONCATENATE", Concatenate)
// RegisterFunction("CONCAT", ) Need to test with Excel
// RegisterFunction("DBCS")
// RegisterFunction("DOLLAR") Need to test with Excel
RegisterFunction("EXACT", Exact)
// RegisterFunction("FIND")
// RegisterFunction("FINDB")
RegisterFunction("LEFT", Left)
RegisterFunction("LEFTB", Left) // for now
RegisterFunction("LEN", Len)
RegisterFunction("LENB", Len) // for now
RegisterFunction("LOWER", Lower)
// RegisterFunction("MID")
// RegisterFunction("MIDB")
// RegisterFunction("NUMBERVALUE")
// RegisterFunction("PHONETIC")
RegisterFunction("PROPER", Proper)
// RegisterFunction("REPLACE")
// RegisterFunction("REPLACEB")
RegisterFunction("REPT", Rept)
RegisterFunction("RIGHT", Right)
RegisterFunction("RIGHTB", Right) // for now
//RegisterFunction("SEARCH", )
//RegisterFunction("SEARCHB", )
//RegisterFunction("SUBSTITUTE", )
RegisterFunction("T", T)
//RegisterFunction("TEXT")
//RegisterFunction("TEXTJOIN")
RegisterFunction("TRIM", Trim)
RegisterFunction("_xlfn.UNICHAR", Char) // for now
RegisterFunction("_xlfn.UNICODE", Unicode)
RegisterFunction("UPPER", Upper)
//RegisterFunction("VALUE", )
}
// Char is an implementation of the Excel CHAR function that takes an integer in
// the range [0,255] and returns the corresponding ASCII character.
func Char(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("CHAR requires a single numeric argument")
}
c := args[0].AsNumber()
if c.Type != ResultTypeNumber {
return MakeErrorResult("CHAR requires a single numeric argument")
}
cv := int(c.ValueNumber)
if cv < 0 || cv > 255 {
return MakeErrorResult("CHAR requires arguments in the range [0,255]")
}
return MakeStringResult(fmt.Sprintf("%c", cv))
}
// Clean is an implementation of the Excel CLEAN function that removes
// unprintable characters.
func Clean(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("CLEAN requires a single string argument")
}
s := args[0].AsString()
if s.Type != ResultTypeString {
return MakeErrorResult("CHAR requires a single string argument")
}
b := bytes.Buffer{}
for _, c := range s.ValueString {
if unicode.IsPrint(c) {
b.WriteRune(c)
}
}
return MakeStringResult(b.String())
}
// Code is an implementation of the Excel CODE function that returns the first
// character of the string as a number.
func Code(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("CODE requires a single string argument")
}
s := args[0].AsString()
if s.Type != ResultTypeString {
return MakeErrorResult("CODE requires a single string argument")
}
// Zero length string returns a zero
if len(s.ValueString) == 0 {
return MakeNumberResult(0)
}
return MakeNumberResult(float64(s.ValueString[0]))
}
func Unicode(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("UNICODE requires a single string argument")
}
s := args[0].AsString()
if s.Type != ResultTypeString {
return MakeErrorResult("UNICODE requires a single string argument")
}
// Zero length string returns an error
if len(s.ValueString) == 0 {
return MakeErrorResult("UNICODE requires a non-zero length argument")
}
return MakeNumberResult(float64(s.ValueString[0]))
}
// Concatenate is an implementation of the Excel CONCATENATE() function.
func Concatenate(args []Result) Result {
buf := bytes.Buffer{}
for _, a := range args {
a = a.AsString()
switch a.Type {
case ResultTypeString:
buf.WriteString(a.ValueString)
default:
return MakeErrorResult("CONCATENATE() requires arguments to be strings")
}
}
return MakeStringResult(buf.String())
}
// Exact is an implementation of the Excel EXACT() which compares two strings.
func Exact(args []Result) Result {
if len(args) != 2 {
return MakeErrorResult("CONCATENATE() requires two string arguments")
}
arg1 := args[0].AsString()
arg2 := args[1].AsString()
if arg1.Type != ResultTypeString || arg2.Type != ResultTypeString {
return MakeErrorResult("CONCATENATE() requires two string arguments")
}
return MakeBoolResult(arg1.ValueString == arg2.ValueString)
}
// Left implements the Excel LEFT(string,[n]) function which returns the
// leftmost n characters.
func Left(args []Result) Result {
n := 1
switch len(args) {
case 1:
// no length argument returns the single left-most character
case 2:
// second argument must be a number
if args[1].Type != ResultTypeNumber {
return MakeErrorResult("LEFT expected number argument")
}
// Excel truncates floating points
n = int(args[1].ValueNumber)
if n < 0 {
return MakeErrorResult("LEFT expected number argument >= 0")
}
if n == 0 { // empty string
return MakeStringResult("")
}
default:
return MakeErrorResult("LEFT expected one or two arguments")
}
// can't call LEFT on a range
if args[0].Type == ResultTypeList {
return MakeErrorResult("LEFT can't be called on a range")
}
v := args[0].Value()
if n > len(v) {
return MakeStringResult(v)
}
return MakeStringResult(v[0:n])
}
// Len is an implementation of the Excel LEN function that returns length of a string
func Len(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("LEN requires a single string argument")
}
s := args[0].AsString()
if s.Type != ResultTypeString {
return MakeErrorResult("LEN requires a single string argument")
}
return MakeNumberResult(float64(len(s.ValueString)))
}
// Lower is an implementation of the Excel LOWER function that returns a lower
// case version of a string.
func Lower(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("LOWER requires a single string argument")
}
s := args[0].AsString()
if s.Type != ResultTypeString {
return MakeErrorResult("LOWER requires a single string argument")
}
return MakeStringResult(strings.ToLower(s.ValueString))
}
// Proper is an implementation of the Excel PROPER function that returns a copy
// of the string with each word capitalized.
func Proper(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("PROPER requires a single string argument")
}
s := args[0].AsString()
if s.Type != ResultTypeString {
return MakeErrorResult("PROPER requires a single string argument")
}
buf := bytes.Buffer{}
prevWasLetter := false
for _, c := range s.ValueString {
if !prevWasLetter && unicode.IsLetter(c) {
buf.WriteRune(unicode.ToUpper(c))
} else {
// seems odd but matches Excel's behavior
buf.WriteRune(unicode.ToLower(c))
}
prevWasLetter = unicode.IsLetter(c)
}
return MakeStringResult(buf.String())
}
// Rept is an implementation of the Excel REPT function that returns n copies of
// a string.
func Rept(args []Result) Result {
if len(args) != 2 {
return MakeErrorResult("REPT requires two arguments")
}
s := args[0].AsString()
if s.Type != ResultTypeString {
return MakeErrorResult("PROPER requires first argument to be a string")
}
n := args[1].AsNumber()
if n.Type != ResultTypeNumber {
return MakeErrorResult("PROPER requires second argument to be a number")
}
if n.ValueNumber < 0 {
return MakeErrorResult("PROPER requires second argument to be >= 0")
}
if n.ValueNumber == 0 {
return MakeStringResult("")
}
buf := bytes.Buffer{}
for i := 0; i < int(n.ValueNumber); i++ {
buf.WriteString(s.ValueString)
}
return MakeStringResult(buf.String())
}
// Right implements the Excel RIGHT(string,[n]) function which returns the
// rightmost n characters.
func Right(args []Result) Result {
n := 1
switch len(args) {
case 1:
// no length argument returns the single right-most character
case 2:
// second argument must be a number
if args[1].Type != ResultTypeNumber {
return MakeErrorResult("RIGHT expected number argument")
}
// Excel truncates floating points
n = int(args[1].ValueNumber)
if n < 0 {
return MakeErrorResult("RIGHT expected number argument >= 0")
}
if n == 0 { // empty string
return MakeStringResult("")
}
default:
return MakeErrorResult("RIGHT accepts one or two arguments")
}
// can't call RIGHT on a range
if args[0].Type == ResultTypeList {
return MakeErrorResult("RIGHT can't be called on a range")
}
v := args[0].Value()
m := len(v)
if n > m {
return MakeStringResult(v)
}
return MakeStringResult(v[m-n : m])
}
// T is an implementation of the Excel T function that returns whether the
// argument is text.
func T(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("T requires a single string argument")
}
s := args[0]
if s.Type == ResultTypeError || s.Type == ResultTypeString {
return s
}
return MakeEmptyResult()
}
// Trim is an implementation of the Excel TRIM function that removes leading,
// trailing and consecutive spaces.
func Trim(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("TRIM requires a single string argument")
}
s := args[0].AsString()
if s.Type != ResultTypeString {
return MakeErrorResult("TRIM requires a single string argument")
}
buf := bytes.Buffer{}
seenLetter := false
prevWasSpace := false
trailingSpaces := 0
for _, c := range s.ValueString {
isSpace := c == ' '
if isSpace {
if !seenLetter {
continue
}
if !prevWasSpace {
trailingSpaces++
buf.WriteRune(c)
}
} else {
trailingSpaces = 0
seenLetter = true
buf.WriteRune(c)
}
prevWasSpace = isSpace
}
buf.Truncate(buf.Len() - trailingSpaces)
return MakeStringResult(buf.String())
}
// Upper is an implementation of the Excel UPPER function that returns a upper
// case version of a string.
func Upper(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("UPPER requires a single string argument")
}
s := args[0].AsString()
if s.Type != ResultTypeString {
return MakeErrorResult("UPPER requires a single string argument")
}
return MakeStringResult(strings.ToUpper(s.ValueString))
}