-
Notifications
You must be signed in to change notification settings - Fork 23
/
test.cpp
333 lines (291 loc) · 7.57 KB
/
test.cpp
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
#include <cmath>
// Uncomment to use Excel4 API. Default is XLOPER12.
//#define XLL_VERSION 4
#include "../xll/xll.h"
#include "../xll/macrofun/xll_macrofun.h"
using namespace xll;
//int break_me = []() { return _crtBreakAlloc = 1178; }();
#ifdef _DEBUG
Auto<Open> xao_test_doc([]() {
xll_url_base::set("https://xlladdins.com/");
return 1;
});
Auto<OpenAfter> xaoa_test_doc([]() {
const char* platform;
platform = Platform();
return Documentation("TEST", "Excel test functions");
});
#endif
XLL_CONST(LONG, CONST_NAME, 42, "a constant", "XLL", "https://google.com");
// Use Alt-F8 then type 'XLL.MACRO' to call 'xll_macro'
// See https://xlladdins.github.io/Excel4Macros/
// for documentation of Excel arguments.
AddIn xai_macro(Macro("xll_macro", "XLL.MACRO"));
// All functions called from Excel must be declared with WINAPI.
int WINAPI xll_macro(void)
{
// And made available to Excel using a pragma.
#pragma XLLEXPORT
Excel(xlcAlert,
Excel(xlfConcatenate,
OPER("XLL.MACRO called with активный cell: "),
Excel(xlfReftext, Excel(xlfActiveCell), OPER(true))
),
OPER(2), // general information
OPER("https://xlladdins.github.io/Excel4Macros/alert.html") // help
);
return TRUE;
}
AddIn xai_tgamma(
// Return a double by calling xll_tgamma using TGAMMA in Excel.
Function(XLL_DOUBLE, "xll_tgamma", "TGAMMA")
// Arguments are an array of one Arg that is a double.
.Arguments({
Arg(XLL_DOUBLE, "x", "is the value for which you want to calculate Gamma.", "10*rand()")
})
.FunctionHelp("Return the Gamma function value.")
.Category("CMATH")
.HelpTopic("https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tgamma-tgammaf-tgammal")
.Documentation(R"xyz(
The Gamma function is \(\Gamma(\alpha) = \int_0^\infty x^{\alpha - 1} e^{-x}\,dx\)
for \(\alpha > 0\).
It satisfies \(\Gamma(n + 1) = n!\) if \(n\) is a natural number.
)xyz")
);
double WINAPI xll_tgamma(double x)
{
#pragma XLLEXPORT
return tgamma(x);
}
#if 0 // change to 1 to test duplicate function names
// AddIn previously defined: TGAMMA
AddIn xai_tgamma2(
Function(XLL_DOUBLE, "xll_tgamma2", "TGAMMA")
.Arguments({
Arg(XLL_DOUBLE, "x", "is the value for which you want to calculate Gamma.")
})
.FunctionHelp("Return the Gamma function value.")
.Category("CMATH")
.HelpTopic("https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tgamma-tgammaf-tgammal")
);
double WINAPI xll_tgamma2(double x)
{
#pragma XLLEXPORT
return tgamma(x);
}
#endif
AddIn xai_jn(
Function(XLL_DOUBLE, "xll_jn", "JN")
.Arguments({
Arg(XLL_LONG, "n", "is the order of the Bessel function.", "1"),
Arg(XLL_DOUBLE, "x", "is the value for which you want to calculate the Bessel function.", "1+.1")
})
.FunctionHelp("Return the value of the n-th order Bessel function of the first kind.")
.Category("CMATH")
.Documentation(R"xyzyx(The \(n\)-th order Bessel function of the first kind
\[
J_n(x) = \sum_{m=0}^\infty \frac{(-1)^m}{m!\Gamma(m + n + 1)}\left(\frac{x}{2}\right)^{2m + n}.
\]
)xyzyx")
);
double WINAPI xll_jn(LONG n, double x)
{
#pragma XLLEXPORT
// This results in #NUM! if returned to Excel.
double result = std::numeric_limits<double>::quiet_NaN();
// Catch exeptions.
try {
result = _jn(n, x);
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
catch (...) {
XLL_ERROR("Unknown exception thrown in: " __FUNCTION__);
}
return result;
}
#if 0
// lambda gets called on open
Auto<Open> xai_open([]() {
return !Excel(xlcAlert, OPER("Auto<Open> called")).is_err();
});
Auto<Close> xai_close([]() {
Excel(xlcAlert, OPER("Auto<Close> called"));
return TRUE;
});
AddIn xai_onwindow(Macro("xll_onwindow", "XLL.ONWINDOW"));
int WINAPI xll_onwindow(void)
{
#pragma XLLEXPORT
Excel(xlcAlert, OPER("ONWINDOW called"));
return TRUE;
}
On<Window> xon_window("", "XLL.ONWINDOW");
AddIn xai_onsheet(Macro("xll_onsheet", "XLL.ONSHEET"));
int WINAPI xll_onsheet(void)
{
#pragma XLLEXPORT
Excel(xlcAlert, OPER("ONSHEET called"));
return TRUE;
}
On<Sheet> xon_sheet("", "XLL.ONSHEET", true);
#endif
AddIn xai_onkey(Macro("xll_onkey", "XLL.ONKEY")
.FunctionHelp("Called when Ctrl-Alt-a is pressed.")
);
int WINAPI xll_onkey(void)
{
#pragma XLLEXPORT
Excel(xlcAlert, OPER("You pressed Ctrl-Alt-a"));
return TRUE;
}
On<Key> xon_key(ON_CTRL ON_ALT "a", "XLL.ONKEY");
AddIn xai_get_workspace(
Function(XLL_LPOPER, "xll_get_workspace", "GET_WORKSPACE")
.Arguments({
Arg(XLL_SHORT, "type_num", "is a number specifying the type of workspace information you want.")
})
.Uncalced()
.FunctionHelp("Return workspace information.")
.Category("XLL")
.HelpTopic("https://xlladdins.github.io/Excel4Macros/get.workspace.html")
);
LPOPER WINAPI xll_get_workspace(SHORT type_num)
{
#pragma XLLEXPORT
static OPER oResult;
try {
oResult = Excel(xlfGetWorkspace, OPER(type_num));
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
oResult = ErrValue;
}
return &oResult;
}
// UTF-8 test
AddIn xai_utf8(Macro("xll_utf8", "XLL.UTF8"));
int WINAPI xll_utf8(void)
{
#pragma XLLEXPORT
Excel(xlcAlert,
OPER("отлично"),
OPER(2), // general information
OPER("https://translate.google.com/#view=home&op=translate&sl=auto&tl=en&text=%D0%BE%D1%82%D0%BB%D0%B8%D1%87%D0%BDo!0")
);
return TRUE;
}
AddIn xai_file(
Function(XLL_LPOPER, "xll_file", "XLL.FILE")
.Arguments({
Arg(XLL_LPOPER, "url", "is a URL to retrieve.")
})
.Category("XLL")
.FunctionHelp("Return contents of URL.")
);
LPOPER WINAPI xll_file(LPOPER po)
{
#pragma XLLEXPORT
static OPER f;
f = Excel(xlfWebservice, *po);
return &f;
}
// ma_n = (x_1 + ... + x_n)/n
// = (ma_{n-1} + x_n)/n
// = ma_{n-1} + (- ma_{n-1} + x)/n
class MA {
size_t n;
double ma;
public:
MA()
: n(0), ma(0)
{ }
MA(size_t n_, const double* x)
: MA()
{
while (n_--) {
next(*x++);
}
}
size_t count() const
{
return n;
}
double value() const
{
return ma;
}
MA& next(double x)
{
++n;
ma += (x - ma) / n;
return *this;
}
};
template<class X, class Y>
struct compose {
const X& x;
const Y& y;
compose(const X& x, const Y& y)
: x(x), y(y)
{ }
double value() const
{
return y.value();
}
compose& next(double z)
{
x.next(z);
y.next(x.value());
return *this;
}
};
#if 0
AddIn xai_ma(
Function(XLL_HANDLEX, "xll_ma", "\\XLL.MA")
.Arguments({
Arg(XLL_FP, "init", "array of initial data for moving averate."),
})
.Uncalced()
.Category("XLL")
.FunctionHelp("Compute a moving average.")
.Documentation("Initialize moving average.")
);
HANDLEX WINAPI xll_ma(_FPX *px)
{
#pragma XLLEXPORT
handle<MA> ma(new MA{});
for (unsigned i = 0; i < size(*px); ++i) {
ma->next(px->array[i]);
}
return ma.get();
}
AddIn xai_ma_next(
Function(XLL_FP, "xll_ma_next", "XLL.MA.NEXT")
.Arguments({
Arg(XLL_HANDLEX, "handle", "is a handle to a moving average."),
Arg(XLL_DOUBLE, "x", "is the value whos moving averate is to be computed."),
Arg(XLL_BOOL, "reset", "reset the moving average computation."),
})
.Category("XLL")
.FunctionHelp("Compute a moving average.")
.Documentation("Update moving average.")
);
_FPX* WINAPI xll_ma_next(HANDLEX ma, double x, BOOL reset)
{
#pragma XLLEXPORT
static xll::FPX_<1, 2> result;
handle<MA> ma_(ma);
ensure(ma_);
if (reset) {
*ma_ = MA{};
}
else {
ma_->next(x);
}
result[0] = ma_->value();
result[1] = static_cast<double>(ma_->count());
return (_FPX*)&result;
}
#endif