Skip to content

Commit 03d1a37

Browse files
committed
add csv/json import/export for sheets
1 parent fdfb235 commit 03d1a37

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed

castle.hxml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
-lib hx3compat:git:https://github.com/HaxeFoundation/hx3compat
66
-lib format:git:https://github.com/HaxeFoundation/format
77
-lib hxbit:git:https://github.com/HeapsIO/hxbit
8+
-lib thx.csv
89
-D lz4js
910
-D analyzer-optimize
1011
-dce std

src/Main.hx

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,26 @@ class Main extends Model {
918918
var ndel = new MenuItem( { label : "Delete" } );
919919
var nindex = new MenuItem( { label : "Add Index", type : MenuItemType.checkbox } );
920920
var ngroup = new MenuItem( { label : "Add Group", type : MenuItemType.checkbox } );
921-
for( m in [nins, nleft, nright, nren, ndel, nindex, ngroup] )
921+
922+
var ncsv = new MenuItem( { label : "CSV..." } );
923+
var csv = new Menu();
924+
var importSheetCSV = new MenuItem( { label : "Import...", type : MenuItemType.checkbox } );
925+
var exportSheetCSV = new MenuItem( { label : "Export...", type : MenuItemType.checkbox } );
926+
csv.append(importSheetCSV);
927+
csv.append(exportSheetCSV);
928+
ncsv.submenu = csv;
929+
930+
var njson = new MenuItem( { label : "JSON..." } );
931+
var json = new Menu();
932+
var importSheetJSON = new MenuItem( { label : "Import...", type : MenuItemType.checkbox } );
933+
var exportSheetJSON = new MenuItem( { label : "Export...", type : MenuItemType.checkbox } );
934+
json.append(importSheetJSON);
935+
json.append(exportSheetJSON);
936+
njson.submenu = json;
937+
938+
for( m in [nins, nleft, nright, nren, ndel, nindex, ngroup, njson, ncsv] )
922939
n.append(m);
940+
923941
nleft.click = function() {
924942
var prev = -1;
925943
for( i in 0...base.sheets.length ) {
@@ -1005,6 +1023,49 @@ class Main extends Model {
10051023
nren.click = function() {
10061024
li.dblclick();
10071025
};
1026+
1027+
importSheetJSON.click = function() {
1028+
var i = J("<input>").attr("type", "file").css("display","none").change(function(e) {
1029+
var j = JTHIS;
1030+
this.importSheetJSON(s, j.val());
1031+
initContent();
1032+
j.remove();
1033+
});
1034+
i.appendTo(J("body"));
1035+
i.click();
1036+
};
1037+
exportSheetJSON.click = function() {
1038+
var i = J("<input>").attr("type", "file").attr("nwsaveas",'${s.name}.json').css("display","none").change(function(e) {
1039+
var j = JTHIS;
1040+
this.exportSheetJSON(s, j.val());
1041+
initContent();
1042+
j.remove();
1043+
});
1044+
i.appendTo(J("body"));
1045+
i.click();
1046+
};
1047+
1048+
importSheetCSV.click = function() {
1049+
var i = J("<input>").attr("type", "file").css("display","none").change(function(e) {
1050+
var j = JTHIS;
1051+
this.importSheetCSV(s, j.val());
1052+
initContent();
1053+
j.remove();
1054+
});
1055+
i.appendTo(J("body"));
1056+
i.click();
1057+
};
1058+
exportSheetCSV.click = function() {
1059+
var i = J("<input>").attr("type", "file").attr("nwsaveas",'${s.name}.csv').css("display","none").change(function(e) {
1060+
var j = JTHIS;
1061+
this.exportSheetCSV(s, j.val());
1062+
initContent();
1063+
j.remove();
1064+
});
1065+
i.appendTo(J("body"));
1066+
i.click();
1067+
};
1068+
10081069
if( s.isLevel() || (s.hasColumn("width", [TInt]) && s.hasColumn("height", [TInt]) && s.hasColumn("props",[TDynamic])) ) {
10091070
var nlevel = new MenuItem( { label : "Level", type : MenuItemType.checkbox } );
10101071
nlevel.checked = s.isLevel();

src/Model.hx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
import cdb.Data;
1717

18+
import thx.csv.*;
19+
1820
typedef Prefs = {
1921
windowPos : { x : Int, y : Int, w : Int, h : Int, max : Bool },
2022
curFile : String,
@@ -189,4 +191,76 @@ class Model {
189191
js.Browser.getLocalStorage().setItem("prefs", haxe.Serializer.run(prefs));
190192
}
191193

194+
@:access(cdb.Sheet)
195+
function exportSheetJSON(_s:cdb.Sheet, _filePath:String) {
196+
sys.io.File.saveContent(_filePath, haxe.Json.stringify(_s.sheet, null, " "));
197+
}
198+
199+
@:access(cdb.Sheet)
200+
function importSheetJSON(_s:cdb.Sheet, _filePath:String) {
201+
trace(_s);
202+
203+
// TODO: do import validation!!!
204+
var s = haxe.Json.parse(sys.io.File.getContent(_filePath));
205+
for( c in cast(s.columns, Array<Dynamic>) ) {
206+
c.type = cdb.Parser.getType(c.typeStr);
207+
c.typeStr = null;
208+
}
209+
210+
_s.sheet = s;
211+
_s.sync();
212+
_s.base.updateSheets();
213+
_s.base.sync();
214+
}
215+
216+
function exportSheetCSV(_s:cdb.Sheet, _filePath:String) {
217+
var lines = [[ for (c in _s.columns) c.name]];
218+
219+
for (l in _s.getLines()) {
220+
var ol = [];
221+
for (c in _s.columns) {
222+
switch (c.type) {
223+
case TId, TString, TFile:
224+
ol.push(Reflect.field(l, c.name));
225+
default:
226+
ol.push(haxe.Json.stringify(Reflect.field(l, c.name)));
227+
}
228+
}
229+
lines.push(ol);
230+
}
231+
sys.io.File.saveContent(_filePath, Csv.encode(lines));
232+
}
233+
234+
@:access(cdb.Sheet)
235+
function importSheetCSV(_s:cdb.Sheet, _filePath:String) {
236+
var d = Csv.decode(sys.io.File.getContent(_filePath));
237+
var header = d.shift(); // get rid off first line
238+
239+
var colMapSheet = new Map<String, Column>();
240+
for (c in _s.columns)
241+
colMapSheet.set(c.name, c);
242+
243+
var lines = [];
244+
for (l in d) {
245+
var line = {};
246+
for (i in 0...l.length) {
247+
var c = colMapSheet.get(header[i]);
248+
if (c == null)
249+
continue;
250+
251+
var val = l[i];
252+
switch (c.type) {
253+
case TId, TString, TFile:
254+
Reflect.setField(line, c.name, val);
255+
default:
256+
Reflect.setField(line, c.name, haxe.Json.parse(val));
257+
}
258+
}
259+
lines.push(line);
260+
}
261+
262+
_s.sheet.lines = lines;
263+
_s.sync();
264+
}
265+
192266
}

0 commit comments

Comments
 (0)