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

Can all the values in a repeating sheet section be totaled?

So, let's say I have something like this going on: <fieldset class="repeating_purviews"> <div class="sheet-col"> <textarea class="sheet-purviewnames" type="text" name="attr_purviewname" style="width:120px; height:20px"/></textarea> </div> <div class="sheet-col"> <select class="sheet-purviewlevels" name="attr_purviewlevel" style="width:50px"> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </div> <div class="sheet-col" width="600px"> <textarea class="sheet-purviewdescs" type="text" name="attr_purviewdesc" style="width:600px; height:20px"/></textarea> </div> </fieldset> Is it possible to total up all the entries players end up having for the attr_purviewlevel entries, such as for the sake of tracking point expenditures?
1535766297
GiGs
Pro
Sheet Author
API Scripter
Yes it is. This Sheet Worker is untested, but should work. Replace the TOTALPOINTS attribute name with the name of the attribute you want the total to be displayed in. on("change:repeating_purviews:purviewlevel remove:repeating_purviews", function() { getSectionIDs("repeating_purviews", function(IDArray) { let fieldNames = [];         for (var i=0; i < IDArray.length; i++) { fieldNames.push("repeating_purviews_" + IDArray[i] + "_purviewlevel"); }         let total = 0;         getAttrs(fieldNames, function(values) { for (var i=0; i < IDArray.length; i++) { total += parseInt(values["repeating_purviews_" + IDArray[i] + "_purviewlevel"])||0; } setAttrs({                 TOTALPOINTS:total             });         }); }); }); There'll be a much more elegant way to do this, of course.
1535778016

Edited 1535778060
Dave
Pro
Thanks for the suggestion! Does this go somewhere in the CSS? Also, would I need to make separate versions of this for each different repeating section that I wanted to total up?
1535808602
GiGs
Pro
Sheet Author
API Scripter
It goes in the html.  At the end of your html file, create a block like this, and put all sheet worker code like the one i posted above inside it. <script type="text/worker"> </script> If you do have multiple repeating sections you need to total, it might be possible to create a single universal sheet worker to manage them all, but it's easiest to just copy the code and replace the attribute names as appropriate.
Awesome! Thank you! I'm having trouble making a second version work, though. This is what I have at the end of my sheet: <script type="text/worker"> on("change:repeating_purviews:purviewlevel remove:repeating_purviews", function() { getSectionIDs("repeating_purviews", function(IDArray) { let fieldNames = []; for (var i=0; i < IDArray.length; i++) { fieldNames.push("repeating_purviews_" + IDArray[i] + "_purviewlevel"); } let total = 0; getAttrs(fieldNames, function(values) { for (var i=0; i < IDArray.length; i++) { total += parseInt(values["repeating_purviews_" + IDArray[i] + "_purviewlevel"])||0; } setAttrs({ PurviewsTotal:total }); }); }); }); on("change:repeating_birthrights:birthrightlevel remove:repeating_birthrights", function() { getSectionIDs("repeating_birthrights", function(IDArray) { let fieldNames = []; for (var i=0; i < IDArray.length; i++) { fieldNames.push("repeating_birthrights_" + IDArray[i] + "_birthrightlevel"); } let total = 0; getAttrs(fieldNames, function(values) { for (var i=0; i < IDArray.length; i++) { total += parseInt(values["repeating_birthrights_" + IDArray[i] + "_birthrightlevel"])||0; } setAttrs({ BirthrightsTotal:total }); }); }); }); </script>     However, the BirthrightsTotal doesn't seem to total anything. I suspect I'm doing something obviously wrong--any ideas?
1535853116
GiGs
Pro
Sheet Author
API Scripter
Nothing jumps out at me as wrong there. Can you post the html for that repreating section, along with the birthrightstotal attribute.
    <h3 align="left">Birthrights [<input type="text" name="attr_ShowBirthrightsTotal" style="width:30px" value="@{BirthrightsTotal}" disabled="disabled"/>]</h3>     <div class="sheet-col" width="300">              <fieldset class="repeating_birthrights">             <div class="sheet-col">                 <textarea class="sheet-birthrightnames" type="text" name="attr_birthrightname" style="width:200px; height:20px"/></textarea>             </div>             <div class="sheet-col">                 <select class="sheet-birthrightlevels" name="attr_birthrightlevel" style="width:40px">                     <option value="0">0</option>                      <option value="1">1</option>                     <option value="2">2</option>                     <option value="3">3</option>                     <option value="4">4</option>                     <option value="5">5</option>                 </select>             </div>             <div class="sheet-col" width="515px">                 <textarea class="sheet-birthrightdescs" type="text" name="attr_birthrightdesc" style="width:515px; height:20px"/></textarea>             </div>         </fieldset>          </div>
1535857896

Edited 1535857956
GiGs
Pro
Sheet Author
API Scripter
I see a ShowBirthrightstotal attribute there but not a birthsrightstotal. You could change that first line to <h3 align="left">Birthrights [<input type="text" name="attr_BirthrightsTotal" style="width:30px" value="0" readonly/>]</h3> sheet workers cant modify attribute inputs set to disabled. Those are reserved for autocalc fields. But you can set an input to readonly - then players cant modify the input, but sheet workers can.
That works perfectly. Thank you so much for all your help! :D
1535901138
GiGs
Pro
Sheet Author
API Scripter
Glad to help. Every now and then someone comes on the forums asking how to sum a repeating set, and for some reason, its a question that nearly always goes unanswered. I myself asked this quest three times over the course of a year, all unanswered, till i pieced together how to do it myself. Now I always answer that particular question when i see it.