#Spreadsheet to journal entry table

1 messages ยท Page 1 of 1 (latest)

left fulcrum
#

Are you doing this on v9 or v10?

#

I'm asking because Journals changed a lot

thick sand
#

on v9 currently

#

but I would appreciate if it also worked after upgrading to v10

left fulcrum
#

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>
thick sand
#

thanks, I'll give it a try and see if I can manage to apply this ๐Ÿ™‚

left fulcrum
#

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

thick sand
#

oh btw stupid question, but would this update whenever the values update? (provided I include that in the module at a later stage)

left fulcrum
#

Nope. You'd have to have a different script that runs game.journal.getName("Test").update({ content: Handlebars.compile("")({ skills }) });

thick sand
#

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

thick sand
#

ok defining it first helped ^^

#

const skills = values.skills

left fulcrum
#

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 }),
});```
thick sand
#

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

left fulcrum
#

I edited my message

thick sand
#

that fixed it. Wondering what it was ^^

thick sand
#

thanks so much ๐Ÿ™‚

left fulcrum
#

Nice ๐Ÿ’ฏ

thick sand
#

(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?

thick sand
#

Nvm I think this time I got it on my own :D

setInterval(function () { getData(); }, 1000);
getData();

thick sand
#

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...

left fulcrum
thick sand
#

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!

#

โค๏ธ

left fulcrum
thick sand
#

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?