# Export Dataview as CSV

**URL:** <https://forum.obsidian.md/t/export-dataview-as-csv/37719>\
**Category:** Help\
**Tags:** dataview\
**Created:** [May 19, 2022, 10:01am UTC](https://forum.obsidian.md/t/export-dataview-as-csv/37719 "2022-05-19T10:01:30Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![epabarker](https://forum.obsidian.md/user_avatar/forum.obsidian.md/epabarker/32/30216_2.png) [@epabarker](https://forum.obsidian.md/u/epabarker)\
**Post date:** [May 26, 2022, 11:20am UTC](https://forum.obsidian.md/t/export-dataview-as-csv/37719/2 "2022-05-26T11:20:08Z")

</div>

Hi @ines.do, I was having a look at your post as well as [Dataview - Exporting Table as CSV](https://forum.obsidian.md/t/dataview-exporting-table-as-csv/29243), 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](https://blacksmithgu.github.io/obsidian-dataview/api/code-reference/#file-io), 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:

```javascript
// ----- 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:

```javascript
// ----- 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();

```

---

_[View the full topic](https://forum.obsidian.md/t/export-dataview-as-csv/37719)._
