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 .
×
Advertisement Create a free account

Is there a way to iterate through the attributes in a repeating section?

1623203911
I'd like to have some fields that aggregate the attribute values in some repeated sections. There are a lot of attributes, and I'd prefer not to hand-code each one. Is there some way to iterate over all of the attributes in a repeating section so that I can add up individual attributes across repeaters by only changing the reference to their repeater ID?
1623209021
Andreas J.
Forum Champion
Sheet Author
Translator
<a href="https://wiki.roll20.net/RepeatingSum" rel="nofollow">https://wiki.roll20.net/RepeatingSum</a>
1623218492
GiGs
Pro
Sheet Author
API Scripter
As Andreas points out, that repeatingSum function will perform almost any calculation you need to do on a repeating section as a whole. Just copy in the function once, then you can use it as many times as you need.
1623270837
That looks great! Thanks!
1623274972
GiGs said: As Andreas points out, that repeatingSum function will perform almost any calculation you need to do on a repeating section as a whole. Just copy in the function once, then you can use it as many times as you need. Hey GiGs. This function is great, and I see that you're the author. I tried going to your GitHub page to submit a PR, but&nbsp; <a href="https://github.com/g-g-g" rel="nofollow">https://github.com/g-g-g</a> &nbsp;doesn't seem like it's it ("Author: GiGs(G-G-G on github)" is listed on&nbsp; <a href="https://wiki.roll20.net/RepeatingSum" rel="nofollow">https://wiki.roll20.net/RepeatingSum</a>). &nbsp;At any rate, I needed a way to filter by section ID, so that it doesn't add up across _all_ rows in the repeater, but only selected rows. Does this code live on GitHub somewhere or something that I can do a PR? How does that process work here?
1623277326

Edited 1623277617
Andreas J.
Forum Champion
Sheet Author
Translator
the code is literally on that page: <a href="https://wiki.roll20.net/RepeatingSum#The_Code" rel="nofollow">https://wiki.roll20.net/RepeatingSum#The_Code</a> /* ===== PARAMETERS ========== destinations = the name of the attribute that stores the total quantity can be a single attribute, or an array: ['total_cost', 'total_weight'] If more than one, the matching fields must be in the same order. section = name of repeating fieldset, without the repeating_ fields = the name of the attribute field to be summed destination and fields both can be a single attribute: 'weight' or an array of attributes: ['weight','number','equipped'] */ const repeatingSum = (destinations, section, fields) = &gt; { if ( ! Array . isArray (destinations)) destinations = [destinations. replace ( / \s / g , '' ). split ( ',' )]; if ( ! Array . isArray (fields)) fields = [fields. replace ( / \s / g , '' ). split ( ',' )]; getSectionIDs (`repeating_${section}`, idArray = &gt; { const attrArray = idArray. reduce ((m, id) = &gt; [...m, ...(fields. map (field = &gt; `repeating_${section}_${id}_${field}`))], []); getAttrs ([...attrArray], v = &gt; { const getValue = (section, id, field) = &gt; v[`repeating_${section}_${id}_${field}`] = = = 'on' &nbsp;? 1 &nbsp;: parseFloat (v[`repeating_${section}_${id}_${field}`]) | | 0 ; const commonMultipliers = (fields. length &lt; = destinations. length )&nbsp;? []&nbsp;: fields. splice (destinations. length , fields. length - destinations. length ); const output = {}; destinations. forEach ((destination, index) = &gt; { output[destination] = idArray. reduce ((total, id) = &gt; total + getValue (section, id, fields[index]) * commonMultipliers. reduce ((subtotal, mult) = &gt; subtotal * getValue (section, id, mult), 1 ), 0 ); }); setAttrs (output); }); }); }; Why do you need to do a PR on his code, what are you trying to achieve with it? Just copy the code block to your sheetworker section and then start making your own function that relies on it. Or use version 2 of it: <a href="https://wiki.roll20.net/RepeatingSum#repeatingSum_Version_2" rel="nofollow">https://wiki.roll20.net/RepeatingSum#repeatingSum_Version_2</a> At any rate, I needed a way to filter by section ID, so that it doesn't add up across _all_ rows in the repeater, but only selected rows. What's the condition for adding some rows, but not others?
1623278316

