1 module libxlsxd.worksheet;
2 
3 import libxlsxd.types;
4 import libxlsxd.datetime;
5 import libxlsxd.format;
6 import libxlsxd.chart;
7 
8 import libxlsxd.xlsxwrap;
9 
10 struct Worksheet {
11 	import std.string : toStringz;
12 	import std.exception : enforce;
13 	lxw_worksheet* handle;
14 
15 	this(lxw_worksheet* handle) @nogc nothrow {
16 		this.handle = handle;
17 	}
18 
19 	void write(T)(RowType row, ColType col, T value) {
20 		this.write(row, col, value, Format(null));
21 	}
22 
23 	void write(T)(RowType row, ColType col, T value, Format format) {
24 		import std.traits : isIntegral, isFloatingPoint, isSomeString;
25 		static if((isFloatingPoint!T || isIntegral!T) && !is(T == bool)) {
26 			this.writeNumber(row, col, value, format);
27 		} else static if(isSomeString!T) {	
28 			this.writeString(row, col, value, format);
29 		} else static if(is(T == Datetime)) {	
30 			this.writeDatetime(row, col, value, format);
31 		} else static if(is(T == bool)) {	
32 			this.writeBoolean(row, col, value, format);
33 		} else {
34 			static assert(false, "The function 'write' does not support type
35 					'" ~ T.stringof ~ "'");
36 		}
37 	}
38 
39 	size_t writeAndGetWidth(T)(RowType row, ColType col, T value) {
40 		return writeAndGetWidth(row, col, value, Format(null));
41 	}
42 
43 	size_t writeAndGetWidth(T)(RowType row, ColType col, T value, 
44 			Format format) 
45 	{
46 		import std.traits : isIntegral, isFloatingPoint, isSomeString;
47 		import std.conv : to;
48 		static if((isFloatingPoint!T || isIntegral!T) && !is(T == bool)) {
49 			this.writeNumber(row, col, value, format);
50 			return to!string(value).length;
51 		} else static if(isSomeString!T) {	
52 			this.writeString(row, col, value, format);
53 			return value.length;
54 		} else static if(is(T == bool)) {	
55 			this.writeBoolean(row, col, value, format);
56 			return value ? 4 : 5;
57 		} else {
58 			static assert(false, "The function 'writeAndGetWidth' does not "
59 					~ " support type '" ~ T.stringof ~ "'");
60 		}
61 	}
62 
63 	void writeNumber(RowType row, ColType col, double num) {
64 		this.writeNumber(row, col, num, Format(null));
65 	}
66 
67 	void writeNumber(RowType row, ColType col, double num,
68 			Format format) 
69 	{
70 		enforce(worksheet_write_number(this.handle, row, col,
71 					num, format.handle) == LXW_NO_ERROR);
72 	}
73 
74 	void writeString(RowType row, ColType col, string str) {
75 		this.writeString(row, col, str, Format(null));
76 	}
77 
78 	void writeString(RowType row, ColType col, string str, Format format) {
79 		enforce(worksheet_write_string(this.handle, row, col,
80 					toStringz(str), format.handle) == LXW_NO_ERROR);
81 	}
82 
83 	void writeFormula(RowType row, ColType col, string formula) {
84 		this.writeFormula(row, col, formula, Format(null));
85 	}
86 	
87 	void writeFormula(RowType row, ColType col, string formula, Format format) {
88 		enforce(worksheet_write_formula(this.handle, row, col,
89 					toStringz(formula), format.handle) == LXW_NO_ERROR);
90 	}
91 	
92 	void writeArrayFormula(RowType firstRow, ColType firstCol,
93 			RowType lastRow, ColType lastCol, string formula) 
94 	{
95 		this.writeArrayFormula(firstRow, firstCol, lastRow, lastCol, formula,
96 				Format(null)
97 			);
98 	}
99 
100 	void writeArrayFormula(RowType firstRow, ColType firstCol,
101 			RowType lastRow, ColType lastCol, string formula, Format format) 
102 	{
103 		enforce(worksheet_write_array_formula(this.handle, firstRow,
104 					firstCol, lastRow, lastCol, toStringz(formula),
105 					format.handle) == LXW_NO_ERROR);
106 	}
107 	
108 	void writeDatetime(RowType row, ColType col, Datetime datetime) {
109 		this.writeDatetime(row, col, datetime, Format(null));
110 	}
111 
112 	void writeDatetime(RowType row, ColType col, Datetime datetime, 
113 			Format format) 
114 	{
115 		enforce(worksheet_write_datetime(this.handle, row, col,
116 					&datetime.handle, format.handle) == LXW_NO_ERROR);
117 	}
118 
119 	void writeUrl(RowType row, ColType col, string url) {
120 		this.writeUrl(row, col, url, Format(null));
121 	}
122 
123 	void writeUrl(RowType row, ColType col, string url, Format format) {
124 		enforce(worksheet_write_url(this.handle, row, col,
125 					toStringz(url), format.handle) == LXW_NO_ERROR);
126 	}
127 
128 	void writeBoolean(RowType row, ColType col, bool value) {
129 		this.writeBoolean(row, col, value, Format(null));
130 	}
131 
132 	void writeBoolean(RowType row, ColType col, bool value, Format format) {
133 		enforce(worksheet_write_boolean(this.handle, row, col,
134 					value, format.handle) == LXW_NO_ERROR);
135 	}
136 
137 	void writeBlank(RowType row, ColType col) {
138 		this.writeBlank(row, col, Format(null));
139 	}
140 
141 	void writeBlank(RowType row, ColType col, Format format) {
142 		enforce(worksheet_write_blank(this.handle, row, col,
143 					format.handle) == LXW_NO_ERROR);
144 	}
145 
146 	void writeFormulaNum(RowType row, ColType col, string formula,
147 			double value) 
148 	{
149 		this.writeFormulaNum(row, col, formula, Format(null), value);
150 	}
151 
152 	void writeFormulaNum(RowType row, ColType col, string formula,
153 			Format format, double value) 
154 	{
155 		enforce(worksheet_write_formula_num(this.handle, row,
156 					col, toStringz(formula), format.handle, value
157 				)
158 				== LXW_NO_ERROR
159 			);
160 	}
161 
162 	void writeRichString(RowType row, ColType col,
163 			lxw_rich_string_tuple** rst)
164 	{
165 		this.writeRichString(row, col, rst, Format(null));
166 	}
167 
168 	void writeRichString(RowType row, ColType col,
169 			lxw_rich_string_tuple** rst, Format format)
170 	{
171 		enforce(worksheet_write_rich_string(this.handle, row,
172 					col, rst, format.handle
173 				)
174 				== LXW_NO_ERROR
175 			);
176 	}
177 
178 	void setRow(RowType row, double height) {
179 		this.setRow(row, height, Format(null));
180 	}
181 
182 	void setRow(RowType row, double height, Format format) {
183 		enforce(worksheet_set_row(this.handle, row, height, format.handle)
184 				== LXW_NO_ERROR
185 			);
186 	}
187 
188 	void setRowOpt(RowType row, double height, lxw_row_col_options* options) {
189 		this.setRowOpt(row, height, Format(null), options);
190 	}
191 
192 	void setRowOpt(RowType row, double height, Format format,
193 			lxw_row_col_options* options) 
194 	{
195 		enforce(worksheet_set_row_opt(this.handle, row, height,
196 					format.handle, options) == LXW_NO_ERROR);
197 	}
198 
199 	void setColumn(ColType firstCol, ColType lastCol, double width) {
200 		this.setColumn(firstCol, lastCol, width, Format(null));
201 	}
202 
203 	void setColumn(ColType firstCol, ColType lastCol, double width, 
204 			Format format) 
205 	{
206 		enforce(worksheet_set_column(this.handle, firstCol, lastCol,
207 					width, format.handle
208 				)
209 				== LXW_NO_ERROR
210 			);
211 	}
212 
213 	void setColumnOpt(ColType firstCol, ColType lastCol, double width,
214 			lxw_row_col_options* options) 
215 	{
216 		this.setColumnOpt(firstCol, lastCol, width, Format(null), options);
217 	}
218 
219 	void setColumnOpt(ColType firstCol, ColType lastCol, double width,
220 			Format format, lxw_row_col_options* options) 
221 	{
222 		enforce(worksheet_set_column_opt(this.handle, firstCol, lastCol,
223 					width, format.handle, options)
224 				== LXW_NO_ERROR
225 			);
226 	}
227 
228 	void insertImage(RowType row, ColType col, string filename) {
229 		enforce(worksheet_insert_image(this.handle, row, col,
230 					toStringz(filename)
231 				)
232 				== LXW_NO_ERROR
233 			);
234 	}
235 
236 	void insertImageOpt(RowType row, ColType col, string filename,
237 			lxw_image_options* options) 
238 	{
239 		enforce(worksheet_insert_image_opt(this.handle, row, col,
240 					toStringz(filename), options
241 				)
242 				== LXW_NO_ERROR
243 			);
244 	}
245 
246 	void insertImageBuffer(RowType row, ColType col, const(ubyte)* buf,
247 			size_t bufSize) 
248 	{
249 		enforce(worksheet_insert_image_buffer(this.handle, row,
250 					col, buf, bufSize
251 				)
252 				== LXW_NO_ERROR
253 			);
254 	}
255 
256 	void insertImageBufferOpt(RowType row, ColType col, const(ubyte)* buf,
257 			size_t bufSize, lxw_image_options* options) 
258 	{
259 		enforce(worksheet_insert_image_buffer_opt(this.handle, row,
260 					col, buf, bufSize, options
261 				)
262 				== LXW_NO_ERROR
263 			);
264 	}
265 
266 	void insertChart(RowType row, ColType col, Chart chart) {
267 		enforce(worksheet_insert_chart(this.handle, row, col,
268 					chart.handle) == LXW_NO_ERROR);
269 	}
270 
271 	void insertChartOpt(RowType row, ColType col, Chart chart,
272 			lxw_image_options* options) 
273 	{
274 		enforce(worksheet_insert_chart_opt(this.handle, row,
275 					col, chart.handle, options
276 				)
277 				== LXW_NO_ERROR
278 			);
279 	}
280 
281 	void mergeRange(RowType firstRow, ColType firstCol, RowType lastRow,
282 			ColType lastCol, string str) 
283 	{
284 		this.mergeRange(firstRow, firstCol, lastRow,
285 			lastCol, str, Format(null));
286 	}
287 
288 	void mergeRange(RowType firstRow, ColType firstCol, RowType lastRow,
289 			ColType lastCol, string str, Format format) 
290 	{
291 		enforce(worksheet_merge_range(this.handle, firstRow, firstCol,
292 					lastRow, lastCol, toStringz(str), format.handle
293 				)
294 				== LXW_NO_ERROR
295 			);
296 	}
297 
298 	void autofilter(RowType firstRow, ColType firstCol, RowType lastRow,
299 			ColType lastCol) {
300 		enforce(worksheet_autofilter(this.handle, firstRow, firstCol,
301 					lastRow, lastCol) == LXW_NO_ERROR);
302 	}
303 
304 	void dataValidationCell(RowType row, ColType col,
305 			lxw_data_validation* validator) 
306 	{
307 		enforce(worksheet_data_validation_cell(this.handle, row,
308 					col, validator
309 				)
310 				== LXW_NO_ERROR
311 			);
312 	}
313 
314 	void dataValidationRange(RowType firstRow, ColType firstCol,
315 			RowType lastRow, ColType lastCol, lxw_data_validation* validator) 
316 	{
317 		enforce(worksheet_data_validation_range(this.handle, firstRow,
318 					firstCol, lastRow, lastCol, validator
319 				)
320 				== LXW_NO_ERROR
321 			);
322 	}
323 
324 	void activate() @nogc nothrow {
325 		worksheet_activate(this.handle);
326 	}
327 
328 	void select() @nogc nothrow {
329 		worksheet_select(this.handle);
330 	}
331 
332 	void hide() @nogc nothrow {
333 		worksheet_hide(this.handle);
334 	}
335 
336 	void setFirstSheet() @nogc nothrow {
337 		worksheet_set_first_sheet(this.handle);
338 	}
339 
340 	void freezePanes(RowType row, ColType col) @nogc nothrow {
341 		worksheet_freeze_panes(this.handle, row, col);
342 	}
343 
344 	void splitPanes(double vertical, double horizontal) @nogc nothrow {
345 		worksheet_split_panes(this.handle, vertical, horizontal);
346 	}
347 
348 	void setSelection(RowType firstRow, ColType firstCol, RowType lastRow,
349 			ColType lastCol) @nogc nothrow 
350 	{
351 		worksheet_set_selection(this.handle, firstRow, firstCol, lastRow,
352 				lastCol
353 			);
354 	}
355 
356 	void setLandscape() @nogc nothrow {
357 		worksheet_set_landscape(this.handle);
358 	}
359 
360 	void setPortrait() @nogc nothrow {
361 		worksheet_set_portrait(this.handle);
362 	}
363 
364 	void setPageView() @nogc nothrow {
365 		worksheet_set_page_view(this.handle);
366 	}
367 
368 	void setPaper(ubyte paperType) @nogc nothrow {
369 		worksheet_set_paper(this.handle, paperType);
370 	}
371 
372 	void setMargins(double left, double right, double top, double bottom)
373 			@nogc nothrow 
374 	{
375 		worksheet_set_margins(this.handle, left, right, top, bottom);
376 	}
377 
378 	void setHeader(string header) {
379 		enforce(worksheet_set_header(this.handle, toStringz(header))
380 				== LXW_NO_ERROR
381 			);
382 	}
383 
384 	void setFooter(string footer) {
385 		enforce(worksheet_set_footer(this.handle, toStringz(footer))
386 				== LXW_NO_ERROR
387 			);
388 	}
389 
390 	void setHeaderOpt(string header, lxw_header_footer_options* options) {
391 		enforce(worksheet_set_header_opt(this.handle, toStringz(header),
392 					options
393 				)
394 				== LXW_NO_ERROR
395 			);
396 	}
397 
398 	void setFooterOpt(string footer, lxw_header_footer_options* options) {
399 		enforce(worksheet_set_footer_opt(this.handle, toStringz(footer),
400 					options
401 				)
402 				== LXW_NO_ERROR
403 			);
404 	}
405 
406 	void setHPagebreaks(RowType[] row) {
407 		enforce(worksheet_set_h_pagebreaks(this.handle, row.ptr)
408 				== LXW_NO_ERROR
409 			);
410 	}
411 
412 	void setVPagebreaks(ColType[] col) {
413 		enforce(worksheet_set_v_pagebreaks(this.handle, col.ptr)
414 				== LXW_NO_ERROR
415 			);
416 	}
417 
418 	void printAcross() @nogc nothrow {
419 		worksheet_print_across(this.handle);
420 	}
421 
422 	void setZoom(ushort scale) @nogc nothrow {
423 		worksheet_set_zoom(this.handle, scale);
424 	}
425 
426 	void gridlines(ubyte option) @nogc nothrow {
427 		worksheet_gridlines(this.handle, option);
428 	}
429 
430 	void centerHorizontally() @nogc nothrow {
431 		worksheet_center_horizontally(this.handle);
432 	}
433 
434 	void centerVertically() @nogc nothrow {
435 		worksheet_center_vertically(this.handle);
436 
437 	}
438 	void printRowColHeaders() @nogc nothrow {
439 		worksheet_print_row_col_headers(this.handle);
440 	}
441 
442 	void repeatRows(RowType firstRow, RowType lastRow) {
443 		enforce(worksheet_repeat_rows(this.handle, firstRow, lastRow) ==
444 				LXW_NO_ERROR);
445 	}
446 
447 	void repeatColumns(ColType firstCol, ColType lastCol) {
448 		enforce(worksheet_repeat_columns(this.handle, firstCol, lastCol)
449 				== LXW_NO_ERROR
450 			);
451 	}
452 
453 	void printArea(RowType firstRow, ColType firstCol, RowType lastRow,
454 			ColType lastCol) 
455 	{
456 		enforce(worksheet_print_area(this.handle, firstRow, firstCol, lastRow,
457 					lastCol
458 				)
459 				== LXW_NO_ERROR
460 			);
461 	}
462 
463 	void fitToPages(ushort width, ushort height) @nogc nothrow {
464 		worksheet_fit_to_pages(this.handle, width, height);
465 	}
466 
467 	void setStartPage(ushort startPage) @nogc nothrow {
468 		worksheet_set_start_page(this.handle, startPage);
469 	}
470 
471 	void setPrintScale(ushort scale) @nogc nothrow {
472 		worksheet_set_print_scale(this.handle, scale);
473 	}
474 
475 	void rightToLeft() @nogc nothrow {
476 		worksheet_right_to_left(this.handle);
477 	}
478 
479 	void hideZero() @nogc nothrow {
480 		worksheet_hide_zero(this.handle);
481 	}
482 
483 	void setTabColor(lxw_color_t color) @nogc nothrow {
484 		worksheet_set_tab_color(this.handle, color);
485 	}
486 
487 	void protect(string password, lxw_protection* options) {
488 		worksheet_protect(this.handle, toStringz(password), options);
489 	}
490 
491 	void outlineSettings(ubyte visible, ubyte symbolsBelow,
492 			ubyte symbolsRight, ubyte autoStyle) @nogc nothrow 
493 	{
494 		worksheet_outline_settings(this.handle, visible, symbolsBelow,
495 			symbolsRight, autoStyle);
496 	}
497 
498 	void setDefaultRow(double height, ubyte hideUnusedRows) @nogc nothrow {
499 		worksheet_set_default_row(this.handle, height, hideUnusedRows);
500 	}
501 }