Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Sheet Worker] Multiplying in repeating Field

January 30 (8 years ago)
Coal Powered Puppet
Pro
Sheet Author
I am trying to get a sheet worker to do multiplication inside a repeating field, and its not being nice.  Can anyone explain what I am doing wrong?  The formula is:

numberleft * massleft = masstotalleft

This is my html
                    <fieldset class="repeating_othergear">
                        <div class='sheet-2colrow'>
                            <div class='sheet-col'>
                                <table class="sheet-skill" style="width:100%">
                                    <tr>
                                        <th>Gear</th>
                                        <th><input type="text" class="sheet-input" name="attr_gear_name-left" /></th>
                                        <th>Amount</th>
                                        <th><input type="number" class="sheet-input" name="attr_numberleft" value="0" /></th>
                                        <th>Mass ea.</th>
                                        <th><input type="number" class="sheet-input" name="attr_massleft" value="0" /></th>
                                    </tr>
                                    <tr>
                                        <td><input type="number" class="sheet-input" name="attr_masstotalleft" value="0" /></tr></td>
                                    </tr>
                                    <tr>
                                        <td colspan="6"><textarea class="sheet-input" name="attr_gearnotes-left"></textarea></td>
                                    </tr>
                                </table>
                            </div>
                            <div class='sheet-col'>
                            <table class="sheet-skill" style="width:100%">
                                    <tr>
                                        <th>Gear</th>
                                        <th><input type="text" class="sheet-input" name="attr_gear_name-right" /></th>
                                        <th>Amount</th>
                                        <th><input type="number" class="sheet-input" name="attr_numberright" value="0" /></th>
                                        <th>Mass ea.</th>
                                        <th><input type="number" class="sheet-input" name="attr_massright" value="0"/></th>
                                    </tr>
                                    <tr>
                                        <td><input type="number" class="sheet-input" name="attr_masstotalright" value="0" /></tr></td>
                                    </tr>
                                    <tr>
                                        <td colspan="4"><textarea class="sheet-input" name="attr_gearnotes-right"></textarea></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </fieldset>
And this is what I am working with in the sheet worker:
    
on("change:repeating_othergear:massleft change:repeating_othergear:numberleft sheet:opened", function() {
   getAttrs([
      "repeating_othergear_massleft", "repeating_othergear_numberleft"
    ], function(values) {
            repeating_othergear_masstotalleft: values.repeating_othergear_massleft * values.repeating_othergear_numberleft
            });
        });
    });
Thanks for your time.
January 30 (8 years ago)
Kryx
Pro
Sheet Author
API Scripter
You need to use getSectionIDs for repeating sections.
You'll probably also need to convert those values to intergers (or floats) using parseInt(VALUE, 10) or parseFloat(VALUE) for them to multiply.
January 30 (8 years ago)
Coal Powered Puppet
Pro
Sheet Author
This worked when I was just adding or subtracting the values.  What the difference now?
January 30 (8 years ago)
Kryx
Pro
Sheet Author
API Scripter
I suspect it didn't work as there is nothing named "repeating_othergear_masstotal_left". Everything with repeating has an ID and you need to reference it by that ID.
January 30 (8 years ago)
Lithl
Pro
Sheet Author
API Scripter
You only seed getSectionIDs if you're trying to operate on multiple rows. If your calculation is only affecting a single row, it's not necessary.

However, Kryx's other point is well-taken: you should absolutely parseInt or parseFloat any values you expect to be numbers. On the backend, attributes might be stored as numbers or strings, and trying to do math on strings will sometimes behave how you want, while other times it won't. Fortunately, parseInt or parseFloat are (essentially) no-op when given a number, so it doesn't matter what the value was originally, you're guaranteed to have a number after parsing.
January 30 (8 years ago)
Coal Powered Puppet
Pro
Sheet Author
...okay.  How do I do that?
January 30 (8 years ago)
Kryx
Pro
Sheet Author
API Scripter

Kryx said:

You'll probably also need to convert those values to intergers (or floats) using parseInt(VALUE, 10) or parseFloat(VALUE) for them to multiply.

repeating_othergear_masstotalleft: parseFloat(values.repeating_othergear_massleft) * parseFloat(values.repeating_othergear_numberleft)

January 31 (8 years ago)
Coal Powered Puppet
Pro
Sheet Author
Thank you Kryx and Brian.  That fixed it.  That was a big item off my list