Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Generated by LiveScript 1.2.0
(function(){
var XLSX, sheet_to_socialcalc, wb;
XLSX = require('xlsx');
sheet_to_socialcalc = function(){
var header, sep, meta, end, scencode, scsave;
header = "socialcalc:version:1.5\nMIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=SocialCalcSpreadsheetControlSave";
sep = "--SocialCalcSpreadsheetControlSave\nContent-type: text/plain; charset=UTF-8";
meta = "# SocialCalc Spreadsheet Control Save\npart:sheet";
end = "--SocialCalcSpreadsheetControlSave--";
scencode = function(it){
return it.replace(/\\/g, "\\b").replace(/:/g, "\\c").replace(/\n/g, "\\n");
};
scsave = function(ws){
var o, oo, r, i$, to$, R, j$, to1$, C, coord, cell;
if (!(ws != null && ws['!ref'])) {
return;
}
o = [];
oo = [];
r = XLSX.utils.decode_range(ws['!ref']);
for (i$ = r.s.r, to$ = r.e.r; i$ <= to$; ++i$) {
R = i$;
for (j$ = r.s.c, to1$ = r.e.c; j$ <= to1$; ++j$) {
C = j$;
coord = XLSX.utils.encode_cell({
r: R,
c: C
});
cell = ws[coord];
console.log(cell);
if ((cell != null ? cell.v : void 8) == null) {
continue;
}
oo = ["cell", coord, 't'];
switch (cell.t) {
case 's':
case 'str':
oo.push(scencode(cell.v));
break;
case 'n':
if (cell.f) {
oo[2] = 'vtf';
oo.push('n');
oo.push(cell.v);
oo.push(scencode(cell.f));
} else {
oo[2] = 'v';
oo.push(cell.v);
}
}
o.push(oo.join(":"));
o.push("sheet:c:" + (r.e.c - r.s.c + 1) + ":r:" + (r.e.r - r.s.r + 1) + ":tvf:1");
o.push("valueformat:1:text-wiki");
o.push("copiedfrom:" + ws['!ref']);
}
}
return o.join("\n");
};
return function(ws, opts){
return [header, sep, meta, sep, scsave(ws), end].join("\n");
};
}();
if (/(?:lsc|node(?:js)?)$/.exec(process.argv[0])) {
process.argv.shift();
}
if (process.argv.length > 1) {
wb = XLSX.readFile(process.argv[1]);
console.log(sheet_to_socialcalc(wb.Sheets[wb.SheetNames[0]]));
} else {
console.log("Please specify a .xlsx file as argument.");
}
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment