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

Auto filling with Sheet Workers

1590096676

Edited 1590096699
Eli N.
Pro
Compendium Curator
I am attempting to create a sheet worker which will auto fill several modifiers from the base ability scores. The ability scores come in two parts whole and fractional so a typical strength might be Strength 14 Fractional 22 (each are in their own field with their own name) and some ability scores require both of them to calculate certain values. I am following an example in this link&nbsp; <a href="https://app.roll20.net/forum/post/8401671/how-to-autofill-a-field-by-table-lookup-of-attribute/?pageforid=8403979#post-8403979" rel="nofollow">https://app.roll20.net/forum/post/8401671/how-to-autofill-a-field-by-table-lookup-of-attribute/?pageforid=8403979#post-8403979</a> If possible, I just want to figure out how to get if/then statements to work first, then when I get my feet under me I can preform some maths to make it simpler.&nbsp; Here is the attributes&nbsp; &lt;div class="sheet-col-1-16"&gt;&lt;input type="text" name="attr_strength"/&gt;&lt;/div&gt; &lt;div class="sheet-col-1-16"&gt;&lt;input type="text" name="attr_strength_fractional"/&gt;&lt;/div&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class="sheet-col-1-12"&gt;&lt;input type="number" name="attr_strength_damage_mod" value="0"/&gt;&lt;/div&gt; &lt;div class="sheet-col-1-12"&gt;&lt;input type="number" name="attr_feat_of_strength" value="0"/&gt;&lt;/div&gt; Here is my sheet worker const int = score =&gt; parseInt(score) || 0; on("change:strength change:strength_fractional sheet:opened", function() { &nbsp; &nbsp; getAttrs(["strength","strength_fractional"], function(values) { &nbsp; &nbsp; &nbsp; &nbsp; const str = int(values._strength); &nbsp; &nbsp; &nbsp; &nbsp; const strfrac = int(values.strength_fractional); &nbsp; &nbsp; &nbsp; &nbsp; let strtot&nbsp; = siz + strfrac*.1; &nbsp; &nbsp; &nbsp; &nbsp; console.log(strtot); &nbsp; &nbsp; &nbsp; &nbsp; let strength_damage_mod = 0; &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp; &nbsp; &nbsp; (strtot &lt;= 1.50)&nbsp; strength_damage_mod = -7; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 2.50)&nbsp; strength_damage_mod = -6; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 3.50)&nbsp; strength_damage_mod = -5; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 5.00)&nbsp; strength_damage_mod = -4; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 6.50)&nbsp; strength_damage_mod = -3; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 8.00)&nbsp; strength_damage_mod = -2; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 10.0)&nbsp; strength_damage_mod = -1; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 12.00)&nbsp; strength_damage_mod = 0; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 14.00)&nbsp; strength_damage_mod = 1; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 15.50)&nbsp; strength_damage_mod = 2; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 17.00)&nbsp; strength_damage_mod = 3; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 18.50)&nbsp; strength_damage_mod = 4; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 19.50)&nbsp; strength_damage_mod = 5; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 20.50)&nbsp; strength_damage_mod = 6; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod = 7; &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feat_of_strength: feat_of_strength &nbsp; &nbsp; }); &nbsp; }); }); I also have a question, since Str has several modifiers (damage and feat of strength) when I get both the if/then stuff worked out would the set attribute structure look like this?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod: strength_damage_mod; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;feat_of_strength: feat_of_strength
1590125498

