Skip to content

Commit e4497c4

Browse files
rememberherwujierui
and
wujierui
authored
ref #65, new formula function: DBCS (#1791)
Co-authored-by: wujierui <wujierui@jimabrand.com>
1 parent 9b07898 commit e4497c4

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

calc.go

+33
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ type formulaFuncs struct {
455455
// DAYS
456456
// DAYS360
457457
// DB
458+
// DBCS
458459
// DCOUNT
459460
// DCOUNTA
460461
// DDB
@@ -13566,6 +13567,38 @@ func (fn *formulaFuncs) concat(name string, argsList *list.List) formulaArg {
1356613567
return newStringFormulaArg(buf.String())
1356713568
}
1356813569

13570+
// DBCS converts half-width (single-byte) letters within a character string to
13571+
// full-width (double-byte) characters. The syntax of the function is:
13572+
//
13573+
// DBCS(text)
13574+
func (fn *formulaFuncs) DBCS(argsList *list.List) formulaArg {
13575+
if argsList.Len() != 1 {
13576+
return newErrorFormulaArg(formulaErrorVALUE, "DBCS requires 1 argument")
13577+
}
13578+
arg := argsList.Front().Value.(formulaArg)
13579+
if arg.Type == ArgError {
13580+
return arg
13581+
}
13582+
if fn.f.options.CultureInfo == CultureNameZhCN {
13583+
var chars []string
13584+
for _, r := range arg.Value() {
13585+
code := r
13586+
if code == 32 {
13587+
code = 12288
13588+
} else {
13589+
code += 65248
13590+
}
13591+
if (code < 32 || code > 126) && r != 165 && code < 65381 {
13592+
chars = append(chars, string(code))
13593+
} else {
13594+
chars = append(chars, string(r))
13595+
}
13596+
}
13597+
return newStringFormulaArg(strings.Join(chars, ""))
13598+
}
13599+
return arg
13600+
}
13601+
1356913602
// EXACT function tests if two supplied text strings or values are exactly
1357013603
// equal and if so, returns TRUE; Otherwise, the function returns FALSE. The
1357113604
// function is case-sensitive. The syntax of the function is:

calc_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,10 @@ func TestCalcCellValue(t *testing.T) {
17231723
"=CONCATENATE(TRUE(),1,FALSE(),\"0\",INT(2))": "TRUE1FALSE02",
17241724
"=CONCATENATE(MUNIT(2))": "1001",
17251725
"=CONCATENATE(A1:B2)": "1425",
1726+
// DBCS
1727+
"=DBCS(\"\")": "",
1728+
"=DBCS(123.456)": "123.456",
1729+
"=DBCS(\"123.456\")": "123.456",
17261730
// EXACT
17271731
"=EXACT(1,\"1\")": "TRUE",
17281732
"=EXACT(1,1)": "TRUE",
@@ -3836,6 +3840,9 @@ func TestCalcCellValue(t *testing.T) {
38363840
// CONCATENATE
38373841
"=CONCATENATE(NA())": {"#N/A", "#N/A"},
38383842
"=CONCATENATE(1,1/0)": {"#DIV/0!", "#DIV/0!"},
3843+
// DBCS
3844+
"=DBCS(NA())": {"#N/A", "#N/A"},
3845+
"=DBCS()": {"#VALUE!", "DBCS requires 1 argument"},
38393846
// EXACT
38403847
"=EXACT()": {"#VALUE!", "EXACT requires 2 arguments"},
38413848
"=EXACT(1,2,3)": {"#VALUE!", "EXACT requires 2 arguments"},
@@ -5194,6 +5201,14 @@ func TestCalcDatabase(t *testing.T) {
51945201
}
51955202
}
51965203

5204+
func TestCalcDBCS(t *testing.T) {
5205+
f := NewFile(Options{CultureInfo: CultureNameZhCN})
5206+
assert.NoError(t, f.SetCellFormula("Sheet1", "A1", "=DBCS(\"`~·!@#$¥%…^&*()_-+=[]{}\\|;:'\"\"<,>.?/01234567890 abc ABC \uff65\uff9e\uff9f \uff74\uff78\uff7e\uff99\")"))
5207+
result, err := f.CalcCellValue("Sheet1", "A1")
5208+
assert.NoError(t, err)
5209+
assert.Equal(t, "\uff40\uff5e\u00b7\uff01\uff20\uff03\uff04\u00a5\uff05\u2026\uff3e\uff06\uff0a\uff08\uff09\uff3f\uff0d\uff0b\uff1d\uff3b\uff3d\uff5b\uff5d\uff3c\uff5c\uff1b\uff1a\uff07\uff02\uff1c\uff0c\uff1e\uff0e\uff1f\uff0f\uff10\uff11\uff12\uff13\uff14\uff15\uff16\uff17\uff18\uff19\uff10\u3000\uff41\uff42\uff43\u3000\uff21\uff22\uff23\u3000\uff65\uff9e\uff9f\u3000\uff74\uff78\uff7e\uff99", result)
5210+
}
5211+
51975212
func TestCalcFORMULATEXT(t *testing.T) {
51985213
f, formulaText := NewFile(), "=SUM(B1:C1)"
51995214
assert.NoError(t, f.SetCellFormula("Sheet1", "A1", formulaText))

0 commit comments

Comments
 (0)