Edited 1623278544
I know it's on that page :)&nbsp; I just figured there might be a master copy somewhere, and I didn't want to edit the wiki and change anything without an approval.&nbsp; I am working with the v2 function. What's the condition for adding some rows, but not others? In the sheet I'm working on there are a bunch of repeated sections for combat skills. Some of those sections may add to a combined combat total, but it depends on the situation. Ex. a player may have a combat skill with two types of Power Armor, but can't wear both at the same time, so they have to select which one they're using, and those bonuses get added to their base combat bonuses. The code I've added is pretty straightforward, but I'm not sure I'm using `extras` correctly. sectionIds = ["-mbi9lxesg8ykw9xdrrv", "-mbibztskucasidm8qg1", "-mbicc-pmg2fbprltnrg"]; // Determined elsewhere repeatingSum(destination, section, field, `filter:${ sectionIds . toString () }`); And in the function itself I've added/changed: let filteredAttrArray = attrArray; if (properties.attributes.filter) { filteredAttrArray = attrArray.filter((attr) =&gt; Object.keys(properties.attributes.filter).some((sectionId) =&gt; attr.includes(sectionId) ) ); } getAttrs( [...filteredAttrArray, ...Object.keys(properties.attributes)],
1623287703

Edited 1623287939
GiGs
Pro
Sheet Author
API Scripter
I dont want anyone but me to edit that, honestly. The function should be universal, and I'm hesitant about feature creep from people modifying it for things that are primarily useful for themselves. (And I have considered rewriting it to improve the syntax, which gets harder if there's stuff in it I didnt create. Honestly, I should set a github for it, and disallow contributions, so i have a safe master copy online.) I think you should be able to do what you want with the v2 already, but I'd need to study what it is you're trying to do to make sure I understand it. There's not enough information in your post.
1623290130

Edited 1623293696
Ya, I can totally do what I want with the existing function and a slight modification. I was just thinking about providing that "extension" to others. If you don't want to do that, I'm fine with it. All good. I should add that if there was a repo and I could submit a PR, you could see the changes in context (as opposed to copy/paste code in a forum), which is why I wanted the facility to submit a PR in the first place :)
1623318409
Andreas J.
Forum Champion
Sheet Author
Translator
GiGs said: I dont want anyone but me to edit that, honestly. The function should be universal, and I'm hesitant about feature creep from people modifying it for things that are primarily useful for themselves. (And I have considered rewriting it to improve the syntax, which gets harder if there's stuff in it I didnt create. Honestly, I should set a github for it, and disallow contributions, so i have a safe master copy online.) This, so much. RepeatSum v1 &amp; RepeatSum v2 shouldn't be edited by anyone but GiGs, and honestly any edits should just lead to new versions of it, and let the existing two version remain untouched. Yeah, create a github repo there you have copies of the two. I should add that if there was a repo and I could submit a PR, you could see the changes in context (as opposed to copy/paste code in a forum), which is why I wanted the facility to submit a PR in the first place :) you can set up a github gist, with the first version being the initial submit, and then update the gist with your version, resulting in being able to display your changes easily. Here is some old gist I made, with two revisions: <a href="https://gist.github.com/Anduh/e10578fc144ce8a4a5ba7dc5f16c386e/revisions" rel="nofollow">https://gist.github.com/Anduh/e10578fc144ce8a4a5ba7dc5f16c386e/revisions</a>
1623331316
To be clear, I'm not trying to step on anyone's toes. I just wanted to contribute upstream to the code to provide additional features. If you don't want that, no problem. Consider this thread closed. Thanks for the help.
1623332984
GiGs
Pro
Sheet Author
API Scripter
Your gesture is appreciated, and is perfectly reasonable since the code is on a wiki after all which are supposed to be user-edited. I'll be looking at your code when I get a chance (over the weekend probably). Do you have the full code of your version of the function anywhere?
1623335130
<a href="https://gist.github.com/ggutenberg/1037a649786da3a364cc800ad1806ff4" rel="nofollow">https://gist.github.com/ggutenberg/1037a649786da3a364cc800ad1806ff4</a> My apologies for the formatting - Prettier had its way with it. I've commented the bit I've added with "// New code begins" and there's a sample call at the bottom.