#Spreadsheet to journal entry table
1 messages ยท Page 1 of 1 (latest)
Okay, so you can create a new journal with JournalEntry.create
e.g.
JournalEntry.create({
name: "Test",
content: "<h1>hi</h1>",
});
All you need to do now is convert your data into HTML to put into content
There's a few ways to do that, but one easy way is with Handlebars since FVTT already uses that.
Here's an example of a template:
<table>
<thead>
<tr>
{{#each skills.[0]}}
<th>{{@key}}</th>
{{/each}}
</tr>
</thead>
<tbody>
{{#each skills}}
<tr>
{{#each this}}
<td>{{this}}</td>
{{/each}}
</tr>
{/each}}
</tbody>
</table>
thanks, I'll give it a try and see if I can manage to apply this ๐
You would use that by calling Handlebars.compile on it and then passing it in your skills
JournalEntry.create({
name: "Test",
content: Handlebars.compile("")({ skills }),
});
Put the template between the quotes there
I think you can use <td>[[/r 1d20+{{this.Level}}]]</td> for the d20
oh btw stupid question, but would this update whenever the values update? (provided I include that in the module at a later stage)
Nope. You'd have to have a different script that runs game.journal.getName("Test").update({ content: Handlebars.compile("")({ skills }) });
alright ๐
sorry to keep asking, but apparently I dont quite understand.
if I enter
name: "Test",
content: Handlebars.compile("")({ skills }),
});```
I get skills not defined
Also, put the template above inside those quotes
const template = `<table>
<thead>
<tr>
{{#each skills.[0]}}
<th>{{@key}}</th>
{{/each}}
</tr>
</thead>
<tbody>
{{#each skills}}
<tr>
{{#each this}}
<td>{{this}}</td>
{{/each}}
</tr>
{{/each}}
</tbody>
</table>`;
JournalEntry.create({
name: "Test",
content: Handlebars.compile(template)({ skills }),
});```
Thanks, for some reasone this produces an error in handlebars.min.js o.O
const skills = values.skills
console.log(skills); // all Skills
console.log(skills[0]); // {"Skills": "Acrobatics","": "","Level": "12"}
console.log(skills[0].Skills); // Acrobatics
console.log(skills[0].Level); // 12
const template = `<table>
<thead>
<tr>
{{#each skills.[0]}}
<th>{{@key}}</th>
{{/each}}
</tr>
</thead>
<tbody>
{{#each skills}}
<tr>
{{#each this}}
<td>{{this}}</td>
{{/each}}
</tr>
{/each}}
</tbody>
</table>`;
JournalEntry.create({
name: "Test",
content: Handlebars.compile(template)({ skills }),
});
});```
handlebars.min.js:28 Uncaught (in promise) Error: Parse error on line 18:
...}}</tbody></table>
---------------------^
Expecting 'OPEN_INVERSE_CHAIN', 'INVERSE', 'OPEN_ENDBLOCK', got 'EOF'
[Detected 1 package: autosheet-importer]
at a.parseError (handlebars.min.js:28)
at a.parse (handlebars.min.js:28)
at d (handlebars.min.js:27)
at d.e [as parse] (handlebars.min.js:27)
at d (handlebars.min.js:28)
at e (handlebars.min.js:28)
at AutosheetImporter.mjs:77
There's a typo in that
I edited my message
that fixed it. Wondering what it was ^^
thanks so much ๐
Nice ๐ฏ
(Please tell me if I start getting on your nerves with my questions. I am very grateful for all the help you provided!) So currently the whole module hooks on init. I was thinking that it maybe an easy way to keep updating the data if I replaced the hook event with something that commonly triggers but couldn't find anything suitable. Is there a way to trigger a function e.g. once a second?
Or is there a smarter way that only refreshes the data when changes where made in the sheet?
Nvm I think this time I got it on my own :D
setInterval(function () { getData(); }, 1000);
getData();
suprise, I failed ^^
{
const response = await fetch(
`https://sheets.googleapis.com/v4/spreadsheets/${ID}/values/${RANGE}?majorDimension=ROWS&key=${API_KEY}`
);
const { values } = await response.json();
game.modules.get(MODULE_ID).api = values;
function cell(x, y)
{
const content = values[y-1][letterToColumn(x)-1];
return content;
}
values.playernames = [cell("B", 2), cell("O", 2), cell("AB", 2), cell("AO", 2)];
console.log(values.playernames);
for (let i=0; i < 42; i++)
{
let skillsHeader;
values.player1 = values.slice(25, 63).map((row, i) => {
const columns = row.slice(letterToColumn("A"), letterToColumn("G"));
skillsHeader ??= columns;
return columns
.reduce((obj, column, i) => {
obj[skillsHeader[i]] = column;
return obj;
},
{});
}).slice(1);
};
createJournals(values.player1, values.playernames[0])
}
Hooks.on("init", async () => {
setInterval(getData()}, 1000);
getData()
});```
const response = await fetch( produces an error now...
What's the error?
unexpected reserved word. Google says the function needs to by async
maybe that fixes it, I'll try (whatever that means ^^)
that fixed it ๐ and now it works!
โค๏ธ
Yeah. You can only use await inside asynchronous functions
Just realized that with my approach it appears that the data sometimes takes several seconds to update. Is there a smarter or more reliable way to update the data?