1 /**
2    This module provides D versions of xlf Excel "functions"
3  */
4 module xlld.func.xlf;
5 
6 import xlld.func.framework: excel12;
7 import xlld.sdk.xlcall: XLOPER12;
8 
9 
10 // should be pure but can't due to calling Excel12
11 int year(double date) @safe @nogc nothrow {
12     import xlld.sdk.xlcall: xlfYear;
13     return datePart(date, xlfYear);
14 }
15 
16 // should be pure but can't due to calling Excel12
17 int month(double date) @safe @nogc nothrow {
18     import xlld.sdk.xlcall: xlfMonth;
19     return datePart(date, xlfMonth);
20 }
21 
22 // should be pure but can't due to calling Excel12
23 int day(double date) @safe @nogc nothrow {
24     import xlld.sdk.xlcall: xlfDay;
25     return datePart(date, xlfDay);
26 }
27 
28 // should be pure but can't due to calling Excel12
29 int hour(double date) @safe @nogc nothrow {
30     import xlld.sdk.xlcall: xlfHour;
31     return datePart(date, xlfHour);
32 }
33 
34 // should be pure but can't due to calling Excel12
35 int minute(double date) @safe @nogc nothrow {
36     import xlld.sdk.xlcall: xlfMinute;
37     return datePart(date, xlfMinute);
38 }
39 
40 // should be pure but can't due to calling Excel12
41 int second(double date) @safe @nogc nothrow {
42     import xlld.sdk.xlcall: xlfSecond;
43     return datePart(date, xlfSecond);
44 }
45 
46 private int datePart(double date, int xlfn) @safe @nogc nothrow {
47     // the Excel APIs for some reason return a double
48     try
49         return cast(int)excel12!double(xlfn, date);
50     catch(Exception ex)
51         return 0;
52 }
53 
54 
55 double date(int year, int month, int day) @safe @nogc nothrow {
56     import xlld.sdk.xlcall: xlfDate;
57     try
58         return excel12!double(xlfDate, year, month, day);
59     catch(Exception ex)
60         return 0;
61 }
62 
63 double time(int year, int month, int day) @safe @nogc nothrow {
64     import xlld.sdk.xlcall: xlfTime;
65     try
66         return excel12!double(xlfTime, year, month, day);
67     catch(Exception ex)
68         return 0;
69 }
70 
71 int rtd(XLOPER12 comId,
72         XLOPER12 server,
73         XLOPER12 topic0 = XLOPER12(),
74         XLOPER12 topic1 = XLOPER12(),
75         XLOPER12 topic2 = XLOPER12(),
76         XLOPER12 topic3 = XLOPER12(),
77         XLOPER12 topic4 = XLOPER12(),
78         XLOPER12 topic5 = XLOPER12(),
79         XLOPER12 topic6 = XLOPER12(),
80         XLOPER12 topic7 = XLOPER12(),
81         XLOPER12 topic8 = XLOPER12(),
82         XLOPER12 topic9 = XLOPER12())
83 {
84     import xlld.sdk.xlcall: xlfRtd;
85     import xlld.sdk.framework: Excel12f;
86 
87     XLOPER12 result;
88     return Excel12f(xlfRtd, &result, comId, server,
89                     topic0, topic1, topic2, topic3, topic4, topic5, topic6, topic7, topic8, topic9);
90 }
91 
92 __gshared immutable callerException = new Exception("Error calling xlfCaller");
93 
94 /**
95    Returns the caller. The returned XLOPER12 is documented here:
96    https://docs.microsoft.com/en-us/office/client-developer/excel/xlfcaller
97  */
98 auto caller() @safe {
99     import xlld.sdk.xlcall: xlfCaller, xlretSuccess;
100     import xlld.sdk.framework: Excel12f;
101     import xlld.func.xl: ScopedOper;
102 
103     XLOPER12 result;
104     () @trusted {
105         if(Excel12f(xlfCaller, &result) != xlretSuccess) {
106             throw callerException;
107         }
108     }();
109 
110     return ScopedOper(result);
111 }
112 
113 auto callerCell() @safe {
114     import xlld.sdk.xlcall: XlType;
115     import xlld.func.xl: coerce, free, Coerced;
116 
117     auto oper = caller();
118 
119     if(oper.xltype != XlType.xltypeSRef)
120         throw new Exception("Caller not a cell");
121 
122     return Coerced(oper);
123 }