Edited 1590126608
GiGs
Pro
Sheet Author
API Scripter
To answer your last question first, you use commas as separator inside setAttrs, so it would look like this &nbsp;&nbsp;&nbsp;&nbsp;setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod: strength_damage_mod, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;feat_of_strength: feat_of_strength &nbsp;&nbsp;&nbsp;&nbsp;}); Your sheet worker looks like it has a problem here: const str = int(values._strength); This should be const str = int(values.strength); Also the setAttrs at the end should be setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod: strength_damage_mod &nbsp; &nbsp; }); You havent created feat_of_strength yet, so having that there will cause an error. Do you have multiple stats using the same calculation to get the bonus? If so there's a couple of ways to cut down on the duplication. Before recommending which approach: Does each stat have an equivalent to feat_of_strength, or do they have have something that is calculated completely uniquely? Replacing the IF statement In any case, here's a way to replace that if statement: const mods = [1.5, 2.5, 3.5, 5, 6.5, 8, 10, 12, 14, 15.5, 17, 18.5, 19.5, 20.5]; const getMod = score =&gt; (score&gt; 20.5) ? 7 : mods.findIndex(check&nbsp;=&gt;&nbsp;check&nbsp;&gt;=&nbsp;score) -7; const&nbsp;strength_damage_mod =&nbsp; getMod(strtot)&nbsp; First you create an array of the modifier thresholds. Then you use a function called findIndex to check which threshold&nbsp; const getMod = score =&gt; mods.findIndex(check&nbsp;=&gt;&nbsp;check&nbsp;&gt;=&nbsp;score); a modifier falls in. You can call this with const value = getMod(3); and it will tell you which branch the 3 falls in. Arrays have indexes that start at 0, and go up one each time. So 3 would fall in the 3.5-or-less+, that gives an index of 2 (it goes 0, 1, 2, remember).&nbsp; Now we need to add a modifier to that to make it match the modifiers above: -7 drops it that modifier to -5, so updating the function gets us const getMod = score =&gt; mods.findIndex(check&nbsp;=&gt;&nbsp;check&nbsp;&gt;=&nbsp;score) -7; One last thing: we have to handle what if the modifier is off the top of the table. We do that by including a test, is the score above 20.5, if so, set it to 7. That gives s the final form of the function: const getMod = score =&gt; (score &gt; 20.5) ? 7 : mods.findIndex(check&nbsp;=&gt;&nbsp;check&nbsp;&gt;=&nbsp;score) -7; If multiple stats use the same formula, you could put these two lines at the top of your script block (along with the int function): const mods = [1.5, 2.5, 3.5, 5, 6.5, 8, 10, 12, 14, 15.5, 17, 18.5, 19.5, 20.5]; const getMod = score =&gt; (score&gt; 20.5) ? 7 : mods.findIndex(check&nbsp;=&gt;&nbsp;check&nbsp;&gt;=&nbsp;score) -7; and then in your stat sheet workers, just need to use&nbsp; const modifier = getMod(statscore); to get the modifier. Just replace modifier and statscore with the variable names you want to use.
1590345864
Eli N.
Pro
Compendium Curator
Oh oops, I didn't see I had the wrong thing underset attribute. So in all it would look like this&nbsp; on("change:strength change:strength_fractional sheet:opened", function() { &nbsp; &nbsp; getAttrs(["strength","strength_fractional"], function(values) { &nbsp; &nbsp; &nbsp; &nbsp; const str = int(values.strength); &nbsp; &nbsp; &nbsp; &nbsp; const strfrac = int(values.strength_fractional); &nbsp; &nbsp; &nbsp; &nbsp; let strtot&nbsp; = siz + strfrac*.1; &nbsp; &nbsp; &nbsp; &nbsp; console.log(strtot); &nbsp; &nbsp; &nbsp; &nbsp; const mods = [1.5, 2.5, 3.5, 5, 6.5, 8, 10, 12, 14, 15.5, 17, 18.5, 19.5, 20.5]; &nbsp; &nbsp; &nbsp; &nbsp; const getMod = score =&gt; (score&gt; 20.5) ? 7 : mods.findIndex(check =&gt; check &gt;= score) -7; &nbsp; &nbsp; &nbsp; &nbsp; const strength_damage_mod =&nbsp; getMod(strtot)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod: strength_damage_mod &nbsp; &nbsp; }); &nbsp; }); }); Because that did not work for me. I also tried to make the changes you noted in my first code, that also did not work for me. const int = score =&gt; parseInt(score) || 0; on("change:strength change:strength_fractional sheet:opened", function() { &nbsp; &nbsp; getAttrs(["strength","strength_fractional"], function(values) { &nbsp; &nbsp; &nbsp; &nbsp; const str = int(values.strength); &nbsp; &nbsp; &nbsp; &nbsp; const strfrac = int(values.strength_fractional); &nbsp; &nbsp; &nbsp; &nbsp; let strtot&nbsp; = siz + strfrac*.1; &nbsp; &nbsp; &nbsp; &nbsp; console.log(strtot); &nbsp; &nbsp; &nbsp; &nbsp; let strength_damage_mod = 0; &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp; &nbsp; &nbsp; (strtot &lt;= 1.50)&nbsp; strength_damage_mod = -7; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 2.50)&nbsp; strength_damage_mod = -6; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 3.50)&nbsp; strength_damage_mod = -5; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 5.00)&nbsp; strength_damage_mod = -4; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 6.50)&nbsp; strength_damage_mod = -3; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 8.00)&nbsp; strength_damage_mod = -2; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 10.0)&nbsp; strength_damage_mod = -1; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 12.00)&nbsp; strength_damage_mod = 0; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 14.00)&nbsp; strength_damage_mod = 1; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 15.50)&nbsp; strength_damage_mod = 2; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 17.00)&nbsp; strength_damage_mod = 3; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 18.50)&nbsp; strength_damage_mod = 4; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 19.50)&nbsp; strength_damage_mod = 5; &nbsp; &nbsp; &nbsp; &nbsp; else if (strtot &lt;= 20.50)&nbsp; strength_damage_mod = 6; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod = 7; &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod: strength_damage_mod &nbsp; &nbsp; }); &nbsp; }); });
1590346100
GiGs
Pro
Sheet Author
API Scripter
Can you post your full sheet html in pastebin and link it here? Those should work, so I'm wondering if there's something else in the sheet that is interfering.
1590346266
GiGs
Pro
Sheet Author
API Scripter
Oh never mind, I spotted the issue: on("change:strength change:strength_fractional sheet:opened", function() { &nbsp; &nbsp; getAttrs(["strength","strength_fractional"], function(values) { &nbsp; &nbsp; &nbsp; &nbsp; const str = int(values.strength); &nbsp; &nbsp; &nbsp; &nbsp; const strfrac = int(values.strength_fractional); &nbsp; &nbsp; &nbsp; &nbsp; let strtot&nbsp; = siz + strfrac*.1; &nbsp; &nbsp; &nbsp; &nbsp; console.log(strtot); &nbsp; &nbsp; &nbsp; &nbsp; const mods = [1.5, 2.5, 3.5, 5, 6.5, 8, 10, 12, 14, 15.5, 17, 18.5, 19.5, 20.5]; &nbsp; &nbsp; &nbsp; &nbsp; const getMod = score =&gt; (score&gt; 20.5) ? 7 : mods.findIndex(check =&gt; check &gt;= score) -7; &nbsp; &nbsp; &nbsp; &nbsp; const strength_damage_mod =&nbsp; getMod(strtot); &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod: strength_damage_mod &nbsp; &nbsp; }); &nbsp; }); }); Notice you define str , but use siz &nbsp;in the calculation.
1590347283
Eli N.
Pro
Compendium Curator
It is working!
1590347521

