Public
Edited
Oct 3, 2024
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
resetToDefaults = function(csvtext2, csvdelim=",", mustHaveField="", varName="csvdata") {
csv;
delimator;
rosarr;
jsarr;
csvMustHaveField;
rosVarName;
(viewof csvMustHaveField).value = mustHaveField;
(viewof csvMustHaveField).dispatchEvent(new Event('input', { bubbles: true }));
(viewof csv).value = csvtext2;
(viewof csv).dispatchEvent(new Event('input', { bubbles: true }));
(viewof delimator).value = csvdelim;
(viewof delimator).dispatchEvent(new Event('input', { bubbles: true }));
(viewof rosVarName).value = varName;
(viewof rosVarName).dispatchEvent(new Event('input', { bubbles: true }));
}
Insert cell
/* TEST ONLY - will cause undeserable side-effects if changes are made while NOT commented out below: */
// resetToDefaults(countriesCsv, ',', "", "countries")
Insert cell
// create a custom "CSV" parser based on the delimator selected (e.g. could be TSV or "semi-colon"-seperated-values)
semicsv = d3.dsvFormat(delimator)
Insert cell
// first we need to parse the data, using the "D3" DSV (aka a generic CSV) parser
jsarr = semicsv.parse(csv, csvFilter)
Insert cell
csvFilter = line => line[csvMustHaveField] != "" ? line : (csvMustHaveField == "" ? line : null)
Insert cell
Insert cell
// the core of the parsing is done here – essentially enumerating the parsed CSV/JS array to create script code to re-create the same array under RouterOS
rosarr = (`:global ${rosVarName} [:toarray ""];\n` + jsarr.map(line => {
let rv = `:set ${rosVarName} ($${rosVarName}, { {`
for (let k in line) {
//line[k].replace(/"/g,"'")
//k = k.replace(/"/g, "'")
rv += `"${k}"="${line[k]}";`
}
rv += "} })\n"
return rv
})).replace(/^,:/gm,":")
Insert cell
// an UNUSED alternative implementation that creates RouterOS "map array" with the outer array being index by one of the columns instead of numerically index
rosarrByIndex = (columnName) => (":global rosarr [:toarray \"\"];\n" + jsarr.map(line => {
let rv = `:set rosarr ($rosarr, { "${line[columnName]}"={`
for (let k in line) {
rv += `"${k}"="${line[k]}";`
}
rv += "} })\n"
return rv
})).replace(/,:/g,":")
Insert cell
sampleCsvSimple = `column1,column2
a1,a2
b1,b2`
Insert cell
Insert cell
Insert cell
Insert cell
// observablehq.com already has XLSX parsing built-in
// xlsx = require('https://bundle.run/xlsx@0.14.1')
Insert cell
table2rsc = (name, table) =>
(`:global ${name} [:toarray ""];\n` + table.map(line => {
let rv = `:set ${name} ($${name}, { {`
for (let k in line) {
//line[k].replace(/"/g,"'")
//k = k.replace(/"/g, "'")
rv += `"${k}"="${line[k]}";`
}
rv += "} })\n"
return rv
})).replace(/^,:/gm,":")
Insert cell
import {icon} from '@severo/fork-awesome-icons'
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more