Skip to content

Commit

Permalink
This support applies partial built-in language number format code
Browse files Browse the repository at this point in the history
- Remove the `Lang` field in the `Style` data type
- Add new `Options` fields `ShortDatePattern`, `LongDatePattern` and `LongTimePattern`
- Add new `Options` field `CultureInfo` and new exported data type `CultureName`
- Upgrade the Excelize to the master branch code
- Update the unit test
  • Loading branch information
xuri committed May 24, 2023
1 parent 958999d commit ea176a4
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 18 deletions.
3 changes: 2 additions & 1 deletion cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/stretchr/testify v1.8.1
github.com/xuri/excelize/v2 v2.7.1
github.com/xuri/excelize/v2 v2.7.2-0.20230522161855-76cd0992b038
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/image v0.5.0
)
20 changes: 12 additions & 8 deletions cmd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj09jdMlkY0aiA6+Skbtl3/c=
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.7.1 h1:gm8q0UCAyaTt3MEF5wWMjVdmthm2EHAWesGSKS9tdVI=
github.com/xuri/excelize/v2 v2.7.1/go.mod h1:qc0+2j4TvAUrBw36ATtcTeC1VCM0fFdAXZOmcF4nTpY=
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 h1:OAmKAfT06//esDdpi/DZ8Qsdt4+M5+ltca05dA5bG2M=
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
github.com/xuri/efp v0.0.0-20230422071738-01f4e37c47e9 h1:ge5g8vsTQclA5lXDi+PuiAFw5GMIlMHOB/5e1hsf96E=
github.com/xuri/efp v0.0.0-20230422071738-01f4e37c47e9/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.7.2-0.20230522161855-76cd0992b038 h1:u3RqvaH87/5qqGjMP5au7XPcxh2yEtRWm7RbMncM0is=
github.com/xuri/excelize/v2 v2.7.2-0.20230522161855-76cd0992b038/go.mod h1:ejxK/GVQckqRTIU0qzshtlPKdiWdxLtkjT6/x842Lzg=
github.com/xuri/nfp v0.0.0-20230503010013-3f38cdbb0b83 h1:xVwnvkzzi+OiwhIkWOXvh1skFI6bagk8OvGuazM80Rw=
github.com/xuri/nfp v0.0.0-20230503010013-3f38cdbb0b83/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI=
golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand All @@ -36,8 +37,9 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -48,10 +50,12 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down
19 changes: 18 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ func regFuncs() {

// regConstants register all exported JavaScript functions on the Window ot Global.
func regConstants() {
for name, constant := range map[string]int{
"CultureNameUnknown": int(excelize.CultureNameUnknown),
"CultureNameEnUS": int(excelize.CultureNameEnUS),
"CultureNameZhCN": int(excelize.CultureNameZhCN),
} {
js.Global().Get("excelize").Set(name, constant)
}
for name, constant := range map[string]int{
"Area": int(excelize.Area),
"AreaStacked": int(excelize.AreaStacked),
Expand Down Expand Up @@ -847,10 +854,20 @@ func ThemeColor(this js.Value, args []js.Value) interface{} {
func NewFile(this js.Value, args []js.Value) interface{} {
fn := map[string]interface{}{"error": nil}
fn["error"] = nil
if err := prepareArgs(args, []argsRule{}); err != nil {
if err := prepareArgs(args, []argsRule{
{types: []js.Type{js.TypeObject}, opts: true},
}); err != nil {
fn["error"] = err.Error()
return js.ValueOf(fn)
}
if len(args) == 1 {
goVal, err := jsValueToGo(args[0], reflect.TypeOf(excelize.Options{}))
if err != nil {
fn["error"] = err.Error()
return js.ValueOf(fn)
}
return regInteropFunc(excelize.NewFile(goVal.Elem().Interface().(excelize.Options)), fn)
}
return regInteropFunc(excelize.NewFile(), fn)
}

Expand Down
18 changes: 13 additions & 5 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,20 @@ func TestNewFile(t *testing.T) {
f := NewFile(js.Value{}, []js.Value{})
assert.True(t, f.(js.Value).Get("error").IsNull())

f = NewFile(js.Value{}, []js.Value{js.ValueOf(true)})
f = NewFile(js.Value{}, []js.Value{js.ValueOf(map[string]interface{}{
"ShortDatePattern": "yyyy/m/d",
})})
assert.True(t, f.(js.Value).Get("error").IsNull())

f = NewFile(js.Value{}, []js.Value{js.ValueOf(map[string]interface{}{
"ShortDatePattern": "yyyy/m/d",
}), js.ValueOf(true)})
assert.EqualError(t, errArgNum, f.(js.Value).Get("error").String())

f = NewFile(js.Value{}, []js.Value{js.ValueOf(map[string]interface{}{
"ShortDatePattern": true,
})})
assert.EqualError(t, errArgType, f.(js.Value).Get("error").String())
}

func TestOpenReader(t *testing.T) {
Expand Down Expand Up @@ -1296,7 +1308,6 @@ func TestNewStyle(t *testing.T) {
"NumFmt": 1,
"DecimalPlaces": 2,
"CustomNumFmt": "0.00",
"Lang": "language",
"NegRed": true,
"Border": []interface{}{
map[string]interface{}{"Type": "left", "Color": "000000", "Style": 1},
Expand Down Expand Up @@ -1339,7 +1350,6 @@ func TestNewStyle(t *testing.T) {
{"NumFmt": "1"},
{"DecimalPlaces": "2"},
{"CustomNumFmt": true},
{"Lang": true},
{"NegRed": "true"},
{"Border": true},
{"Border": []interface{}{map[string]interface{}{"Type": true}}},
Expand Down Expand Up @@ -2444,15 +2454,13 @@ func TestGoValueToJS(t *testing.T) {
result, err = goValueToJS(reflect.ValueOf(excelize.Style{
NumFmt: 1,
CustomNumFmt: &exp,
Lang: "en",
Alignment: &excelize.Alignment{Indent: 1},
Border: []excelize.Border{{Type: "left"}, {Type: "top"}},
}), reflect.TypeOf(excelize.Style{}))
assert.NoError(t, err)
assert.Equal(t, 1, js.ValueOf(result).Get("NumFmt").Int())
assert.Equal(t, exp, js.ValueOf(result).Get("CustomNumFmt").String())
assert.Equal(t, 1, js.ValueOf(result).Get("Alignment").Get("Indent").Int())
assert.Equal(t, "en", js.ValueOf(result).Get("Lang").String())
assert.Equal(t, "left", js.ValueOf(result).Get("Border").Index(0).Get("Type").String())
assert.Equal(t, "top", js.ValueOf(result).Get("Border").Index(1).Get("Type").String())

Expand Down
56 changes: 53 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,61 @@ declare module 'excelize-wasm' {
};

/**
* Options define the options for open and reading spreadsheet.
* This section defines the currently supported country code types
* enumeration for apply number format.
*/
export enum CultureName {
CultureNameUnknown,
CultureNameEnUS,
CultureNameZhCN,
}

/**
* Options define the options for o`pen and reading spreadsheet.
*
* MaxCalcIterations specifies the maximum iterations for iterative
* calculation, the default value is 0.
*
* Password specifies the password of the spreadsheet in plain text.
*
* RawCellValue specifies if apply the number format for the cell value or get
* the raw value.
*
* UnzipSizeLimit specifies the unzip size limit in bytes on open the
* spreadsheet, this value should be greater than or equal to
* UnzipXMLSizeLimit, the default size limit is 16GB.
*
* UnzipXMLSizeLimit specifies the memory limit on unzipping worksheet and
* shared string table in bytes, worksheet XML will be extracted to system
* temporary directory when the file size is over this value, this value
* should be less than or equal to UnzipSizeLimit, the default value is
* 16MB.
*
* ShortDatePattern specifies the short date number format code. In the
* spreadsheet applications, date formats display date and time serial numbers
* as date values. Date formats that begin with an asterisk (*) respond to
* changes in regional date and time settings that are specified for the
* operating system. Formats without an asterisk are not affected by operating
* system settings. The ShortDatePattern used for specifies apply date formats
* that begin with an asterisk.
*
* LongDatePattern specifies the long date number format code.
*
* LongTimePattern specifies the long time number format code.
*
* CultureInfo specifies the country code for applying built-in language number
* format code these effect by the system's local language settings.
*/
export type Options = {
MaxCalcIterations?: number;
Password?: string;
RawCellValue?: boolean;
UnzipSizeLimit?: number;
UnzipXMLSizeLimit?: number;
ShortDatePattern?: string;
LongDatePattern?: string;
LongTimePattern?: string;
CultureInfo?: CultureName;
};

/**
Expand Down Expand Up @@ -106,7 +153,6 @@ declare module 'excelize-wasm' {
NumFmt?: number;
DecimalPlaces?: number;
CustomNumFmt?: string;
Lang?: string;
NegRed?: boolean;
};

Expand Down Expand Up @@ -849,8 +895,9 @@ declare module 'excelize-wasm' {

/**
* NewFile provides a function to create new file by default template.
* @param opts The options for open and reading spreadsheet
*/
export function NewFile(): NewFile;
export function NewFile(opts?: Options): NewFile;

/**
* OpenReader read data stream from buffer and return a populated spreadsheet
Expand Down Expand Up @@ -2505,6 +2552,9 @@ declare module 'excelize-wasm' {
ThemeColor: typeof ThemeColor,
NewFile: typeof NewFile;
OpenReader: typeof OpenReader;
CultureNameUnknown: typeof CultureName.CultureNameUnknown;
CultureNameEnUS: typeof CultureName.CultureNameEnUS;
CultureNameZhCN: typeof CultureName.CultureNameZhCN;
Area: typeof ChartType.Area;
AreaStacked: typeof ChartType.AreaStacked;
AreaPercentStacked: typeof ChartType.AreaPercentStacked;
Expand Down

0 comments on commit ea176a4

Please sign in to comment.