-
Notifications
You must be signed in to change notification settings - Fork 6
/
workbook.d
204 lines (173 loc) · 4.98 KB
/
workbook.d
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
module libxlsxd.workbook;
import libxlsxd.xlsxwrap;
import std.string : toStringz;
import std.typecons : refCounted, RefCounted;
import libxlsxd.worksheet;
import libxlsxd.format;
import libxlsxd.chart;
import libxlsxd.chartsheet;
import libxlsxd.datetime;
import libxlsxd.docproperties;
alias Workbook = RefCounted!WorkbookImpl;
Workbook newWorkbook(string filename) @trusted {
Workbook wb;
wb.filename = filename;
wb.open();
return wb;
}
struct WorkbookImpl {
WorkbookOpen workBook;
alias workBook this;
~this() @safe {
this.workBook.close();
}
}
struct WorkbookOpen {
import std.exception : enforce;
import std.datetime : DateTime;
import std.array : empty;
lxw_workbook* handle;
string filename;
Format[string] formats;
Format dateFormat;
Format timeFormat;
Format dateTimeFormat;
static WorkbookOpen opCall(string filename) @safe {
WorkbookOpen ret;
ret.filename = filename;
ret.open();
ret.buildDefaultFormats();
return ret;
}
void open() @trusted {
this.handle = workbook_new(this.filename.toStringz());
this.buildDefaultFormats();
}
void buildDefaultFormats() @safe {
this.dateFormat = this.addFormat();
this.dateFormat.setNumFormat("yyyy-mm-dd");
this.dateTimeFormat = this.addFormat();
this.dateTimeFormat.setNumFormat("yyyy-mm-ddThh:MM:ss");
this.timeFormat = this.addFormat();
this.timeFormat.setNumFormat("hh:MM:ss");
}
void close() @trusted {
if(this.handle !is null) {
workbook_close(this.handle);
}
this.handle = null;
}
WorksheetFluent addFluentWorksheet(string name) @trusted {
return WorksheetFluent(&this, this.addWorksheet(name));
}
Worksheet addWorksheet(string name) nothrow @trusted {
return Worksheet(workbook_add_worksheet(this.handle,
name.empty ? null : name.toStringz()),
&this.dateTimeFormat, &this.dateFormat, &this.timeFormat
);
}
Chartsheet addChartsheet(string name) @trusted {
return Chartsheet(workbook_add_chartsheet(this.handle,
name.empty ? null : name.toStringz())
);
}
Format addFormat() nothrow @nogc @trusted {
return Format(workbook_add_format(this.handle));
}
version(No_Overloads_Or_Templates) {
Format addFormatNamed(string name) nothrow @safe {
return this.addFormatNamedImpl(name);
}
} else {
Format addFormat(string name) nothrow @safe {
return this.addFormatNamedImpl(name);
}
}
private Format addFormatNamedImpl(string name) nothrow @trusted {
auto t = Format(workbook_add_format(this.handle));
this.formats[name] = t;
return t;
}
Format getFormat(string name) nothrow @safe {
return this.formats[name];
}
Chart addChart(ubyte chartType) @nogc nothrow @trusted {
return Chart(workbook_add_chart(this.handle, chartType));
}
void setProperties(DocProperties property) @trusted {
enforce(workbook_set_properties(this.handle, property.handle)
== LXW_NO_ERROR
);
}
version(No_Overloads_Or_Templates) {
void setCustomPropertiesBool(string name, bool b) @safe {
return this.setCustomPropertiesImpl(name, b);
}
void setCustomPropertiesInt(string name, long l) @safe {
return this.setCustomPropertiesImpl(name, l);
}
void setCustomPropertiesNumber(string name, double d) @safe {
return this.setCustomPropertiesImpl(name, d);
}
void setCustomPropertiesDateTime(string name, DateTime d) @safe {
return this.setCustomPropertiesImpl(name, Datetime.fromDateTime(d));
}
} else {
alias setCustomProperties = setCustomPropertiesImpl;
}
private void setCustomPropertiesImpl(T)(string name, T t) @trusted {
import std.traits : isIntegral, isFloatingPoint, isSomeString;
import std.conv : to;
static if(is(T == bool)) {
enforce(workbook_set_custom_property_boolean(this.handle,
name.toStringz(), t
)
== LXW_NO_ERROR
);
} else static if(isIntegral!T) {
enforce(workbook_set_custom_property_integer(this.handle,
name.toStringz(), to!int(t)
)
== LXW_NO_ERROR
);
} else static if(isFloatingPoint!T) {
enforce(workbook_set_custom_property_number(this.handle,
name.toStringz(), to!double(t)
)
== LXW_NO_ERROR
);
} else static if(is(T == Datetime)) {
enforce(workbook_set_custom_property_datetime(this.handle,
toStringz(name), &t.handle)
== LXW_NO_ERROR
);
} else {
static assert(false, "setCustomProperties does not work with '" ~
T.stringof ~ "'");
}
}
void defineName(string name, string formula) @trusted {
enforce(workbook_define_name(this.handle, name.toStringz(),
formula.toStringz()
)
== LXW_NO_ERROR
);
}
Worksheet getWorksheetByName(string name) @trusted {
return Worksheet(workbook_get_worksheet_by_name(this.handle,
name.toStringz()
),
&this.dateTimeFormat, &this.dateFormat, &this.timeFormat
);
}
Chartsheet getChartByName(string name) @trusted {
return Chartsheet(workbook_get_chartsheet_by_name(this.handle,
name.toStringz()
)
);
}
bool validateSheetName(string name) @trusted {
return workbook_validate_sheet_name(this.handle, name.toStringz())
== LXW_NO_ERROR;
}
}