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

Sheet Worker Script Code Help?

1585625228

Edited 1585625270
<script type='text/worker'> on('change:repeating_rangedweapons:attr_R_weapon-weight remove:repeating_rangedweapons change:gear_weight change:repeating_gear:gear_weight_r remove:repeating_gear', () => { getSectionIDs(`repeating_gear`, idArray => { const fieldnames = []; idArray.forEach(id => fieldnames.push (`repeating_gear_${id}_gear_weight_r`)); getAttrs(['weight_total', 'gear_weight', ...fieldnames], v => { const getValue = (id, field) => parseFloat(v[`repeating_gear_${id}_${field}`]) || 0 const total = parseFloat(v.weight_total) || 0 const weight = parseFloat(v.gear_weight) || 0 const weight_r = idArray.reduce((total, id) => total + getValue(id,'gear_weight_r'), 0) setAttrs({weight_total: weight + weight_r}); }); }); }); </script>     So the above code is one of Roll20 API's sheet workers.  What I want to do is add repeating_rangedweapons:attr_R_weapon-weight and the item amount (attr_gear_quantity) which is a value from a different fieldset into this sheet worker, but I'm not sure how I can do it with the current written code layout.     Essentially I need these red arrow fields to influence the weight calculation so, once triggered by either a change in values or removed entry in the fieldset, it runs again and recalculates weight value.  To do this, I must rewrite the code's layout.  I cannot use the code as-is because I am attempting to retrieve data for use from multiple fieldsets.  Is this even possible?  The below was my attempt after trying to rewrite it, but it still doesn't work.  I cannot read replies until 8-9 hours from now. <script type='text/worker'> on('change:repeating_rangedweapons:attr_R_weapon-weight remove:repeating_rangedweapons change:gear_weight change:repeating_gear:gear_weight_r remove:repeating_gear', () => { getSectionIDs(`repeating_gear`, idArray => { const fieldnames = [] const total = 0 const weight = 0 const weight_r = 0 idArray.forEach(id => fieldnames.push(`repeating_gear_${id}_gear_weight_r`)) idArray.forEach(id => fieldnames.push(`repeating_rangedweapons_${id}_attr_R_weapon-weight`)) getAttrs(['weight_total', 'gear_weight', ...fieldnames], v => { const getValue = (id, field) => parseFloat(v[`repeating_gear_${id}_${field}`]) || 0 total += parseFloat(v.weight_total) || 0 weight += parseFloat(v.gear_weight) || 0 weight_r += idArray.reduce((total, id) => total + getValue(id,'gear_weight_r'), 0) }) getAttrs(['weight_total', 'attr_R_weapon-weight', ...fieldnames], v => { const getValue = (id, field) => parseFloat(v[`repeating_rangedweapons_${id}_${field}`]) || 0 total += parseFloat(v.weight_total) || 0 weight += parseFloat(v.gear_weight) || 0 weight_r += idArray.reduce((total, id) => total + getValue(id,'attr_R_weapon-weight'), 0) }) setAttrs({weight_total: weight + weight_r}); }) }) </script>
1585626457

Edited 1585627552
GiGs
Pro
Sheet Author
API Scripter
Your picture doesnt show up for me. It's not possible to say how to do this without seeing the the html for the two repeating sets you want to combine. But it sounds like you'll have to match the name from one repeating set to another, which can be complicated and is prone to error (and very subtle errors that users of the sheet will easily miss) as it depends on users typing the name correctly both times. If players enter names via a select (and so their names are properly constrained to easily recognised values), this will work. If relying on inputs, I wouldnt do it this way: I would require independent entry into each table, and abandon looking up values between them. If you do want to do it though, we can help you. But you'll have to use two getSectionIDs, one inside the other, then a getAttrs inside that, to get the ids of both repeating sections, and we'll then have to create a function to match the name items in one section to the other section, so we can get the matching row ids (they will not be the same) - and handle what happens when there is no match (and with player input this will be common).
1585628046
GiGs
Pro
Sheet Author
API Scripter
By the way, in your macro you have this structure getAttrs(); getAttrs(); setAttrs({weight_total: weight + weight_r}); Because of the async nature of these functions you cant run them one after the other like that. the weight and weight_r attributes will have lost context by the time the setAttrs runs, and the values will be undefined. You have to nest them inside each other. So you'd combine the first two getAttrs, and do them like this getAttrs(     setAttrs(); ); That's also why you need to nest two getSectionIDs inside each other, one for the gear section, and again for the rangedweapons section, and would need to change the idArray name so they each have a different array name.
1585652630
Andreas J.
Forum Champion
Sheet Author
Translator
Jebediah S. said: So the above code is one of Roll20 API's sheet workers. For future reference, we refer to the character sheet's sheetworker as "sheet workers", and "API" is only used to refer to the Pro-subscription feature . Not mixing them up will make it easier in future discussions to know which one you're talking about if the context isn't clear, like in this post.
GiGs said: Your picture doesnt show up for me. It's not possible to say how to do this without seeing the the html for the two repeating sets you want to combine. To prevent posting a wall of text, here is the build's file . Ranged weapons fieldset starts at Line 1117:  < div class =' equipment_ranged_weapons_rep ' > Initial gear amount at Line 1275:  < input name =' attr_gear_quantity ' type =' text ' placeholder =' Amount ' class =' gear_field ' /> Repeating gear amount at Line 1287:  < input name =' attr_gear_quantity_r ' type =' text ' placeholder =' Amount ' class =' gear_field ' /> Initial gear weight at Line 1277:  < input name =' attr_gear_weight ' type =' text ' placeholder =' Weight ' class =' gear_field ' /> Repeating gear weight at Line 1289:  < input name =' attr_gear_weight_r ' type =' text ' class =' gear_field ' /> These two repeating gear values are both part of the same gear fieldset, where Golden Donkey item is listed in the screencap example. Now Let me just say that looking at what you are saying about it makes it seem very complex.  Thank you for your insight on how matching names across fieldsets can become error-prone when working with user input.  I think that unless there are more errors that can apply in the context of what I am trying to achieve, I would prefer to go along with this multi-GetSectionIDs/GetAttrs nested solution. Although What if I made two hidden input fields: each have a sheet worker for them which calculates a value, then I used an auto-calculator field to add the value of both inputs together?  I can disconnect the calculations and have the sheet workers work on their own accord.