# Dataviewjs script for creating markdown table

**URL:** <https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056>\
**Category:** Basement\
**Created:** [June 25, 2021, 12:24am UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056 "2021-06-25T00:24:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![derekvan](https://forum.obsidian.md/user_avatar/forum.obsidian.md/derekvan/32/11293_2.png) [@derekvan](https://forum.obsidian.md/u/derekvan)\
**Post date:** [June 25, 2021, 12:24am UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/1 "2021-06-25T00:24:41Z")

</div>

I’m wondering if it’s possible to create a Dataviewjs script that would be able to output a markdown table, so that the resulting markdown file could be run through Pandoc and styled easily in a word processor without a lot of CSS tweaking.

It looks like @Christian has a good approach in his script [which puts a “copy” button at the top of a dataviewjs table which then copies the list of file names to the clipboard](https://github.com/chhoumann/Templater_Templates/blob/master/dataview/copy_links_in_query.md). It’s way beyond my scripting capabilities to customize the button part of that script to produce a markdown table (although I was able to script my own query to replace his example), but I wonder if it’s possible–this approach seems like it might be useful to others.

My use case here is creating a syllabus as a teacher. I have grouped my tables by “Weeks”, then sorted by “days” to end up with this:

 ![Screen Shot 2021-06-24 at 8.22.11 PM](https://forum.obsidian.md/uploads/default/original/3X/5/2/525653b742e2d81b32ce70035dc3e3f3817f0189.png)

Pretty good! But now the question is getting this into a form to share with students. I can export to PDF, but it’s not quite right and I’d rather tweak in a word processor than via CSS. I’m pretty familiar with using a docx reference file in Pandoc, so that’s my thinking right now, although I’d certainly be interested in other folks’ ideas along these lines.

---

<div class="post-metadata">

**Author:** ![tech\_monk](https://forum.obsidian.md/user_avatar/forum.obsidian.md/tech_monk/32/13824_2.png) [@tech\_monk](https://forum.obsidian.md/u/tech_monk)\
**Post date:** [June 25, 2021, 6:04pm UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/2 "2021-06-25T18:04:04Z")

</div>

My non-scripting workaround for what you’re describing:

- Install Typora and set it as the “Default App” in Obsidian
- Open the note in Obsidian and then choose “Open in Default App” from the ‘three dot menu.’
- Note opens in Typora
- Export note from Typora in whatever format you need. If you have pandoc installed and on your path, the export options include .doc, .rtf, and several others.
- Open in your word processor, tweak the doc, and publish.

This is a bit more effort than running a script, but only marginally so. And the results are consistently good because, well, pandoc.

---

<div class="post-metadata">

**Author:** ![derekvan](https://forum.obsidian.md/user_avatar/forum.obsidian.md/derekvan/32/11293_2.png) [@derekvan](https://forum.obsidian.md/u/derekvan)\
**Post date:** [June 25, 2021, 6:56pm UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/3 "2021-06-25T18:56:13Z")

</div>

But typora won’t understand the dataview table, right?

---

<div class="post-metadata">

**Author:** ![Christian](https://forum.obsidian.md/user_avatar/forum.obsidian.md/christian/32/10431_2.png) [@Christian](https://forum.obsidian.md/u/Christian)\
**Post date:** [June 25, 2021, 9:31pm UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/4 "2021-06-25T21:31:16Z")

</div>

Hey @derekvan. I took a look at it, and this is what I came up with.

I had to try to replicate your pages and table as much as possible. Maybe I did an OK job of it, but I am not sure.

I tried to make it at least kind of generic, such that others may benefit from it as well.

 ![UOns6AL0XW](https://forum.obsidian.md/uploads/default/original/3X/7/4/74070a13dab4a2e408c53ef4e63e716b9ed8e92f.gif)

> <https://github.com/chhoumann/Templater_Templates/blob/master/dataview/make_markdown_table_from_dataview_table.md>

---

<div class="post-metadata">

**Author:** ![derekvan](https://forum.obsidian.md/user_avatar/forum.obsidian.md/derekvan/32/11293_2.png) [@derekvan](https://forum.obsidian.md/u/derekvan)\
**Post date:** [June 25, 2021, 11:21pm UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/5 "2021-06-25T23:21:12Z")

</div>

> [@Christian](#):
>
> I tried to make it at least kind of generic, such that others may benefit from it as well.

This looks really great! I can’t wait to tinker with it tomorrow. Very generous of you.

---

<div class="post-metadata">

**Author:** ![derekvan](https://forum.obsidian.md/user_avatar/forum.obsidian.md/derekvan/32/11293_2.png) [@derekvan](https://forum.obsidian.md/u/derekvan)\
**Post date:** [June 25, 2021, 11:41pm UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/6 "2021-06-25T23:41:22Z")

</div>

Ok @Christian , I had a chance to tinker already and it works! Thanks. However, one question. How can I change the query to look in a particular folder instead? I’d like to have something like this, but it’s throwing an error:

`const pages = dv.pages('"teaching/65011 - Reading & Interpreting"');`  
instead of  
`const pages = dv.pages().where(f => f.theme && f.readings);`

---

<div class="post-metadata">

**Author:** ![Christian](https://forum.obsidian.md/user_avatar/forum.obsidian.md/christian/32/10431_2.png) [@Christian](https://forum.obsidian.md/u/Christian)\
**Post date:** [June 25, 2021, 11:53pm UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/7 "2021-06-25T23:53:59Z")

</div>

Good to hear. Sure thing.

Can you tell me which error is occurring?

Edit: `const pages = dv.pages('"teaching/65011 - Teaching and Interpreting"').where(f => f.theme && f.readings);` seems to work fine for me. Even without the `where`.

---

<div class="post-metadata">

**Author:** ![derekvan](https://forum.obsidian.md/user_avatar/forum.obsidian.md/derekvan/32/11293_2.png) [@derekvan](https://forum.obsidian.md/u/derekvan)\
**Post date:** [June 26, 2021, 3:48am UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/8 "2021-06-26T03:48:53Z")

</div>

Thanks so much for your help with this.

This line produces a table:

```
const pages = dv.pages('"teaching/65011 - Reading & Interpreting"').where(f => f.theme && f.readings)

```

But this line gives an error:

```
const pages = dv.pages('"teaching/65011 - Reading & Interpreting"')

```

Error:

`Evaluation Error:  
map@[native code]

map@[native code]

forEach@[native code]  
eval code  
eval@[native code]

generatorResume@[native code]

Promise@[native code]  
\_\_awaiter

generatorResume@[native code]

Promise@[native code]  
\_\_awaiter  
capacitor://localhost/app.js:1:757698  
capacitor://localhost/app.js:1:758120

generatorResume@[native code]  
fulfilled  
promiseReactionJob@[native code]`

---

<div class="post-metadata">

**Author:** ![derekvan](https://forum.obsidian.md/user_avatar/forum.obsidian.md/derekvan/32/11293_2.png) [@derekvan](https://forum.obsidian.md/u/derekvan)\
**Post date:** [June 26, 2021, 3:49am UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/9 "2021-06-26T03:49:44Z")

</div>

Actually, this is probably my fault. There are files in that folder that don’t have proper YAML fields yet …

---

<div class="post-metadata">

**Author:** ![Christian](https://forum.obsidian.md/user_avatar/forum.obsidian.md/christian/32/10431_2.png) [@Christian](https://forum.obsidian.md/u/Christian)\
**Post date:** [June 26, 2021, 10:54am UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/10 "2021-06-26T10:54:36Z")

</div>

In the code, it’s assumed that every page in `pages` has a `day`, `week`, `theme`, and `readings` property. So if, like you mentioned, a page is incomplete in its properties, the code fails.

I used `const pages = dv.pages('"teaching/65011 - Reading & Interpreting"').where(f => f.theme && f.readings)` to make sure that every page found has both the `theme` and `readings` properties - and even that may fail in the case that those pages don’t have a `day` and `week` property.

This would be ‘more correct’.

```auto
const pages = dv.pages('"teaching/65011 - Reading & Interpreting"').where(f => f.theme && f.readings && f.day && f.week)

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![system](https://forum.obsidian.md/uploads/default/original/3X/b/e/bec35fbd23ab28909bb6bfe62cb32eb82dd34019.png) [@system](https://forum.obsidian.md/u/system)\
**Post date:** [June 27, 2021, 10:54am UTC](https://forum.obsidian.md/t/dataviewjs-script-for-creating-markdown-table/20056/11 "2021-06-27T10:54:55Z")

</div>

Closed on June 27, 2021, 10:54am UTC

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
