Hi all, I recently posted an excel macro that can quickly format tables for use with Table Export and Recursive Tables and after a great suggestion from keithcurtis I am working my way through making it into an API script. What I have so far I literally just started learning JS this week so my progress might be fairly slow and I still have a lot to read through but so far I have a script that can take an input like this: 01-05 1d6+5 Kobolds and 1d4 bandits 06-10 2d4 Bears 11-20 1 Ettin and turn it into this: !import-table-item --TableName --<%%91%%><%%91%%>1d6+5<%%93%%><%%93%%> Kobolds and <%%91%%><%%91%%>1d4<%%93%%><%%93%%> bandits --5 !import-table-item --TableName --<%%91%%><%%91%%>2d4<%%93%%><%%93%%> Bears --5 !import-table-item --TableName --1 Ettin --10 Code: Below is the code I have so far. I am sure I am making sins related to best practices and such so if anything is offensive to look at please let me know! Disclaimer: The str variable is hardcoded at the moment for my testing purposes but would be intended to receive the chat input. Edit: Cleaned up the code for manipulating table outputs to make better use of regex //updated code 10/21/2020 15:55
var diceRoll = /(\d+d\d+(\+\d)?)/g, lCont = "<%%91%%><%%91%%>", rCont = "<%%93%%><%%93%%>";
var str = "01-05\t1d6+5 Kobolds and a bandit\n06-10\t12d4 Bats and 1d4 Bears\n11-20\t1 Ettin"; //replace with str.content.msg
var column, weight, tableRow = str.split(/\n/);
//Separate each row by \t to separate column 0 and colummn 1
for (var r = 0; r<tableRow.length; r++) {
column = tableRow[r].split(/\t/);
(column[1].match(diceRoll) ? column[1].replace(diceRoll, lCont + "$1" + rCont) : ""); //item output
//Find weight of item
var rangeDice = column[0].split("-");
(rangeDice[1] !== undefined ? weight = parseInt(rangeDice[1]) - parseInt(rangeDice[0]) + 1 : weight = 1); //weight output
}
//Original code
var diceRoll = /[0-9]d[0-9]/, column, weight, lCont = "<%%91%%><%%91%%>", rCont = "<%%93%%><%%93%%>";
var str = "01-05\t1d6+5 Kobolds and 1d4 bandits\n06-10\t2d4 Bears\n11-20\t1 Ettin"; //replace with str.content.msg
var tableRow = str.split(/\n/);
//Separate each row by \t to separate column 0 and colummn 1
for (var r = 0; r<tableRow.length; r++) {
column = tableRow[r].split(/\t/);
//Find weight of item
var rangeDice = column[0].split("-");
(rangeDice[1] !== undefined ? weight = parseInt(rangeDice[1]) - parseInt(rangeDice[0]) + 1 : weight = 1);
//separte string into substrings and identify if substring is a dice roll
var substr = column[1].split(/\s/), i = 0;
for (i = 0; i<substr.length; i++) {
(substr[i].match(diceRoll) ? substr[i] = lCont + substr[i] + rCont : substr[i] = substr[i]);
}
//Output as format used for Table Export and Recursive Tables
console.log("!import-table-item --TableName --" + substr.join(" ") + " --" + weight)
}
The next step I have a basic understanding of the chat message event, but to my knowledge each new line in the chat provokes a new chat event which would make pasting the information into chat invalid as it would parse the first line after a command such as "!convert" and then treat the following lines as regular chat sends. I am looking for a way to take data that pastes as multiple lines into the chat box and have them all be considered a single string separated by tabs and new lines. The end goal Now, in the grand scheme of things I would like to take keithcurtis 's suggestion and make it so that the script can see an event of a handout being opened, then read the handout to check for a table. If a table exists: Check that the table meets the criteria of a rollable table Include an "ADD" button to optionally add the table to the game's tables Include a "ROLL" button in the handout to roll from said table. (Either via the table in-game or directly from the data that was collected) As I mentioned, I still have some work to do on my own to handle the data from the events and such but I certainly welcome any advice along the way. If anyone is willing to provide some insight for this endeavor or just provide general advice for going about a project like this I welcome the input. I aim to learn as much of the process as possible, but I am also willing to admit when something is just too far over my head. Thanks!