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 Workers] Problem with a formula that sums attributes with different multipliers

Hello! I've tried to create a sheet worker that sums different attributes ( ppiu, p, m, l, n ) with different multipliers to calculate the final attribute. on('change:Ppiu change:P change:M change:L change:N', () => { getAttrs(['Ppiu', 'P', 'M', 'L', 'N'], values => { const Ppiu = parseInt(values.Ppiu) || 0; P = parseInt(values.P) || 0; M = parseInt(values.M) || 0; L = parseInt(values.L) || 0; N = parseInt(values.N) || 0; const slot_total = Math.floor(Ppiu / 2)+P+(M*2)+(L*2)+(N*3); }); setAttrs({ slot_total: slot_total }); }); }); I think I made a mistake somewhere. Can someone give me a hand? Thank you!
1740617049

Edited 1740618500
vÍnce
Pro
Sheet Author
Hi LU MASTER, the structure of your sheetworker looks good to me other than you might want to stick to all lowercase attribute names in the event listener. ie on('change: ...')  That could cause problems triggering events in the past but I'm not sure if it's still an issue.  Make sure none of the attributes used in this worker are set to disabled in the HTML? (use readonly instead)  Do the inputs have names, types, and values?  Are these all non-repeating attributes? I'll often use logs in my code when developing to see what's happening ie console.log(`Change Detected:${eventInfo.sourceAttribute}`); Logs will help determine if the listener is being triggered or not and/or if there is an issue further into your worker, like it's not getting all the values from the attributes. Actually there may be an issue with your variable declarations. Try declaring them separately (may work with commas after each instead of ; as well but I like declaring them on separate lines to help easily see what they are) Also, getAttrs parameter 'values' in needs to have parenthesis. ie (values) Try this (remove/comment out logs after you get things working) on('change:ppiu change:p change:m change:l change:n', (event) => { console.log(`Change Detected:${event.sourceAttribute}`); getAttrs(['Ppiu', 'P', 'M', 'L', 'N'], (values) => { const Ppiu = parseInt(values.Ppiu) || 0; const P = parseInt(values.P) || 0; const M = parseInt(values.M) || 0; const L = parseInt(values.L) || 0; const N = parseInt(values.N) || 0; const slot_total = Math.floor(Ppiu / 2)+P+(M*2)+(L*2)+(N*3); console.log(`Ppiu:${Ppiu} P:${P} M:${M} L:${L} N:${N} slot_total:${slot_total}`); setAttrs({ slot_total: slot_total, }); }); });
1740631243

Edited 1740646174
GiGs
Pro
Sheet Author
API Scripter
Are the attributes autocalc attributes? In other words, are they inputs with the disabled property? Sheetworkers cannot modify disabled attribute, nor use them in calculations. Autocalc attributes and sheet workers are incompatible. If they are autocalcs, you need to replace them with sheet workers, or reproduce the calculation to create those attributes in your sheet worker.. PS: as Vince says, you need to change this line to only use lower case letters: on('change:Ppiu change:P change:M change:L change:N', () => { It should be on('change:ppiu change:p change:m change:l change:n', () => { You don't need to change the capitalisation of attribute names anywhere else - just on this line. It may or may not be the source of your current problem, but it is a problem that can cause the sheet worker to randomly fail at some point in the future.
1740631895
GiGs
Pro
Sheet Author
API Scripter
vÍnce said: Actually there may be an issue with your variable declarations. Also, getAttrs parameter 'values' in needs to have parenthesis. ie (values) Your sheet worker is good, but I wanted to point these things out. You can remove line breaks in a sheet worker. I too like to have variable declarations on different lines, but you don't have to. Also the values in getAttrs doesn't need to be in parenthesis (but it can be). You only need parenthesis when you are doing something with the variable or using more than one variable, to make them be treated as one unit. (That's a general rule, not specifically for getAttrs.)
1740633146
vÍnce
Pro
Sheet Author
Thanks GiGs. I probably rely too heavily on eslint to warn me when something might be out of spec. I often follow coding "patterns" that have produced seemingly workable results.  Even if I do not fully understand the how or why.  Ignorance is bliss. ;-P
1740646125

Edited 1740646143
GiGs
Pro
Sheet Author
API Scripter
Ignorance can be bliss, hehe. ESLisnt is great for advice- some of its recommendations will avoid crashes, while others are just for creating a common style so when other people look at your code they see something familiar and can easily spot potential problems. It's a good thing to follow, but some recommendations can be ignored.
1740651350

Edited 1740662453
Thank you GiG's! You help is amazing as always. Your version of the code works! Thank you again!