Skip to content

Instantly share code, notes, and snippets.

@omerida
Created October 18, 2022 15:33
Show Gist options
  • Save omerida/11fd362ccbf2e9fde5a40b1acd15fe9e to your computer and use it in GitHub Desktop.
Save omerida/11fd362ccbf2e9fde5a40b1acd15fe9e to your computer and use it in GitHub Desktop.
Foundry Macro: Text to RollTable Ma
function makeTable(tableName, inputRows) {
let rows = inputRows.split(/\r?\n/);
// prepare table rows
let results = rows.map((row, count) => {
return {
text: row.trim(),
type: CONST.TABLE_RESULT_TYPES.TEXT,
weight: 1,
range: [count + 1, count + 1],
drawn: false
}
});
let table = game.tables.getName(tableName);
if (table){
// update an existing table?
table.createEmbeddedDocuments("TableResult", results);
table.formula = "1d" + results.length;
} else {
// create a new table
table = RollTable.createDocuments([{
name: tableName,
description: '',
results: results,
formula: "1d" + results.length,
replacement: true,
}]);
}
}
let content = `<form>
<label>Rows</label>
<textarea rows=8 cols=80 name="inputRows"></textarea>
<p><em>Each line becomes a new result</em></p><br>
<label>Table Name</label>
<input type="text" name="tableName"><br><br>
<p><em>New Table to create. If name exists, nothing happens?</em></p>
</form>`
new Dialog({
title: `Text to RollTable`,
content: content,
buttons: {
yes: {
icon: "<i class='fas fa-check'></i>",
label: "Create Table",
callback: (html) => {
let tableName = html.find("input[name='tableName']").val();
let inputRows = html.find("textarea[name='inputRows']").val();
makeTable(tableName, inputRows);
}
},
no: {
icon: "<i class='fas fa-times'></i>",
label: 'Cancel'
}
},
}).render(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment