Export Dataview as CSV

Hi, I really need to export my dataview to excel. Is there a way to export as csv? I saw a question like this (below) but no replays.

Thanks for clues!

Continuing the discussion from Dataview - Exporting Table as CSV:

Things I have tried

What I’m trying to do

5 Likes

Hi @ines.do, I was having a look at your post as well as Dataview - Exporting Table as CSV, and trying to look through Dataview documentation to see if this is something that is in the API.

There is mention of loading a csv, but I couldn’t see an option for saving.

I tried to come up with a general purpose route for:

Load relevant notes → grab relevant columns → export to csv

My rudimentary non-JS brain pieced together this:

// ----- Gather data and specify table columns -----
// Grab all relevant notes/pages
let projects = dv.pages("#projects");
// Grab the columns you want using .map function
const projects_tbl = projects.map(p => [p.file.link,p.status,p.domain]);
// Map function traverses each row and joins elements with ",", then we join the array of strings with "\n"
let csvContent = "data:text/csv;charset=utf-8," + projects_tbl.map(e => e.join(",")).join("\n");

// ------ Create CSV file ------
// Create a hidden <a> DOM node and set its download attribute as follows, where "my_data.csv" will be the name of your csv file
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download","my_data.csv");
document.body.appendChild(link);
link.click();

I would use this at your discretion, as it uses some functions I am unaware of, but suffice to say I’ve tested this and I got a table just as I wanted, as a csv file. Once this code has run, it will automatically bring up a window to save the file, and bear in mind that this will keep running if you have set dataview queries to re-evaluate in Live-Preview. So, I would run it as a script perhaps?

Would love some extra suggestions from either yourself or others to improve upon this.

Generalisable version here:

// ----- Gather data and specify table columns -----
// Grab all relevant notes/pages
let <name_of_pages> = dv.pages("<source>");
// Grab the columns you want using .map function
const <table_name> = <name_of_pages>.map(p => [p.<field1>,p.<field2>,p.<field3>]);
// Map function traverses each row and joins elements with ",", then we join the array of strings with "\n"
let csvContent = "data:text/csv;charset=utf-8," + <table_name>.map(e => e.join(",")).join("\n");

// ------ Create CSV file ------
// Create a hidden <a> DOM node and set its download attribute as follows, where "my_data.csv" will be the name of your csv file
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download","<csv_file_name>.csv");
document.body.appendChild(link);
link.click();

I‘ve just found a perfect solution to solve this problem of CSV table export. With the new community plugin „Table to CSV Exporter“ from Stefan Wolfrum @metawops at
GitHub - metawops/obsidian-table-to-csv-export: An Obsidian Plugin that allows to export tables from a pane in reading mode to CSV files. It‘s all what you‘ll need. Here‘s my first test with three CSV exports to the top folder of my vault:

3 Likes

Thanks a lot, @Edmund! :heart:

You should definitely post some Zettelkasten analytics visualisations here, too! At least, this was one reason why I wrote that plugin: to be able to export metadata about the notes in our vaults and visualise them in tools like MS Power BI … :wink:

Oh, and in case somebody doesn’t see the exported CSV files in Obsidian’s File Explorer: make sure that the switch “Detect all file extensions” in the “Files & Links” section of the settings is switched on. (In case you want to see them in your File Explorer.)

2 Likes

I’m not so sure about CSV, but the new ability to render and manipulate Markdown from Dataview seems very promising!

Good luck!

2 Likes

@ines.do Can my table to CSV export plugin help in your case? Or would you need something different? Is there anything I could add to my plugin to include your use case? :flushed:

Hi, the “Table to CSV Exporter” has been very helpful for me, it’s great! Hower, I am using diacritics in my data, which aren’t displayed correctly after exporting the tables to csv. Is there a way to export them to csv utf-8? Maybe I should try out the code suggested by @epabarker above but using the plugin looks way easier.

1 Like