$(
function
(){
$.getJSON(gdriveUrl, (data) => {
console.log(data.values);
$(
'#area'
).val(JSON.stringify(data.values));
});
});
var
$files = [];
function
filesSelected(files) {
for
(
var
i = 0; i < files.length; i++) {
var
file = files[i];
$files.push(file);
var
reader =
new
FileReader();
reader.onload =
function
(event){
$(
'#area'
).text(reader.result);
var
data = reader.result;
convert(data);
};
reader.readAsText(file,
'shift-jis'
);
};
}
function
convert(ary){
var
json = {};
var
str_json =
''
;
var
has_key = $(
'input[name="key"]'
).prop(
'checked'
);
var
has_indent = $(
'input[name="indent"]'
).prop(
'checked'
);
json = aryToKeyHash(ary);
if
(has_indent){
str_json = JSON.stringify(json,
null
,
'\t'
);
}
else
{
str_json = JSON.stringify(json);
}
$(
'#reslut'
).text(str_json);
}
function
aryToKeyHash(ary){
var
array = [];
var
keys = ary.shift();
ary.forEach(
function
(cols){
var
line = {};
for
(
var
j=0;j<cols.length;j++){
cols[j] = cols[j].replace(
'\r'
,
''
);
line[keys[j]] = cols[j];
}
array.push(line);
});
console.log(array);
return
array;
}
$(
function
(){
$(
'button'
).on(
'click'
,
function
(e){
var
v = $(
'#area'
).val();
var
ary = JSON.parse(v);
convert(ary);
});
$(
'#area'
).on(
'dragover'
,
function
(e){
e.preventDefault();
e.stopPropagation();
$(
this
).addClass(
'drag'
);
});
$(
'#area'
).on(
'dragleave'
,
function
(e){
e.preventDefault();
e.stopPropagation();
$(
this
).removeClass(
'drag'
);
});
$(
'#area'
).on(
'drop'
,
function
(e){
console.log(e);
e.stopPropagation();
e.preventDefault();
$(
this
).removeClass(
'drag'
);
filesSelected(e.dataTransfer.files);
});
});