Skip to content

Commit 2fc993a

Browse files
Laeeth IsharcLaeeth Isharc
Laeeth Isharc
authored and
Laeeth Isharc
committed
wrapping working
1 parent 07c948d commit 2fc993a

File tree

8 files changed

+154
-78
lines changed

8 files changed

+154
-78
lines changed

README

+61
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,64 @@ type into cell A10 =funcsum(A1:A3). you should see the sum of the numbers
1717

1818
more coming soon.
1919

20+
see generic.d for a set of sample workbook functions.
21+
workbook functions and dialogue boxes seem to work. toolbars crash (so don't register them for now).
22+
23+
to write a function in D, you will need to add it to the registration table.
24+
see Excel reference for the function string and format
25+
26+
ultimate aim is to achive pyxll style wrapping of D functions using UDAs.
27+
28+
in the meantime it's possible to convert excel worksheet matrices to D
29+
and D double[][] back to excel using the functions
30+
31+
convert from excel XLOPER* to double[] or double[][]
32+
======================================================
33+
double[] fromXLOPER12!(double[])(LPXLOPER12 arg);
34+
double[][] fromXLOPER12!(double[][])(LPXLOPER12 arg);
35+
36+
convert from double[] or double[][] to XLOPER*
37+
=============================================
38+
LPXLOPER12 makeXLOPER12(double[] arg);
39+
LPXLOPER12 makeXLOPER12(double[][] arg);
40+
41+
memory is allocated using std.experimental.allocator as ubyte, and freed
42+
via xlAutoFree. so in theory the user does not need to concern himself with
43+
this. Not sure that I free everything when I should in fromXLOPER12.
44+
45+
D exceptions will cause problems - D code should have a try{} catch(Throwable t){} around all code and return:
46+
47+
LPXLOPER12 makeXLOPER12Error(int errorCode,string errorMessage);
48+
then the message can be retrieved from excel by:
49+
=lastErrorMessage()
50+
51+
52+
53+
for example:
54+
extern(Windows) LPXLOPER12 WrapSquare3(
55+
LPXLOPER12 px1,LPXLOPER12 px2,LPXLOPER12 px3,LPXLOPER12 px4,
56+
LPXLOPER12 px5,LPXLOPER12 px6,LPXLOPER12 px7,LPXLOPER12 px8,
57+
LPXLOPER12 px9,LPXLOPER12 px10,LPXLOPER12 px11,LPXLOPER12 px12,
58+
LPXLOPER12 px13,LPXLOPER12 px14,LPXLOPER12 px15,LPXLOPER12 px16,
59+
LPXLOPER12 px17,LPXLOPER12 px18,LPXLOPER12 px19,LPXLOPER12 px20,
60+
LPXLOPER12 px21,LPXLOPER12 px22,LPXLOPER12 px23,LPXLOPER12 px24,
61+
LPXLOPER12 px25,LPXLOPER12 px26,LPXLOPER12 px27,LPXLOPER12 px28,
62+
LPXLOPER12 px29)
63+
{
64+
import std.algorithm:map,sum;
65+
auto args=px1.fromXLOPER12!(double[]);
66+
if (args.length==0)
67+
return makeXLOPER12Error(100,"you must pass at least one argument");
68+
double[][] retD;
69+
retD.length=args.length;
70+
foreach(i;0..args.length)
71+
{
72+
retD[i].length=args.length;
73+
foreach(j;0..args.length)
74+
{
75+
retD[i][j]=args[0..j+1].map!(arg=>arg*arg).sum;
76+
}
77+
}
78+
return makeXLOPER12(retD);
79+
}
80+

generic.d

+61-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ import framework;
8181
import core.stdc.wchar_ : wcslen;
8282
import core.stdc.wctype:towlower;
8383
import std.format;
84-
//import generic;
84+
import xlld.wrap;
85+
8586
enum GWLP_WNDPROC=-4;
8687
enum MAXWORD = 0xFFFF;
8788
debug=0;
@@ -152,7 +153,7 @@ wchar[20] g_szBuffer = ""w;
152153
g_rgWorksheetFuncsRows define the number of rows in the table. The
153154
g_rgWorksheetFuncsCols represents the number of columns in the table.
154155
*/
155-
enum g_rgWorksheetFuncsRows =3;
156+
enum g_rgWorksheetFuncsRows =5;
156157
enum g_rgWorksheetFuncsCols =10;
157158

158159
__gshared wstring[g_rgWorksheetFuncsCols][g_rgWorksheetFuncsRows] g_rgWorksheetFuncs =
@@ -180,6 +181,30 @@ __gshared wstring[g_rgWorksheetFuncsCols][g_rgWorksheetFuncsRows] g_rgWorksheetF
180181
"Adds the arguments"w,
181182
"Number1,number2,... are 1 to 29 arguments for which you want to sum."w
182183
],
184+
[ "lastErrorMessage"w,
185+
"Q"w, // up to 255 args in Excel 2007 and later,
186+
// upto 29 args in Excel 2003 and earlier versions
187+
"lastErrorMessage"w,
188+
""w,
189+
"1"w,
190+
"Generic Add-In"w,
191+
""w,
192+
""w,
193+
"Return last D error message"w,
194+
""w,
195+
],
196+
[ "WrapSquare3"w,
197+
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"w, // up to 255 args in Excel 2007 and later,
198+
// upto 29 args in Excel 2003 and earlier versions
199+
"WrapSquare3"w,
200+
"number1,number2,..."w,
201+
"1"w,
202+
"Generic Add-In"w,
203+
""w,
204+
""w,
205+
"Sum of squares of the arguments"w,
206+
"Number1,number2,... are 1 to 29 arguments for which you want to sum."w
207+
],
183208
[ "FuncFib"w,
184209
"UU"w,
185210
"FuncFib"w,
@@ -448,7 +473,8 @@ extern(Windows) BOOL /*APIENTRY*/ DllMain( HANDLE hDLL, DWORD dwReason, LPVOID l
448473
extern(Windows) int /*WINAPI*/ xlAutoOpen()
449474
{
450475
import std.conv;
451-
476+
import core.runtime:rt_init;
477+
rt_init();
452478
static XLOPER12 xDLL, // name of this DLL //
453479
xMenu, // xltypeMulti containing the menu //
454480
xTool, // xltypeMulti containing the toolbar //
@@ -674,6 +700,9 @@ extern(Windows) int /*WINAPI*/ xlAutoClose()
674700
Excel12f(xlFree, cast(XLOPER12*)0, [cast(LPXLOPER12) &xRes]);
675701
}
676702

703+
import core.runtime:rt_term;
704+
rt_term();
705+
677706
return 1;
678707
}
679708

@@ -1284,6 +1313,35 @@ extern(Windows) LPXLOPER12 /*WINAPI*/ Func1 (LPXLOPER12 x)
12841313
History: Date Author Reason
12851314
*/
12861315

1316+
extern(Windows) LPXLOPER12 WrapSquare3(
1317+
LPXLOPER12 px1,LPXLOPER12 px2,LPXLOPER12 px3,LPXLOPER12 px4,
1318+
LPXLOPER12 px5,LPXLOPER12 px6,LPXLOPER12 px7,LPXLOPER12 px8,
1319+
LPXLOPER12 px9,LPXLOPER12 px10,LPXLOPER12 px11,LPXLOPER12 px12,
1320+
LPXLOPER12 px13,LPXLOPER12 px14,LPXLOPER12 px15,LPXLOPER12 px16,
1321+
LPXLOPER12 px17,LPXLOPER12 px18,LPXLOPER12 px19,LPXLOPER12 px20,
1322+
LPXLOPER12 px21,LPXLOPER12 px22,LPXLOPER12 px23,LPXLOPER12 px24,
1323+
LPXLOPER12 px25,LPXLOPER12 px26,LPXLOPER12 px27,LPXLOPER12 px28,
1324+
LPXLOPER12 px29)
1325+
{
1326+
import std.algorithm:map,sum;
1327+
import std.experimental.allocator;
1328+
import std.conv:to;
1329+
auto args=px1.fromXLOPER12!(double[]);
1330+
double[][] retD;
1331+
if (args.length==0)
1332+
return makeXLOPER12Error(100,"you must pass at least one argument");
1333+
retD.length=args.length;
1334+
foreach(i;0..args.length)
1335+
{
1336+
retD[i].length=args.length;
1337+
foreach(j;0..args.length)
1338+
{
1339+
retD[i][j]=args[0..j+1].map!(arg=>arg*arg).sum;
1340+
}
1341+
}
1342+
return makeXLOPER12(retD);
1343+
}
1344+
12871345
extern(Windows) LPXLOPER12 /*WINAPI*/ FuncSum(
12881346
LPXLOPER12 px1,LPXLOPER12 px2,LPXLOPER12 px3,LPXLOPER12 px4,
12891347
LPXLOPER12 px5,LPXLOPER12 px6,LPXLOPER12 px7,LPXLOPER12 px8,

generic32.def

+20-17
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@ DESCRIPTION 'Simple D add-in to Excel'
33
EXETYPE NT
44
CODE PRELOAD DISCARDABLE
55
DATA PRELOAD MULTIPLE
6+
LIBRARY "generic64e.xll"
67
EXPORTS
7-
xlAutoOpen @2
8-
xlAutoClose @3
9-
xlAutoRegister12 @4
10-
xlAutoAdd @5
11-
xlAutoRemove @6
8+
xlAutoOpen @2
9+
xlAutoClose @3
10+
xlAutoRegister12 @4
11+
xlAutoAdd @5
12+
xlAutoRemove @6
1213
xlAddInManagerInfo12 @7
13-
DIALOGMsgProc @8
14-
ExcelCursorProc @9
15-
HookExcelWindow @10
16-
UnhookExcelWindow @11
17-
fShowDialog @12
18-
GetHwnd @13
19-
Func1 @14
20-
FuncSum @15
21-
fDance @16
22-
fDialog @17
23-
fExit @18
24-
FuncFib @19
14+
DIALOGMsgProc @8
15+
ExcelCursorProc @9
16+
HookExcelWindow @10
17+
UnhookExcelWindow @11
18+
fShowDialog @12
19+
GetHwnd @13
20+
Func1 @14
21+
FuncSum @15
22+
fDance @16
23+
fDialog @17
24+
fExit @18
25+
FuncFib @19
26+
WrapSquare3 @20
27+
lastErrorMessage @21

generic64.def

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
LIBRARY "generic64.dll"
2-
DESCRIPTION 'Simple D add-in to Excel'
3-
EXETYPE NT
4-
CODE PRELOAD DISCARDABLE
5-
DATA PRELOAD MULTIPLE
1+
LIBRARY "generic64e.xll"
62
EXPORTS
73
xlAutoOpen @2
84
xlAutoClose @3
@@ -22,3 +18,5 @@ EXPORTS
2218
fDialog @17
2319
fExit @18
2420
FuncFib @19
21+
WrapSquare3 @20
22+
lastErrorMessage @21

make32.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
dmd -c -g -m32 -ofgeneric32.obj generic.d memorymanager.d memorypool.d xlcall.d xlcallcpp.d framework.d
1+
dmd -c -g -m32 -ofgeneric32.obj generic.d memorymanager.d memorypool.d xlcall.d xlcallcpp.d framework.d wrap.d
22
dmd -m32 -ofgeneric32.xll -L/IMPLIB generic32.obj generic32.def xlcall32d.lib -g -map

make64.bat

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
rm generic64.xll
2-
dmd -c -gc -m64 -map -ofgeneric64.obj generic.d memorymanager.d memorypool.d xlcall.d xlcallcpp.d framework.d
3-
dmd -m64 -g -L/OUT:generic64.xll -L/NOLOGO -L generic64.obj generic64.def xlcall64d.lib
2+
dmd -c -gc -m64 -map -ofgeneric64.obj generic.d memorymanager.d memorypool.d xlcall.d xlcallcpp.d framework.d wrap.d
3+
dmd -m64 -g -L/OUT:generic64d.xll -L/NOLOGO -L generic64.obj generic64.def xlcall64d.lib

genericwithwrap.d renamed to old/generic.d

+3-47
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ import framework;
8181
import core.stdc.wchar_ : wcslen;
8282
import core.stdc.wctype:towlower;
8383
import std.format;
84-
import xlld.wrap;
85-
84+
//import generic;
8685
enum GWLP_WNDPROC=-4;
8786
enum MAXWORD = 0xFFFF;
8887
debug=0;
@@ -153,7 +152,7 @@ wchar[20] g_szBuffer = ""w;
153152
g_rgWorksheetFuncsRows define the number of rows in the table. The
154153
g_rgWorksheetFuncsCols represents the number of columns in the table.
155154
*/
156-
enum g_rgWorksheetFuncsRows =4;
155+
enum g_rgWorksheetFuncsRows =3;
157156
enum g_rgWorksheetFuncsCols =10;
158157

159158
__gshared wstring[g_rgWorksheetFuncsCols][g_rgWorksheetFuncsRows] g_rgWorksheetFuncs =
@@ -181,18 +180,6 @@ __gshared wstring[g_rgWorksheetFuncsCols][g_rgWorksheetFuncsRows] g_rgWorksheetF
181180
"Adds the arguments"w,
182181
"Number1,number2,... are 1 to 29 arguments for which you want to sum."w
183182
],
184-
[ "WrapSquare3"w,
185-
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"w, // up to 255 args in Excel 2007 and later,
186-
// upto 29 args in Excel 2003 and earlier versions
187-
"WrapSquare3"w,
188-
"number1,number2,..."w,
189-
"1"w,
190-
"Generic Add-In"w,
191-
""w,
192-
""w,
193-
"Sum of squares of the arguments"w,
194-
"Number1,number2,... are 1 to 29 arguments for which you want to sum."w
195-
],
196183
[ "FuncFib"w,
197184
"UU"w,
198185
"FuncFib"w,
@@ -461,8 +448,7 @@ extern(Windows) BOOL /*APIENTRY*/ DllMain( HANDLE hDLL, DWORD dwReason, LPVOID l
461448
extern(Windows) int /*WINAPI*/ xlAutoOpen()
462449
{
463450
import std.conv;
464-
import core.runtime:rt_init;
465-
rt_init();
451+
466452
static XLOPER12 xDLL, // name of this DLL //
467453
xMenu, // xltypeMulti containing the menu //
468454
xTool, // xltypeMulti containing the toolbar //
@@ -688,9 +674,6 @@ extern(Windows) int /*WINAPI*/ xlAutoClose()
688674
Excel12f(xlFree, cast(XLOPER12*)0, [cast(LPXLOPER12) &xRes]);
689675
}
690676

691-
import core.runtime:rt_term;
692-
rt_term();
693-
694677
return 1;
695678
}
696679

@@ -1301,33 +1284,6 @@ extern(Windows) LPXLOPER12 /*WINAPI*/ Func1 (LPXLOPER12 x)
13011284
History: Date Author Reason
13021285
*/
13031286

1304-
extern(Windows) LPXLOPER12 WrapSquare3(
1305-
LPXLOPER12 px1,LPXLOPER12 px2,LPXLOPER12 px3,LPXLOPER12 px4,
1306-
LPXLOPER12 px5,LPXLOPER12 px6,LPXLOPER12 px7,LPXLOPER12 px8,
1307-
LPXLOPER12 px9,LPXLOPER12 px10,LPXLOPER12 px11,LPXLOPER12 px12,
1308-
LPXLOPER12 px13,LPXLOPER12 px14,LPXLOPER12 px15,LPXLOPER12 px16,
1309-
LPXLOPER12 px17,LPXLOPER12 px18,LPXLOPER12 px19,LPXLOPER12 px20,
1310-
LPXLOPER12 px21,LPXLOPER12 px22,LPXLOPER12 px23,LPXLOPER12 px24,
1311-
LPXLOPER12 px25,LPXLOPER12 px26,LPXLOPER12 px27,LPXLOPER12 px28,
1312-
LPXLOPER12 px29)
1313-
{
1314-
import std.algorithm:map,sum;
1315-
import std.experimental.allocator;
1316-
import std.conv:to;
1317-
auto args=px1.fromXLOPER12!(double[]);
1318-
double[][] retD;
1319-
retD.length=args.length;
1320-
foreach(i;0..args.length)
1321-
{
1322-
retD[i].length=args.length;
1323-
foreach(j;0..args.length)
1324-
{
1325-
retD[i][j]=args[0..j+1].map!(arg=>arg*arg).sum;
1326-
}
1327-
}
1328-
return makeXLOPER12(retD);
1329-
}
1330-
13311287
extern(Windows) LPXLOPER12 /*WINAPI*/ FuncSum(
13321288
LPXLOPER12 px1,LPXLOPER12 px2,LPXLOPER12 px3,LPXLOPER12 px4,
13331289
LPXLOPER12 px5,LPXLOPER12 px6,LPXLOPER12 px7,LPXLOPER12 px8,
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
rm generic64e.xll
2-
dmd -c -gc -m64 -map -ofgeneric64.obj genericwithwrap.d memorymanager.d memorypool.d xlcall.d xlcallcpp.d framework.d wrap.d
3-
dmd -m64 -g -L/OUT:generic64e.xll -L/NOLOGO -L generic64.obj generic64.def xlcall64d.lib
1+
rm generic64e.xll
2+
dmd -c -gc -m64 -map -ofgeneric64.obj genericwithwrap.d memorymanager.d memorypool.d xlcall.d xlcallcpp.d framework.d wrap.d
3+
dmd -m64 -g -L/OUT:generic64e.xll -L/NOLOGO -L generic64.obj generic64.def xlcall64d.lib

0 commit comments

Comments
 (0)