Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Reading files through the api for more fancy rollable tables

Is there any api method that will let me open a file dialog box which can then be parsed in my code? I basically wanted to have a rollable table which lets me show the name as well as the description of the entry. Kinda based on the discussion here -  Community Forums: Rollable tables with description tied to the result? | Roll20: Online virtual tabletop  I decided to make it through the use of powercards and macros. I have an xlsx file which has one table in every sheet. I wrote some code in python which takes the table from excel and then spits out a text file which you can copy and paste into your macro. The powercard has conditionals for every row of the table and it is not limited as to how much information can be displayed about one item. I wanted to be able to do all this directly through the API. Basically when I call the API script, it opens an upload file dialog box where you can upload a csv file and then it can create a powercard and a macro which runs said powercard. Will this be possible? I don't like the idea of running half my code elsewhere and then uploading stuff on roll20 manually (although I automated that by pyautogui too, it's still clunky). I just wanted all my tables from dmg in one place, easily callable.
1649686120
The Aaron
Roll20 Production Team
API Scripter
Not possible.  The API Sandbox doesn't run in your browser, it runs on a server in the cloud. Even if it could open files (it can't) it couldn't open your files.  Generally we API scriptors handle this problem be either generating code that creates the data structure we want and passes it to our script, or we create api commands to pass the data via the chat, or we read the data from a Roll20 object, like the GM notes on a token. 
So I would have to either have the tables inside my script or I have to copy and paste the csv into the chat via a command I create or have all the data on a token instead of csv/xls?
1649686868

Edited 1649686901
The Aaron
Roll20 Production Team
API Scripter
You could also have your script define an import function, and call that from a script you generate as a wrapper of the data. That's what the ShapedCompanion script does for importing home brew data. 
1649687314

Edited 1649687712
Riddhi M.
API Scripter
I am really sorry I didn't understand that. Would it be possible for you to share some pseudocode/relevant part of the code from ShapedCompanion (if and when you have the time)? This would be my first attempt ever at developing the api
1649689133

Edited 1649689356
The Aaron
Roll20 Production Team
API Scripter
No problem.  Here's a very small exmaple: script: const ImporterSample = (()=>{ const HE = (() => { const esRE = (s) => s.replace(/(\\|\/|\[|\]|\(|\)|\{|\}|\?|\+|\*|\||\.|\^|\$)/g,'\\$1'); const e = (s) => `&${s};`; const entities = { '<' : e('lt'), '>' : e('gt'), "'" : e('#39'), '@' : e('#64'), '{' : e('#123'), '|' : e('#124'), '}' : e('#125'), '[' : e('#91'), ']' : e('#93'), '"' : e('quot') }; const re = new RegExp(`(${Object.keys(entities).map(esRE).join('|')})`,'g'); return (s) => s.replace(re, (c) => (entities[c] || c) ); })(); const importData = (d) => { sendChat('',`Imported data: ${HE(JSON.stringify(d))}`); }; return { import: importData }; })(); Import script 1: on('ready',()=>{ ImporterSample.import({ foo: 'bar', baz: 33 }); }); Import script 2: on('ready',()=>{ setTimeout(()=>ImporterSample.import({ foo: 'tacos', baz: 42 }), 2000); }); I bolded the important parts above.  In the script, it creates a function to take the imported data.  It exposes the import function via the Revealing Module Pattern, then the two scripts call that function to pass the data to the script.
Oh okay. Now I get it. Thanks!