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

Sheetworker Javascript Template Literals Help

1540156150

Edited 1540156185
I am trying to condense a long section of code, but the full code below does not work. Other sheetworkers stop working too, so I think it's a syntax error. Probably something with the template literals I am using without being completely versed in their usage. const spellclasses = ["spellclass01", "spellclass02"]; const spelllevels = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09"]; const spellcountAttrs = (_.map(spellclasses,(c)=>`${c}_castertype`) + ',' + _.flatten(_.map(spellclasses,(c)=>_.map(spelllevels, (s)=>`${c}_spells${s}_count_base`))) + ',' + _.flatten(_.map(spellclasses,(c)=>_.map(spelllevels, (s)=>`${c}_spells${s}_count_ability`))) + ',' + _.flatten(_.map(spellclasses,(c)=>_.map(spelllevels, (s)=>`${c}_spells${s}_count_extra`)))); const spellcountEvents = _.map((spellcountAttrs.split(",")),(a)=>`change:${a}`); on(`${spellcountEvents.join(' ')}`, function() { getAttrs(spellcountAttrs, function (value) =>{ let update = {}; _.each((spellclasses, c)=>{ _.each((spelllevels, s)=>{ if (value[`${c}_spells${s}_count_base`] == "-") { update[`${c}_spells${s}_count_max`] = 0; } else { update[`${c}_spells${s}_count_max`] = (parseInt(value[`${c}_spells${s}_count_base`],10) || 0) + (parseInt(value[`${c}_spells${s}_count_ability`],10) || 0) + (parseInt(value[`${c}_spells${s}_count_extra`],10) || 0); }; }); }); setAttrs(update); }); }); I know that the problem is somewhere in the bolded code above. When I remove that section and put in a console.log(); command, it shows in the console when one of the right attributes from spellcountEvents is changed. Thanks in advance for any assistance/advice/pointing out of errors.
1540157922
Jakob
Sheet Author
API Scripter
If you suspect a syntax error, use a syntax checker (or even better, have a syntax checker in your editor, it's easy with e.g. VSCode). Anyway, this here's your problem: getAttrs(spellcountAttrs, function (value) =>{
1540159937
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
To add to Jakob's answer, use the Google closure compiler . And to expand what is wrong, you are mixing fat arrow syntax and function syntax.
Thank you both. I used a JSLint site to check the code and it did not catch the arrow error. I will use the Google one now. Now other sheetworkers continue to function on the sheet, but this code is not updating the attributes it's supposed to. Logging seems to indicate it does not even get into the getAttrs function, and a new error is showing in the console: TypeError: right-hand side of 'in' should be an object, got string [Learn More] Any thoughts/insights?
1540258531
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
This is actually something I wasn't sure of earlier. I'm not sure your spellcountAttrs is giving what you think it is. Do some logging of that to make sure, but I'm pretty sure. const spellcountAttrs = (_.map(spellclasses,(c)=>`${c}_castertype`) + ',' + _.flatten(_.map(spellclasses,(c)=>_.map(spelllevels, (s)=>`${c}_spells${s}_count_base`))) + ',' + _.flatten(_.map(spellclasses,(c)=>_.map(spelllevels, (s)=>`${c}_spells${s}_count_ability`))) + ',' + _.flatten(_.map(spellclasses,(c)=>_.map(spelllevels, (s)=>`${c}_spells${s}_count_extra`)))); Even assuming that your _.maps are working as anticipated, you are then adding it to a string (','). This then creates the string "[object],". And this concatenation occurs with each one.
1540264786

Edited 1540264963
Logging on sheet:opened to give me spellcountAttrs is giving me what I expect to see. spellclass01_castertype,spellclass02_castertype,spellclass01_spells00_count_base,spellclass01_spells01_count_base,spellclass01_spells02_count_base, spellclass01_spells03_count_base,spellclass01_spells04_count_base,spellclass01_spells05_count_base,spellclass01_spells06_count_base,spellclass01_spells07_count_base, spellclass01_spells08_count_base,spellclass01_spells09_count_base,spellclass02_spells00_count_base,spellclass02_spells01_count_base,spellclass02_spells02_count_base, spellclass02_spells03_count_base,spellclass02_spells04_count_base,spellclass02_spells05_count_base,spellclass02_spells06_count_base,spellclass02_spells07_count_base, spellclass02_spells08_count_base,spellclass02_spells09_count_base,spellclass01_spells00_count_ability,spellclass01_spells01_count_ability,spellclass01_spells02_count_ability, spellclass01_spells03_count_ability,spellclass01_spells04_count_ability,spellclass01_spells05_count_ability,spellclass01_spells06_count_ability, spellclass01_spells07_count_ability,spellclass01_spells08_count_ability,spellclass01_spells09_count_ability,spellclass02_spells00_count_ability, spellclass02_spells01_count_ability,spellclass02_spells02_count_ability,spellclass02_spells03_count_ability,spellclass02_spells04_count_ability, spellclass02_spells05_count_ability,spellclass02_spells06_count_ability,spellclass02_spells07_count_ability,spellclass02_spells08_count_ability, spellclass02_spells09_count_ability,spellclass01_spells00_count_extra,spellclass01_spells01_count_extra,spellclass01_spells02_count_extra, spellclass01_spells03_count_extra,spellclass01_spells04_count_extra,spellclass01_spells05_count_extra,spellclass01_spells06_count_extra, spellclass01_spells07_count_extra,spellclass01_spells08_count_extra,spellclass01_spells09_count_extra,spellclass02_spells00_count_extra, spellclass02_spells01_count_extra,spellclass02_spells02_count_extra,spellclass02_spells03_count_extra,spellclass02_spells04_count_extra, spellclass02_spells05_count_extra,spellclass02_spells06_count_extra,spellclass02_spells07_count_extra,spellclass02_spells08_count_extra, spellclass02_spells09_count_extra This is the same set of attributes I use in the longer, more explicit code. Two spellclasses, and for each class a _base attribute, an _ability attribute, and an _extra attribute for each of 10 spell levels (0-9). Perhaps there is a more elegant way to do this. For D&D 3.5, I am generating max spells by level that can be prepped by day (for ex: spellclass02_spells04_count_max would be the max number of 4th level spells the character could prep for their 2nd spellcasting class). I want to account for Bards, Paladins, and Rangers that have levels where they have a base of 0 spells, but can prep spells if they have an ability bonus. In the spells per day grid in the sheet, the _base value would contain a "-" if the character could not use that spell level at all, and would have "0" if they could spells with an ability bonus. I think I see what you mean: While this works for getAttrs in the comma-separated text above, it needs to be an array for the template literals inside the _.each?
1540267181

Edited 1540267339
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
You're problem is that that's a single string of all those, rather than an array of all of those strings. In your getAttrs call change spellcountattrs to spellcountAttrs.split(','). Edit: at least I'm pretty sure that's what is going on. You could log typeof spellcountattrs to be sure.