Edited 1590347573
Eli N.
Pro
Compendium Curator
So if I wanted to add a feat of strength aspect would it just look like this? (I'll of course change the mods on("change:strength change:strength_fractional sheet:opened", function() { &nbsp; &nbsp; getAttrs(["strength","strength_fractional"], function(values) { &nbsp; &nbsp; &nbsp; &nbsp; const str = int(values.strength); &nbsp; &nbsp; &nbsp; &nbsp; const strfrac = int(values.strength_fractional); &nbsp; &nbsp; &nbsp; &nbsp; let strtot&nbsp; = str + strfrac*.1; &nbsp; &nbsp; &nbsp; &nbsp; console.log(strtot); &nbsp; &nbsp; &nbsp; &nbsp; const mods = [1.5, 2.5, 3.5, 5, 6.5, 8, 10, 12, 14, 15.5, 17, 18.5, 19.5, 20.5]; &nbsp; &nbsp; &nbsp; &nbsp; const getMod = score =&gt; (score&gt; 20.5) ? 7 : mods.findIndex(check =&gt; check &gt;= score) -7; &nbsp; &nbsp; &nbsp; &nbsp; const strength_damage_mod =&nbsp; getMod(strtot); &nbsp; &nbsp; &nbsp; &nbsp; const mods2 = [1.5, 2.5, 3.5, 5, 6.5, 8, 10, 12, 14, 15.5, 17, 18.5, 19.5, 20.5]; &nbsp; &nbsp; &nbsp; &nbsp; const getMod2 = score =&gt; (score&gt; 20.5) ? 7 : mods2.findIndex(check =&gt; check &gt;= score) -7; &nbsp; &nbsp; &nbsp; &nbsp; const feat_of_strength =&nbsp; getMod(strtot); &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod: strength_damage_mod, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feat_of_strength: feat_of_strength &nbsp; &nbsp; }); &nbsp; }); }); Yes! It worked!
1590349252
GiGs
Pro
Sheet Author
API Scripter
Great! You dont actually need the mods2 and getmod2 lines there - see below.&nbsp; on("change:strength change:strength_fractional sheet:opened", function() { &nbsp; &nbsp; getAttrs(["strength","strength_fractional"], function(values) { &nbsp; &nbsp; &nbsp; &nbsp; const str = int(values.strength); &nbsp; &nbsp; &nbsp; &nbsp; const strfrac = int(values.strength_fractional); &nbsp; &nbsp; &nbsp; &nbsp; let strtot&nbsp; = str + strfrac*.1; &nbsp; &nbsp; &nbsp; &nbsp; console.log(strtot); &nbsp; &nbsp; &nbsp; &nbsp; const mods = [1.5, 2.5, 3.5, 5, 6.5, 8, 10, 12, 14, 15.5, 17, 18.5, 19.5, 20.5]; &nbsp; &nbsp; &nbsp; &nbsp; const getMod = score =&gt; (score&gt; 20.5) ? 7 : mods.findIndex(check =&gt; check &gt;= score) -7; &nbsp; &nbsp; &nbsp; &nbsp; const strength_damage_mod =&nbsp; getMod(strtot); &nbsp; &nbsp; &nbsp; &nbsp; const feat_of_strength =&nbsp; getMod(strtot); &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod: strength_damage_mod, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feat_of_strength: feat_of_strength &nbsp; &nbsp; }); &nbsp; }); }); One of the things you can do is create reusable functions, which is what getMod is.&nbsp; You could rewrite the code to let you use that function in other sheet workers, like this: const mods = [1.5, 2.5, 3.5, 5, 6.5, 8, 10, 12, 14, 15.5, 17, 18.5, 19.5, 20.5]; const getMod = score =&gt; (score&gt; 20.5) ? 7 : mods.findIndex(check =&gt; check &gt;= score) -7; on("change:strength change:strength_fractional sheet:opened", function() { &nbsp; &nbsp; getAttrs(["strength","strength_fractional"], function(values) { &nbsp; &nbsp; &nbsp; &nbsp; const str = int(values.strength); &nbsp; &nbsp; &nbsp; &nbsp; const strfrac = int(values.strength_fractional); &nbsp; &nbsp; &nbsp; &nbsp; let strtot&nbsp; = str + strfrac*.1; &nbsp; &nbsp; &nbsp; &nbsp; console.log(strtot); &nbsp; &nbsp; &nbsp; &nbsp; const strength_damage_mod =&nbsp; getMod(strtot); &nbsp; &nbsp; &nbsp; &nbsp; const feat_of_strength =&nbsp; getMod(strtot); &nbsp; &nbsp; &nbsp; &nbsp; setAttrs({ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strength_damage_mod: strength_damage_mod, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feat_of_strength: feat_of_strength &nbsp; &nbsp; }); &nbsp; }); }); Then when you create another sheet worker beneath that one, you can use getMod inside it, without needing to repeat those first two lines.
1590349758
Eli N.
Pro
Compendium Curator
But that would only work if both stats used the same progression
1590350419
GiGs
Pro
Sheet Author
API Scripter
Yes, you're right. I assumed stats would be using the same progression. If not, you are better off keeping those inside the sheet worker.
1590356037
Eli N.
Pro
Compendium Curator
Now is there a way to do a reverse progression? What if I have an ability that goes from 7 to -1 as wisdom goes from 1 to 20
1590387030
GiGs
Pro
Sheet Author
API Scripter
There's a way, but you;ll need to share the actual progression.
1590423605
Eli N.
Pro
Compendium Curator
I just did it like normal, and then multiplied the output by -1
1590424140
GiGs
Pro
Sheet Author
API Scripter
So its the same progression just in reverse? yes, that'll do it.&nbsp;
1590425867
Eli N.
Pro
Compendium Curator
Yeah I fit the progression to that stat, and then multiplied it by -1. Each ability score had around 3 different stats they influenced with 3 different progressions. I got it all working now. Thanks for that help.&nbsp;
1590426149
GiGs
Pro
Sheet Author
API Scripter
you're welcome :)