demondownload

Import macro

Feb 8th, 2023 (edited)
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.22 KB | Source Code | 0 0
  1. const parseFile = html => {
  2.   const file = html.find('#file')[0].files[0]
  3.   const tableId = html.find('#table-id')[0].value
  4.  
  5.   var reader = new FileReader();
  6.   reader.onload = (e) => {
  7.     const contents = e.target.result
  8.     importContents(contents, tableId)
  9.   }
  10.   reader.readAsText(file)
  11. }
  12.  
  13. const importContents = (contents, tableId) => {
  14.   const table = game.tables.get(tableId)
  15.   const entries = contents.split('\n').map(line => {
  16.     return {
  17.       type: 0, text: line, range: [1,2], weight: 1
  18.     }
  19.   })
  20.   table.createEmbeddedDocuments("TableResult", entries)
  21.   table.normalize()
  22. }
  23.  
  24. await new Promise(resolve => {
  25.   new Dialog({
  26.     title: 'Import CSV File to RollTable',
  27.     content: '<form><div class="form-group"><label style="max-width:100px">File</label><div class="form-fields"><input id="file" type="file"/></div></div><div class="form-group"><label style="max-width:100px">RollTable ID</label><div class="form-fields"><input id="table-id" type="string"></div></div></form>',
  28.     buttons: {
  29.       roll: {
  30.         icon: '<i class="fas fa-file-import"></i>',
  31.         label: "Import",
  32.         callback: html => resolve(parseFile(html))
  33.       },
  34.     },
  35.     close: () => resolve(null)
  36. }).render(true);})
Tags: foundry
Advertisement
Add Comment
Please, Sign In to add comment