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

Returning summed values using the repeating sum macro by Aaron

1676421917
Warhound
Pro
Marketplace Creator
Sheet Author
Compendium Curator
I am trying to rewrite this function:  RepeatingSum - Roll20 Wiki  to return the value instead of writing it to an attribute.  I managed to get it working however it requires a global variable to function, which I cant update unless the sheet is closed and reopened.  My function call:             var sum_test    = repeatingSumReturn('characteristics','characteristic_spending_str_extra')         console.log(sum_test); this works when I manually return a 1 in the function. This function isn't working. I cant figure out how to return the sumTotal as returning it in the getAttrs doesn't return the value.     var sumTotal = 0;     const repeatingSumReturn = (section, fields, multiplier = 1) => {       var sumTotal = 0;       if (!Array.isArray(fields)) fields = [fields];       getSectionIDs(`repeating_${section}`, idArray => {         const attrArray = idArray.reduce( (m,id) => [...m, ...(fields.map(field => `repeating_${section}_${id}_${field}`))],[]);         getAttrs(attrArray, v => {           // getValue: if not a number, returns 1 if it is 'on' (checkbox), otherwise returns 0..           getValue = (section, id,field) => parseFloat(v[`repeating_${section}_${id}_${field}`]) || (v[`repeating_${section}_${id}_${field}`] === 'on' ? 1 : 0);           sumTotal = idArray.reduce((total, id) => total + fields.reduce((subtotal,field) => subtotal * getValue(section, id,field),1),0);               console.log("Sum1= "+ sumTotal); });       });       console.log("Sum1= "+ sumTotal);       return sumTotal       // return 1 //This was a test and worked, somehow I need to grab the sum total value     }; my console log: Sum2= 0 0 Sum1= 13
1676435611

Edited 1676435715
GiGs
Pro
Sheet Author
API Scripter
I plan to rewrite it at some point to return a local attribute, but it was written that way to avoid a prolem with asynchronicity in roll20. The problem is anything inside getAttrs only exists inside that getAttrs - unless you create an attribute before that.  You would need your return sumTotal to be inside the getAttrs. I'd recommend just using it as intended in the meantime, and let it write to a created attribute. You can hide that attribute, and only use it in other sheet workers. Doing it that way does make code more modular. PS: the reatingSum macro isnt by Aarom, it's by me. Aaron does have a script that does the same things as repeatingSum (and a bit more, I think), but the syntax is a bit more complicated and requires a lot more code (which doesn't really matter). I wroterepeatingSum to keep things simpler for the end user, and avoid the need of learning any complex syntax.