Excel to BBCode / SMF
Convert your MS-Excel table into BBCode or Simple Machine Forum (SMF) Code
HTML
<body>
Copy from excel, paste here. First line is assumed to contain column names.
<textarea id="tsv_input"></textarea>
<br>
<button id="button_convert" type="button">convert to BB</button>
<br>
<textarea id="output_bb"></textarea>
</body>
CSS
* { font-family: Courier; font-size: 10pt; }
textarea { width: 700pt; height: 200pt; }
JavaScript
// ref 2013 : http://javascript.crockford.com/remedial.html
// small hack, return empty string when value is not number or string
if (!String.prototype.supplant) {
String.prototype.supplant = function (o) {
return this.replace(
/\{([^{}]*)\}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : "";
}
);
};
}
var getColumnNames = function (tsv) {
var matches = /[^\n\r]+/.exec(tsv),
firstLine = matches[0] || "";
return firstLine.split("\t");
};
var getData = function (tsv, columnNames, conversionRules) {
// assumed is
// - that the first line contains column names
// - that the column names are suitable for json
//
// columns that have no column name are ignored
var items = [],
tempLines = tsv.split(/\r?\n/),
dataLines = tempLines.splice(1),
columnNames = columnNames || getColumnNames(tsv);
// for later...
conversionRules = conversionRules || {};
$.each(dataLines, function (index, line) {
var parts = line.split("\t"),
item = {};
$.each(columnNames, function(index, columnName) {
var conversionRule = conversionRules.hasOwnProperty(columnName)
? conversionRules[columnName]
: null;
item[columnName] = conversionRule === null
? parts[index]
: conversionRule(parts[index]);
});
items.push(item);
});
return items;
};
var jsonToBB = function (columnNames, data)
{
var bbTemplates = {
table : "[tstyle][/tstyle][table]{allRowsBB}[/table]",
row : "\t[tr]{cellsBB}[/tr]",
header : "[th]{name}[/th]",
cell : "[td]{value}[/td]"
},
allRowsBB = [],
headerBBParts = [];
$.each(columnNames, function(index, columName) {
headerBBParts.push(bbTemplates.header.supplant({ name : columName }));
});
allRowsBB.push(bbTemplates.row.supplant({ "cellsBB" :...