-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxll_array_index.cpp
More file actions
59 lines (51 loc) · 1.41 KB
/
xll_array_index.cpp
File metadata and controls
59 lines (51 loc) · 1.41 KB
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
// xll_array_index.cpp - Project rows/columns
#include "xll_array.h"
using namespace xll;
AddIn xai_array_index(
Function(XLL_FP, "xll_array_index", "ARRAY.INDEX")
.Arguments({
Arg(XLL_FP, "array", "is a array or handle to a array."),
Arg(XLL_LPOPER, "rows", "are an array of rows to return."),
Arg(XLL_LPOPER, "columns", "are an array of columns to return."),
})
.FunctionHelp("Return rows and columns of array.")
.Category(CATEGORY)
.Documentation(R"(
This works like <code>INDEX</code> for arrays except indices are cyclic.
If <code>rows</code> or <code>columns</code> are missing then all
rows or columns are returned.
)")
);
_FP12* WINAPI xll_array_index(_FP12* pa, LPOPER pr, LPOPER pc)
{
#pragma XLLEXPORT
static FPX a;
try {
unsigned r = size(*pr);
unsigned c = size(*pc);
if (size(*pa) == 1) {
handle<FPX> h_(pa->array[0]);
if (h_) {
pa = h_->get();
}
}
if (isMissing(*pr)) {
r = pa->rows;
}
if (isMissing(*pc)) {
c = pa->columns;
}
a.resize(r, c);
for (unsigned i = 0; i < r; ++i) {
unsigned ri = isMissing(*pr) ? i : static_cast<unsigned>((*pr)[i].val.num);
for (unsigned j = 0; j < c; ++j) {
unsigned cj = isMissing(*pc) ? j : static_cast<unsigned>((*pc)[j].val.num);
a(i, j) = index(*pa, ri, cj);
}
}
}
catch (const std::exception& ex) {
XLL_ERROR(ex.what());
}
return a.get();
}