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

Unable to total calculated attribute from repeating section?

1564566012

Edited 1564566449
I'm trying to use a sheet worker script to total up the value of a calculated attribute ("skilltotal") from the repeating fieldset below. Ideally, the total should update whenever the user updates the "skill-ap" attribute. The final result should be output to the "Skillstotal" attribute. <h3 align="left">Skills [<input type="text" name="attr_Skillstotal" style="width:50px" value="0" readonly/>]</h3> <fieldset class="repeating_skills"> <div class="sheet-row" align="left" style="height:auto"> <div class="sheet-col" width="25px"> <button type='roll' style="width:15px; height:15px" value='!power {{ --name|@{character_name}: @{skillname} --leftsub|AV @{skill-ap}/EV ?{EV|Strength, @{tstrength}|Will, @{tintelligence}|Aura, @{taura}} --rightsub|OV ?{OV|11}/RV ?{RV|11} --TN|[[@{skill-ap}-?{OV}+11]] --Result|[[?{EV}-?{RV}+([[2d10!!cf0cs0]]-11+floor((?{EV})/6))]] RAPs}}' name='roll_skill'></button> </div> <div class="sheet-col" style="width:260px"> <textarea class="sheet-skillnames" type="text" name="attr_skillname" style="width:233px; height:13px; overflow:hidden;"/></textarea> </div> <div class="sheet-col" style="width:50px"> <input type="number" class="sheet-skills" name="attr_skill-ap" style="width:50px; height:23px" value="1"/> </div> <div class="sheet-col" style="width:50px"> <input type="number" class="sheet-skills" name="attr_skill-base" style="width:50px; height:23px" value="5"/> </div> <div class="sheet-col" style="width:50px"> <input type="number" class="sheet-skills" name="attr_skill-factor" style="width:50px; height:23px" value="1"/> </div> <div class="sheet-col" style="width:50px"> <input type="number" class="sheet-skills" name="attr_skilllevel" style="width:50px; height:23px" value="[[@{skill-base}+(@{skill-ap}*@{skill-factor})]]" disabled="disabled"/> </div> <div class="sheet-col"> <input type='checkbox' class='controller' name='attr_controller' value='1'> <div class='container'> <span class='sizer' name='attr_skilldesc'></span> <textarea class='visible' name='attr_skilldesc' style='width:250px'></textarea> </div> </div> </div> </fieldset> Here is the sheet worker script code: on("change:repeating_skills:skill-ap remove:repeating_skills", function() { getSectionIDs("repeating_skills", function(IDArray) { let fieldNames = []; for (var i=0; i < IDArray.length; i++) { fieldNames.push("repeating_skills_" + IDArray[i] + "_skilllevel"); } let total = 0; getAttrs(fieldNames, function(values) { for (var i=0; i < IDArray.length; i++) { total += parseInt(values["repeating_skills_" + IDArray[i] + "_skilllevel"])||0; } setAttrs({ skillstotal:total }); }); }); }); Thanks in advance for any suggestions or help anyone can give!
1564566979

Edited 1564567047
GiGs
Pro
Sheet Author
API Scripter
The problem is your skilllevel attribute is an autocalc field, and sheet workers dont work with them (without a lot of extra work, that isnt worth it). You should change this line <input type="number" class="sheet-skills" name="attr_skilllevel" style="width:50px; height:23px" value="[[@{skill-base}+(@{skill-ap}*@{skill-factor})]]" disabled="disabled"/> to <input type="number" class="sheet-skills" name="attr_skilllevel" style="width:50px; height:23px" value="" readonly /> And use a sheet worker to set its value from the skill-base, skill-ap, and skill-factor stats. And if any of those stats are autocalc fields, you'll need to replace them with sheet workers. Autocalc fields are fine if they are the endpoint of a calculation, but if you need to use them in other calculations, it's better to replace with sheet workers.
1564567034
GiGs
Pro
Sheet Author
API Scripter
PS: if you are going to do multiple summing of repeating sets, you might find the repeatingSum script useful.
Thank you for the reply! I'm afraid I'm pretty clueless about sheet workers, having only been shown how to use them in a limited way. How hard would it be to create one that would do those calculations?
1564570543

