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

Adding value on repeating section

1693879910

Edited 1693879947
I have a section called background which can have various user inputed values.&nbsp; And I want to add all the values but cannot seem to get it to work, there might be a typo or something of the kind since I only copy and paste the example on this " <a href="https://wiki.roll20.net/RepeatingSum" rel="nofollow">https://wiki.roll20.net/RepeatingSum</a> " &nbsp; &nbsp; //Handle advantages/disadvantages cost &nbsp; &nbsp; const bTotal = ['advantage_total', 'disadvantage_total']; &nbsp; &nbsp; const bSection = ['advantage', 'disadvantage']; &nbsp; &nbsp; const bCosts = 'advantage_cost'; &nbsp; &nbsp; const repeatingSum = (bTotal, bSection, bCosts) =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; if (!Array.isArray(bTotal)) bTotal = [bTotal.replace(/\s/g, '').split(',')]; &nbsp; &nbsp; &nbsp; &nbsp; if (!Array.isArray(bCosts)) bCosts = [bCosts.replace(/\s/g, '').split(',')]; &nbsp; &nbsp; &nbsp; &nbsp; getSectionIDs(`repeating_${bSection}`, idArray =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const attrArray = idArray.reduce((m, id) =&gt; [...m, ...(bCosts.map(field =&gt; `repeating_${bSection}_${id}_$ {field}`))], []); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getAttrs([...attrArray], v =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const getValue = (bSection, id, field) =&gt; v[`repeating_${bSection}_${id}_${field}`] === 'on' ? 1 : parseFloat(v[`repeating_${bSection}_${id}_${field}`]) || 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const commonMultipliers = (bCosts.length &lt;= bTotal.length) ? [] : bCosts.splice(bTotal.length, bCosts.length - bTotal.length); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const output = {}; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bTotal.forEach((destination, index) =&gt; { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output[destination] = idArray.reduce((total, id) =&gt; total + getValue(bSection, id, bCosts[index]) * commonMultipliers.reduce((subtotal, mult) =&gt; subtotal * getValue(bSection, id, mult), 1), 0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setAttrs(output); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp; }; &nbsp; &nbsp; on('change:repeating_advantage:advantage_cost', function() { &nbsp; &nbsp; &nbsp; &nbsp; repeatingSum("advantage_total", "advantage_cost"); &nbsp; &nbsp; }); HTML &nbsp; &nbsp; &nbsp; &nbsp; &lt; div class = "advantages" &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt; h1 &gt; Ventajas &lt;/ h1 &gt;&lt; br &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt; input type = "number" name = "attr_advantage_total" readonly /&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt; fieldset class = "repeating_advantage" &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt; select name = "attr_advantage" &gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;...&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt; input type = "number" name = "attr_advantage_cost" value = "1" &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt; div class = "advantageData" &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt; textarea name = "attr_advantage" &gt;&lt;/ textarea &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/ div &gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/ fieldset &gt; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/ div &gt;
1693882960
GiGs
Pro
Sheet Author
API Scripter
This doesn't look right to me: on('change:repeating_advantage:advantage_cost', function() { repeatingSum("advantage_total", "advantage_cost"); }); repeatingSum needs three items, and you only have two. The three items needed are the name of the destination, the name of the repeating section, and the name of the column in the repeating section. You are missing the repeating section name, so that shou;d probably be: on('change:repeating_advantage:advantage_cost', function() { repeatingSum("advantage_total", "advantage", "advantage_cost"); });
1693884011

Edited 1693884087
GiGs
Pro
Sheet Author
API Scripter
It looks like you have edited the repeatingSum function too. You should copy it, paste it into your script block, and do not edit it . Your repeatingSum function should be exactly this, with no changes : 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); }); }); };
I see, thank you for your help. I know this is probably not the correct section/thread to ask, but is there a way to make it so the HTML sheet reads a json to get the data needed for the sheat?
1694047078
vÍnce
Pro
Sheet Author
MaRiTg R. said: I see, thank you for your help. I know this is probably not the correct section/thread to ask, but is there a way to make it so the HTML sheet reads a json to get the data needed for the sheat? This would either have to be programed into the sheet (ie sheetworker js) or an API Mod/Script.&nbsp; I'm fairly confident that either method would involve in-depth knowledge of the sheet's attributes and how they are calculated if sheetworkers are already involved for auto-calculated attributes.
I see, thank you both for the help!
1694054982
GiGs
Pro
Sheet Author
API Scripter
I believe there have been one or two sheets which did support JSON import. As Vince points out, these requires knowledge of all the character sheet attributes. You'll likely need a textarea as an attribute. paste your JSON into that, and a sheet worker runs, parses each value in the JSON and converts it into a Roll20 attribute. This will be a laborious task if there's a lot of attributes. It has to be written, fully accounting for all possible details.
1694062808

Edited 1694062881
John D.
Pro
Sheet Author
MaRiTg R. said: I see, thank you for your help. I know this is probably not the correct section/thread to ask, but is there a way to make it so the HTML sheet reads a json to get the data needed for the sheat? Are you looking to use JSON for importing data and filling out a sheet, or are you looking to use JSON for storing attribute values?&nbsp; If the latter, I do this extensively in the new sheet I'm developing and it works great!&nbsp; If you're interested I can go into more detail or try to address any questions you have.