In addition to the methods mentioned above, another approach is to register a function in customJS
to process CSV files.
(system/script/csv.js
)
class CSV {
async load(path) {
const file = app.vault.getAbstractFileByPath(path);
if (!file) {
console.error('File not found:', path);
return [];
}
const content = await app.vault.read(file);
const lines = content.split('\n').filter(line => line.trim() !== '');
const rows = lines.map(line => line.split(',').map(cell => cell.trim()));
if (rows.length < 2) {
console.warn('CSV has insufficient rows:', path);
return [];
}
const headers = rows[0].map(CSV.toCamelCase);
const data = rows
.slice(1)
.map(row => Object.fromEntries(headers.map((key, i) => [key, row[i] || ''])));
return data;
}
static toCamelCase(str) {
return str
.toLowerCase()
.replace(/[^a-zA-Z0-9 ]/g, '')
.replace(/ ([a-z])/g, (_, letter) => letter.toUpperCase())
.replace(/^([A-Z])/, (_, letter) => letter.toLowerCase());
}
}
(Usage example)
const { CSV } = await cJS()
const data = await CSV.load('path-to-csv.csv')
Hope this helps!