Edited 1564577529
GiGs
Pro
Sheet Author
API Scripter
very easily, it would look like this on("change:repeating_skills:skill-base change:repeating_skills:skill-a p change:repeating_skills:skill-factor remove:repeating_skills", function() { getAttrs(['repeating_skills_skill-base','repeating_skills_skill-factor','repeating_skills_skill-ap'], function(values) {                     var base = +values['repeating_skills_skill-base'] || 0;                     var ap = +values['repeating_skills_skill-ap'] || 0;                     var factor = +values['repeating_skills_skill-factor'] || 0;                     var total = (base + ap) * factor; setAttrs({ repeating_skills_skilllevel: total }); }); }); You could write this a lot more compactly, but this way you can see the steps involved. By the way, I'd recommend using underscores in your attribute names instead of dashes, which simplifies accessing their values in sheet workers. The above sheet worker would be written a little shorter on("change:repeating_skills:skill_base change:repeating_skills:skill_a p change:repeating_skills:skill_factor remove:repeating_skills", function() { getAttrs(['repeating_skills_skill_base','repeating_skills_skill_factor','repeating_skills_skill_ap'], function(values) {                     var base = +values.repeating_skills_skill_base || 0;                     var ap = +values.repeating_skills_skill_ap || 0;                     var factor = +values.repeating_skills_skill_factor || 0;                     var total = (base + ap) * factor; setAttrs({ repeating_skills_skilllevel: total }); }); }); Look at the var lines, for the difference. It's small but when you are doing a lot of sheet workers, it adds up. You cant use the second syntax if attribute names have a dash in them.
Hmm. I can't get the lines to total now. The line total just sits at 0. I'm probably missing something obvious, but here's what I have: <h3 align="left">Skills [<input type="text" name="attr_Skillstotal" style="width:50px" value="0" readonly/>]</h3> <div class="sheet-col" style="width:800px"> <fieldset class="repeating_skills"> <div class="sheet-row" align="left" style="height:auto"> <div class="sheet-col" width="25px"> <button type='roll' style="width:15px; height:15px" value='!power {{ --name|@{character_name}: @{skillname} --leftsub|AV @{skill_ap}/EV ?{EV|Strength, @{tstrength}|Will, @{tintelligence}|Aura, @{taura}} --rightsub|OV ?{OV|11}/RV ?{RV|11} --TN|[[@{skill_ap}-?{OV}+11]] --Result|[[?{EV}-?{RV}+([[2d10!!cf0cs0]]-11+floor((?{EV})/6))]] RAPs}}' name='roll_skill'></button> </div> <div class="sheet-col" style="width:260px"> <textarea class="sheet-skillnames" type="text" name="attr_skillname" style="width:233px; height:13px; overflow:hidden;"/></textarea> </div> <div class="sheet-col" style="width:50px"> <input type="number" class="sheet-skills" name="attr_skill_ap" style="width:50px; height:23px" value="1"/> </div> <div class="sheet-col" style="width:50px"> <input type="number" class="sheet-skills" name="attr_skill_base" style="width:50px; height:23px" value="5"/> </div> <div class="sheet-col" style="width:50px"> <input type="number" class="sheet-skills" name="attr_skill_factor" style="width:50px; height:23px" value="1"/> </div> <div class="sheet-col" style="width:50px"> <input type="number" class="sheet-skills" name="attr_skilllevel" style="width:50px; height:23px" value="" readonly /> </div> <div class="sheet-col"> <input type='checkbox' class='controller' name='attr_controller' value='1'> <div class='container'> <span class='sizer' name='attr_skilldesc'></span> <textarea class='visible' name='attr_skilldesc' style='width:250px'></textarea> </div> </div> </div> </fieldset> Using the following for the script: on("change:repeating_skills:skill_base change:repeating_skills:skill_ap change:repeating_skills:skill_factor remove:repeating_skills", function() { getAttrs(['skill_base','skill_factor','skill_ap'], function(values) { var base = +values.repeating_skills_skill_base || 0; var ap = +values.repeating_skills_skill_ap || 0; var factor = +values.repeating_skills_skill_factor || 0; var total = (base + ap) * factor; setAttrs({ repeating_skills_skilllevel: total }); }); }); on("change:repeating_skills:skill_base change:repeating_skills:skill_ap change:repeating_skills:skill_factor remove:repeating_skills", function() { getSectionIDs("repeating_skills", function(IDArray) { let fieldNames = []; for (var i=0; i < IDArray.length; i++) { fieldNames.push("repeating_skills_" + IDArray[i] + "_skilllevel"); } let total = 0; getAttrs(fieldNames, function(values) { for (var i=0; i < IDArray.length; i++) { total += parseInt(values["repeating_skills_" + IDArray[i] + "_skilllevel"])||0; } setAttrs({ skillstotal:total }); }); }); });
1564577472
GiGs
Pro
Sheet Author
API Scripter
That's a little oopsie on my part, I forgot the repeating_skills_ for the attribute names on this line getAttrs(['skill_base','skill_factor','skill_ap'], function(values) { which should be getAttrs(['repeating_skills_skill_base','repeating_skills_skill_factor','repeating_skills_skill_ap'], function(values) { I've corrected above. Minor point: the first line of repeating section sheet worker is currently this: on("change:repeating_skills:skill_base change:repeating_skills:skill_ap change:repeating_skills:skill_factor remove:repeating_skills", function() { getSectionIDs("repeating_skills", function(IDArray) { since it triggers off skilllevel, it should be this on("change:repeating_skills:skilllevel remove:repeating_skills", function() { getSectionIDs("repeating_skills", function(IDArray) { A couple of styling points: It doesnt affect the script but you have a capital letter at the start of your skillstotal name.  Also some of your attribute names you use skills_something, and other times you leave out the underscore. I'd suggest using the same scheme for consistency. e.g. maybe change skilllevel to skill_level, and skilltotal to skill_total to match the others.
That works like a charm--thank you so much!