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

Get values from auto-calc input

1611585469

Edited 1611586279
Hi, I have 2 questions about calculating in sheet-worker ! 1. I have an input that auto-calc a lot of number. Is it possible to get the input value in sheet worker? (like formula = 1 + 3, so I want to get "4" as value. I only get the formula itself). 2. If not, how can I get the sum of every number (that is store in the same number of input (let's say 10 inputs)) ? I tried the example in the wiki with the var.forEach() loop but it only get the value of each field and set it. In this case, what I want is that we get the values of every field, add it to a variable and set the final variable to input. Exemple const = ["for", "dex", "int", "agi", "const"] total = 0 on change:for ... change:const get every values from for, dex, int, agi, const total = sum of for, dex, int, agi, const set total to an input (outside the loop)
1611596859
GiGs
Pro
Sheet Author
API Scripter
autocalc inputs are not compatible with sheet workers. What youre asking for is probably covered in the Universal Sheet Workers entry in the wiki, but it's not clear.  Do you just want to add the 5 stats for, dex, int, agi, and const together? Or do you want to construct the for, dex, etc stats: if so, what re they constructed from?
I want to add the 5 stats (in reality, there are 30) together and set the total to an other input. The update must be done after each changes. I'm looking for a loop because I don't want to write the 30 differents input manually ^^'
1611597239

Edited 1611602886
GiGs
Pro
Sheet Author
API Scripter
If you just want to add those five (or more) stats, here's a sheet worker that will do it:     const stats = ['for', 'dex', 'int', 'agi', 'const'];     const buildChanges = arr => arr.reduce((changes, stat) => `${changes} change:${stat}`, '');     on(buildChanges(stats), () => {         getAttrs(stats, values => {             const sumValues = Object.values(values).reduce((a, b) => a + (+b|| 0), 0);             setAttrs({                 total: sumValues             });         });     }); I'd normally spend more time explaining, but I'm rushed today. You can add as many attribute names to the stats array at the start as you want, but they must be valid attribute names, and they cant be autocalcs. change total (inside setAttrs) to the attribute name you want to save the total to.
1611602573
GiGs
Pro
Sheet Author
API Scripter
I just tweaked the code a bit, in case you already tried it.
GiGs said: I just tweaked the code a bit, in case you already tried it. Hi @GiGs, yes I already tried it and tweaked it too. Thank you, it works !
1611605141
GiGs
Pro
Sheet Author
API Scripter
Great! I tweaked it mainly to add this line: const buildChanges = arr => arr.reduce((changes, stat) => `${changes} change:${stat}`, ''); In case you needed more sheet workers to add up bunches of stats. This above line is a reusable function. You can move it up the start of your sheet worker script block (just after the type="text/worker") line, and then can use it in multiple sheet workers. Like, if you had another set of stats to add together, you could do const morestats = ["stat1","stat2", "stat3"];     on(buildChanges(morestats), () => {         getAttrs(morestats, values => {             const sumValues = Object.values(values).reduce((a, b) => a + (+b|| 0), 0);             setAttrs({                 total: sumValues             });         });     }); Any sheet worker where you have a bunch of stats, you can use that buildChanges function instead of typing out a long list of  'change:stat1 change:stat2   change:stat3"  etc.
Yes, thank you ! I'll keep it in case of ;)