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

Sheetworker help with repeatingSum

Hi,  I have a problem with a function from the repeatingSum page on the wiki. I am trying to replicate the conditional example for it.  I have a field for the weight in the repeating section as well as a checkbox. I want to have a field outside the repeating section that tallies up the total weight only for the rows where the checkbox is marked. The example on the wiki is: on ( 'change:repeating_armour remove:repeating_armour' , function () { repeatingSum ( "armour_weight" , "armour" ,[ "armour_piece" ,"armour_worn"]); }); And here is the HTML from my test: <input type="text" class="sheet-input" name="attr_geartotalcarriedtest"/> <input type="text" class="sheet-input" name="attr_geartotalweighttest"/> <div>     <fieldset class="repeating_othergear">         <div>             <table class="sheet-skill" style="width:800px">                 <tr>                     <th><input type="checkbox" class="sheet-carried" name="attr_gearequipped" value="0"/><span></span></th>                     <th data-i18n="gearcell-u"style="width:50px;">Gear</th>                     <th><input type="text" class="sheet-input" style="width:140px" name="attr_gearnamerep" /></th>                     <th data-i18n="gearquantity-u" style="width:50px;">Quantity</th>                     <th><input type="text" class="sheet-input"style="width:40px;" name="attr_gearquantityrep" value="0" /></th>                     <th data-i18n="gearweight-u"style="width:50px;">KG each</th>                     <th><input type="text" class="sheet-input" style="width:50px;" name="attr_gearmasseachrep" value="0" step="0.05"/></th>                     <th data-i18n="gearweightplusquantity-u"style="width:50px;">Weight</th>                     <th><input type="text" class="sheet-input" style="width:40px;" name="attr_gearmasstotalrep" value="0" step="0.05"/></th>                     <th><input class="sheet-textbox" type="text" style="width:290px; margin-left:5px;" value="" name="attr_gearnoterep" placeholder="Info" /></th>                 </tr>             </table>         </div>     </fieldset> </div> <script type="text/worker"> const repeatingSum = (destinations, section, fields) => { if (!Array.isArray(destinations)) destinations = [destinations.replace(/\s/g, '').split(',')]; if (!Array.isArray(fields)) fields = [fields.replace(/\s/g, '').split(',')]; getSectionIDs(`repeating_${section}`, idArray => { const attrArray = idArray.reduce((m, id) => [...m, ...(fields.map(field => `repeating_${section}_${id}_${field}`))], []); getAttrs([...attrArray], v => { const getValue = (section, id, field) => v[`repeating_${section}_${id}_${field}`] === 'on' ? 1 : parseFloat(v[`repeating_${section}_${id}_${field}`]) || 0; const commonMultipliers = (fields.length <= destinations.length) ? [] : fields.splice(destinations.length, fields.length - destinations.length); const output = {}; destinations.forEach((destination, index) => { output[destination] = idArray.reduce((total, id) => total + getValue(section, id, fields[index]) * commonMultipliers.reduce((subtotal, mult) => subtotal * getValue(section, id, mult), 1), 0); }); setAttrs(output); }); }); }; on('change:repeating_othergear remove:repeating_othergear', function() { repeatingSum("geartotalcarriedtest","othergear",["gearmasstotalrep", "gearequipped"]); }); on('change:repeating_othergear remove:repeating_othergear', function() { repeatingSum("geartotalweighttest","othergear","gearmasstotalrep"); }); </script> The field (geartotalcarriedtest) that the total of the checked rows should be totalled gets set to 0 and it sets all checkboxes to marked. Not sure what is different from the example to what I put in there.  What should I change to get it work?
1642702563

Edited 1642702822
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
You checkbox has an error in it. You've set it's checked value to "0", which is the same value all checkboxes have when they are unchecked. You need to set it to "1". Checkbox's default settings are: 0 = unchecked "on" = checked unchecked It isn't possible in a Roll20 sheet to change the unchecked state of 0. I recommend always changing the checked value to something that relates to the purpose of the checkbox or will make sheetworker calculations easier. And, finally, if you want to set a checkbox to be checked by default, you add the "checked" property to the input like so: <input type="checkbox" class="sheet-carried" name="attr_gearequipped" value="1" checked>
Scott C. said: You checkbox has an error in it. You've set it's checked value to "0", which is the same value all checkboxes have when they are unchecked. You need to set it to "1" Excellent, thank you. That made it work!