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

Averaging all values in a repeating section

I'm trying to figure out how to get the average of all of a specific value in a repeating section to be displayed outside of the repeating section. Necessary code is below. The repeating section: <fieldset class="repeating_body"> <input type="number" class="sheet-breakdownArmorBpCurInput" name="attr_bodyArmorBpCur" value="0"> </fieldset> Where I want the average to be displayed: <input type="number" class="sheet-averageArmorBpCurInput" name="attr_aabpCur"> For extra context, I want the average to change any time one of the rows in the repeating section is changed.
1612734823

Edited 1612753863
GiGs
Pro
Sheet Author
API Scripter
Here's a sheet worker to do that calculation - see the notes afterwards: on ( 'change:repeating_body:bodyarmorbpcur remove:repeating_body' , ()  =>  {      getSectionIDs ( 'repeating_body' ,  idarray   =>  {          const   fields  = [];          idarray . forEach ( id   =>   fields . push ( `repeating_body_ ${ id } _bodyArmorBpCur` ));          getAttrs ( fields ,  bp   =>  {              const   values =  Object . values ( bp ). map ( v   =>  + v  || 0 );              const   sum  =  values . reduce (( a ,  b )  =>   a  +  b ,  0 );              const   average  =  Math . round ( sum  / values . length );              setAttrs ({                  aabpCur :   average             });         });     }); }); This rounds the average off to decimals. If you want to round to whole numbers, change this line:              const   average  =  Math . round (( sum  / values . length ) * 100 ) / 100 ; to              const   average  =  Math . round ( sum  / values . length ) ;
That's just copying the value I change any of the rows to. Also I'm rounding to whole numbers.
1612753838
GiGs
Pro
Sheet Author
API Scripter
There was a typo in the code, I have corrected. Though with that typo it shouldnt have been producing any output. Try it now
That works perfectly. Thanks for the help!
1612829085
GiGs
Pro
Sheet Author
API Scripter
great :)