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 Help - Building values for setAttrs

So I have this HTML on my sheet: <select name="attr_spellclass01_name" title="spellclass01_name"> <option value="Spellcasting Class 1" selected>None</option> <option value="class01">Class 1</option> <option value="class02">Class 2</option> <option value="class03">Class 3</option> <option value="class04">Class 4</option> <option value="class05">Class 5</option> <option value="class06">Class 6</option> </select> Elsewhere on the sheet, attributes like class01_name, class01_level, class02_name, class02_level, etc., are defined by inputboxes. I wish to set other attributes (spellclass01_display and spellclass01_casterlevel) based on the values of the attribute related to the select choice above. So if  "class02_name" was "Wizard" and "class02_level" was "5" selecting Class 2 would set these other attributes to "Wizard" and "5" respectively. I have tried numerous ways in my sheetworker code to get this to work with no success. I just don't know the right syntax for building and assigning the value, so I will stop flailing and guessing and ask for help. Sheetworker code: on("sheet:opened change:spellclass01_name", function() { getAttrs(["spellclass01_name"], function(value) { switch(value.spellclass01_name) { case "Spellcasting Class 1": setAttrs({ "spellclass01_display": "Spellcasting Class 1", "spellclass01_casterlevel": "0" }); break; default: setAttrs({ "spellclass01_display": value[spellclass01_name + "_name"], "spellclass01_casterlevel": value[spellclass01_name + "_level"] }); } }); }); Any help greatly appreciated. Thanks!
1485744858
Lithl
Pro
Sheet Author
API Scripter
Any attribute you try to get from the value  parameter (eg, value[spellclass01_name + "_name"] ) must be in the first parameter of the getAttrs  function call. So if you want to be able to get class01_name, your getAttrs needs to include it. Also, in your code spellclass01_name is not a variable to use for indexing the value object, it's the name of one of the value  object's properties. You would instead need to use something like value[value.spellclass01_name + "_name"] . On the first point, this does mean that you need to calculate all of the attribute names you need. Fortunately, it appears your attribute names are named consistently, so you could build that array programatically. Something along the lines of: var MAX_CLASSES = 10, // adjust to correct number DIGITS = Math.floor(Math.log10(MAX_CLASSES)) + 1, attributesToGet = _.chain(_.range(1, MAX_CLASSES)) .map((n) => [ `spellclass${n.pad(DIGITS)}_name`, `class${n.pad(DIGITS)}_name`, `class${n.pad(DIGITS)}_level` ]).flatten() .value(); Number.prototype.pad = function(size) { var s = String(this); while (s.length < size) s = `0${s}`; return s; }; attributesToGet would then be the array ['spellclass01_name', 'class01_name', 'class01_level', 'spellclass02_name', 'class02_name', 'class02_level', ..., 'spellclass10_name', 'class10_name', 'class10_level']
Many thanks, Brian! Seems my problem was more complicated than I thought. I will have to take some time to absorb, understand and incorporate this. Thanks again!