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 a third attr to a repeating-section calculation sheetworker

So someone recently helped me out with a sheetworker that would take the product of two attrs within a repeating section row and then sum these numbers across all rows of the repeating section: on("change:repeating_extras:varex1 clicked:repeating_extras:itemexw clicked:repeating_extras:itemtux remove:repeating_extras", function() { getSectionIDs("repeating_extras", function(IDArray) { var fieldNames = [];         for (var i=0; i < IDArray.length; i++) { fieldNames.push("repeating_extras_" + IDArray[i] + "_itemex"); fieldNames.push("repeating_extras_" + IDArray[i] + "_varex1"); }         var total = 0;         getAttrs(fieldNames, function(values) { for (var i=0; i < IDArray.length; i++) { total += (parseInt(values["repeating_extras_" + IDArray[i] + "_itemex"])||0) * (parseInt(values["repeating_extras_" + IDArray[i] + "_varex1"])||0); } setAttrs({ "extraload": total });                 }); }); }); For example, if the first row had itemex=2 and varex1=3 and the second row had itemex=4 and varex1=5 then extraload would be (2*3)+(4*5)=26. But now I want to add a third attr to the product. I tried this but it doesn't work (in fact it breaks the sheetworker): on("change:repeating_extras:varex1 clicked:repeating_extras:itemexw clicked:repeating_extras:itemtux remove:repeating_extras", function() { getSectionIDs("repeating_extras", function(IDArray) { var fieldNames = [];         for (var i=0; i < IDArray.length; i++) { fieldNames.push("repeating_extras_" + IDArray[i] + "_itemex"); fieldNames.push("repeating_extras_" + IDArray[i] + "_varex1"); fieldNames.push("repeating_extras_" + IDArray[i] + "_itemtut"); }         var total = 0;         getAttrs(fieldNames, function(values) { for (var i=0; i < IDArray.length; i++) { tac += (parseInt(values["repeating_extras_" + IDArray[i] + "_itemex"])||0) * (parseInt(values["repeating_extras_" + IDArray[i] + "_varex1"])||0) * (parseInt(values["repeating_extras_" + IDArray[i] + "_itemtut"])||0); } setAttrs({ "extratac" : tac });                 }); }); }); Can anybody spot where I've gone wrong?
1646665732

Edited 1646666602
GiGs
Pro
Sheet Author
API Scripter
For your code, your problem is probably here: fieldNames.push("repeating_extras_" + IDArray[i] + "_itemtut"); the fieldname itemtut doesnt match the name in the event line: clicked:repeating_extras:itemtux
1646667227

Edited 1646667477
GiGs
Pro
Sheet Author
API Scripter
Also, a readabilty suggestion: there is no good reason to use attribute names like itemtut, varex, and itemex1. You sould always use longer names that fully describe what the attribute is referencing, like item_tutorial, variable_example, item_example1 or itemtutorial, variableexample, itemexample1 I have no idea what the actual names represent, but am just demonstrating how you can use longer names you could even create an attribute like this <item type="number" name="attr_this_is_a_really_big_attribute_name" value="0"> It make's your code more readable, and it'll be easier to figure out what it's doing when you look again at the code months later. Likewise in your for loop you have this line: tac += (parseInt(values["repeating_extras_" + IDArray[i] + "_itemex"])||0) * (parseInt(values["repeating_extras_" + IDArray[i] + "_varex1"])||0) * (parseInt(values["repeating_extras_" + IDArray[i] + "_itemtut"])||0); There's sometimes av value in compactness, but this is really hard to read. You would be much better off replacing that line with this:                  const itemex = parseInt ( values [ "repeating_extras_" + IDArray [ i ] + "_itemex" ]) || 1 ;                 const varex1 = parseInt ( values [ "repeating_extras_" + IDArray [ i ] + "_varex1" ]) || 1 ;                 const itemtux = parseInt ( values [ "repeating_extras_" + IDArray [ i ] + "_itemtux" ]) || 1 ;                 tac += itemex * varex1 * itemtux ; const works just like var, except the variable exists only within the loop, and can't be changed once created. So this breaks apart the creation of each part of the expression, and makes that multiple much easier to read.When you get into logging and debugging, it also lets you examine each attribute, so you can see where the problem is, which will be very useful later. Generally speaking (and just IMO- it's not a rule) if you are doing something with 2 or more attributes at the same time, it's better to put them in variables first, like this. Notice I changed the || 0 to || 1. That sets a default value, and if you are multiplying things together, 1 is a better default value than 0 - when your attribute doesnt exist or has an incorrect value, the rest of the multiple still works. 0 is better if you want to see immediately when something is wrong with one of your attributes. When the sheet is complete and has been robustly tested, 1 is better.
GiGs said: For your code, your problem is probably here: fieldNames.push("repeating_extras_" + IDArray[i] + "_itemtut"); the fieldname itemtut doesnt match the name in the event line: clicked:repeating_extras:itemtux That's not it – itemtux is a button whilst itemtut is a number attr. All I did was take a working sheetworker (see first code in my OP) and do the following to it: – Added this line: fieldNames.push("repeating_extras_" + IDArray[i] + "_itemtut"); – Added this operation to the end of the product calculation: * (parseInt(values["repeating_extras_" + IDArray[i] + "_itemtut"])||0) – Replaced 'total' with 'tac' and 'extraload' with 'extratac' Aaaaaaand I think I've figured it out. ... Yup, that 'total' is more than an arbitrary name I see! Shouldn't have changed it and now that I've put it back it all works.
1646667553
GiGs
Pro
Sheet Author
API Scripter
Aero said: That's not it – itemtux is a button whilst itemtut is a number attr. oh I should have noticed it was saying clicked, hehe. Glad you sorted it.