1 module libxlsxd.workbook;
2 
3 import libxlsxd.xlsxwrap;
4 import std.string : toStringz;
5 import std.typecons : refCounted, RefCounted;
6 
7 import libxlsxd.worksheet;
8 import libxlsxd.format;
9 import libxlsxd.chart;
10 import libxlsxd.chartsheet;
11 import libxlsxd.docproperties;
12 
13 alias Workbook = RefCounted!WorkbookImpl;
14 
15 Workbook newWorkbook(string filename) {
16 	Workbook wb;
17 	wb.filename = filename;
18 	wb.open();
19 	return wb;
20 }
21 
22 struct WorkbookImpl {
23 	import std.exception : enforce;
24 	lxw_workbook* handle;
25 	string filename;
26 
27 	Format[string] formats;
28 
29 	void open() {
30 		this.handle = workbook_new(this.filename.toStringz());
31 	}
32 
33 	~this() {
34 		workbook_close(this.handle);
35 	}
36 
37 	Worksheet addWorksheet(string name) nothrow {
38 		return Worksheet(workbook_add_worksheet(this.handle,
39 					name.toStringz())
40 				);
41 	}
42 
43 	Chartsheet addChartsheet(string name) {
44 		return Chartsheet(workbook_add_chartsheet(this.handle, 
45 					name.toStringz())
46 				);
47 	}
48 
49 	Format addFormat() nothrow @nogc {
50 		return Format(workbook_add_format(this.handle));
51 	}
52 
53 	Format addFormat(string name) nothrow {
54 		auto t = Format(workbook_add_format(this.handle));
55 		this.formats[name] = t;
56 		return t;
57 	}
58 
59 	Format getFormat(string name) nothrow {
60 		return this.formats[name];
61 	}
62 
63 	Chart addChart(ubyte chartType) @nogc nothrow {
64 		return Chart(workbook_add_chart(this.handle, chartType));
65 	}
66 
67 	void setProperties(DocProperties property) {
68 		enforce(workbook_set_properties(this.handle, property.handle) 
69 				== LXW_NO_ERROR
70 			);
71 	}
72 
73 	void setCustomProperties(T)(string name, T t) {
74 		import std.traits : isIntegral, isFloatingPoint, isSomeString;
75 		import std.datetime.datetime : DateTime;
76 
77 		static if(is(T == bool)) {
78 			enforce(workbook_set_custom_property_boolean(this.handle,
79 						name.toStringz(), t
80 					) 
81 					== LXW_NO_ERROR
82 				);
83 		} else static if(isIntegral!T) {
84 			enforce(workbook_set_custom_property_integer(this.handle,
85 						name.toStringz(), to!int(t)
86 					) 
87 					== LXW_NO_ERROR
88 				);
89 		} else static if(isFloatingPoint!T) {
90 			enforce(workbook_set_custom_property_number(this.handle,
91 						name.toStringz(), to!double(t)
92 					) 
93 					== LXW_NO_ERROR
94 				);
95 		} else static if(is(T == Datetime)) {
96 			enforce(workbook_set_custom_property_datetime(this.handle, 
97 						toStringz(name), &t.handle)
98 					== LXW_NO_ERROR
99 				);
100 		} else {
101 			static assert(false, "setCustomProperties does not work with '" ~
102 					T.stringof ~ "'");
103 		}
104 	}
105 
106 	void defineName(string name, string formula) {
107 		enforce(workbook_define_name(this.handle, name.toStringz(),
108 						formula.toStringz()
109 					)
110 				== LXW_NO_ERROR
111 			);
112 	}
113 
114 	Worksheet getWorksheetByName(string name) {
115 		return Worksheet(workbook_get_worksheet_by_name(this.handle,
116 						name.toStringz()
117 					)
118 				);
119 	}
120 
121 	Chartsheet getChartByName(string name) {
122 		return Chartsheet(workbook_get_chartsheet_by_name(this.handle,
123 						name.toStringz()
124 					)
125 				);
126 	}
127 
128 	bool validateSheetName(string name) {
129 		return workbook_validate_sheet_name(this.handle, name.toStringz()) 
130 				== LXW_NO_ERROR;
131 	}
132 }