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 .
×
May your rolls be merry + bright! 🎄
Create a free account

Getting values of all repeating sections through getAttrs()

Basic idea: output all skill names (or whatever else) from a repeating section as an attr_ that can be placed in a span. Been working on this for the last day and can't get it to work.  The following should work according to all documentation I've seen, but doesn't: <div>     <span name="attr_skill_list"></span><br>     <fieldset class="repeating_skills">                      <select name="attr_dtype" class="dtype">                               <option value="d4">d4</option>               <option value="d6">d6</option>               <option value="d8">d8</option>               <option value="d10">d10</option>               <option value="d12">d12</option>           </select>           <input type="text" name="attr_skillname" />             </fieldset> </div> <script type="text/worker"> on("change:repeating_skills remove:repeating_skills", function() {     getSectionIDs("skillname", function(idarray) {         var text = "";         for(var i=0; i < idarray.length; i++) {             //Do something with the IDs             getAttrs(["repeating_skills_" + idarray[i] + "_skillname"], function(values) {                 text = text + values["repeating_skills_" + idarray[i] + "_skillname"];             });                     }         setAttrs({skill_list:text});     }); }); // ... etc </script> The above is the entire charsheet (no css, nothing else that can mess with the functionality).  The issue is definitely in the getAttrs section (even if I replace values["..."] with "hello" or String(idarray[i]), it fails to do anything.  According to the sheetworkers documentation: On the other hand, if you aren't currently in a repeating section, you can specifically request that value of a field in a repeating section row by specifying its ID manually: getAttrs(["repeating_spells_-ABC123_SpellDamage"]... And I'm trying to do so, but it fails.  How does one do this?
1513707853

Edited 1513707877
Lithl
Pro
Sheet Author
API Scripter
getAttrs is asynchronous. By the time you're calling setAttrs, the callback in getAttrs may not have even begun yet. Try something like this: on('change:repeating_skills remove:repeating_skills', () => {   getSectionIDs('skillname', (idarray) => {     getAttrs(idarray.map((id) => `repeating_skills_${id}_skillname`), (values) => { setAttrs({ skill_list: idarray.map((id) => values[`repeating_skills_${id}_skillname`]).join(''), });     });   }); });
That worked!  Thank You!  If I wanted to include other values, do I do another getAttrs, or is there a way to include more than one value in the  getAttrs(idarray.map((id) => `repeating_skills_${id}_skillname`) part?
1513719160
Jakob
Sheet Author
API Scripter
The most economical way to get multiple values would be an expression like [ ...idarray.map((id) => `repeating_skills_${id}_skillname`), ...idarray.map((id) => `repeating_skills_${id}_skillbonus`), ...idarray.map((id) => `repeating_skills_${id}_skillblah`), // ... ] (the above would be the first argument of getAttrs).
Thank you both for the help, this will be great.
1513728533

Edited 1513730613
Okay, so changed my sheet to this   <div>     <span style="margin-left: 20px;"><b>Skills: </b><span name="attr_npc_skills" style="font-style: italic;"></span>     <h3>- Skills -</h3>             <fieldset class="repeating_skills">                 <input type="checkbox" class="skill-edit-btn">                 <span class="skill-edit">                     <input type="text" name="attr_skillname" class="skill-name" value="Skill Name"></input>                     <select name="attr_skill_roll" class="skill-roll">                         <option value="S/D">S/D</option>                         <option value="S/F">S/F</option>                         <option value="W">W</option>                     </select>                     <select name="attr_skill_level" class="skill-rank">                         <option value="Trained">Trained</option>                         <option value="Apprentice">Apprentice</option>                         <option value="Journeyman">Journeyman</option>                         <option value="Master">Master</option>                     </select>                 </span>                 <span class="skill-display">                     <span name="attr_skillname" class="sp-skill-name"></span>                     <span name="attr_skill_roll" class="sp-skill-roll"></span>                     <span name="attr_skill_level" class="sp-skill-rank"></span>                 </span>             </fieldset> </div> Since it's what's going in my real sheet.  This works: <script type="text/worker"> on('change:repeating_skills remove:repeating_skills', () => {   getSectionIDs('repeating_skills', (idarray) => {     getAttrs(idarray.map((id) => `repeating_skills_${id}_skillname`), (values) => {       setAttrs({         npc_skills: idarray.map((id) => values[`repeating_skills_${id}_skillname`]).join(', '),       });     });   }); }); // ... etc </script> And gives the name of each skill like this:   Attack, Archery Of course, it's in the same order the skills were added to the sheet, not the order you dragged them to, but that's fine. This does NOT work: <script type="text/worker"> on('change:repeating_skills remove:repeating_skills', () => {   getSectionIDs('repeating_skills', (idarray) => {     getAttrs([idarray.map((id) => `repeating_skills_${id}_skillname`),               idarray.map((id) => `repeating_skills_${id}_skill_level`)], (values) => {       setAttrs({         npc_skills: idarray.map((id) => values[`repeating_skills_${id}_skillname`]).join(', '),       });     });   }); }); // ... etc </script> What am I doing wrong this time? Goal: if skill_level is Apprentice, Journeyman, Master, list skill as "name [A]" else just as "name" (can't grab both sets of values, so can't even start with logic yet. EDIT: The following works, but it requires nesting.  I'll likely leave it be, but in case you're finding this on a searchL on('change:repeating_skills remove:repeating_skills', () => {     getSectionIDs('repeating_skills', (idarray) => {         getAttrs(idarray.map((id) => `repeating_skills_${id}_skillname`), (values) => {             getAttrs(idarray.map((id) => `repeating_skills_${id}_skill_level`), (levels) => {                 var skills = '';                 var level = '';                 for(var i = 0; i < idarray.length; i++ ){                     skills += idarray.map((id) => values[`repeating_skills_${id}_skillname`])[i];                     level = idarray.map((id) => levels[`repeating_skills_${id}_skill_level`])[i];                     lev = level.charAt(0);                     // higher level                     if (lev != 'T') {                         skills += ' [' + lev + ']'                     }                     // need a comma...                     if (i < idarray.length - 1) {                             skills += ", ";                     }                                      }                 setAttrs({npc_skills: skills});             });         });     }); });
1513758288

Edited 1513763960
Jakob
Sheet Author
API Scripter
The problem was that you forgot the "..." in front of idarray. That's the spread operator, which inserts the contents of the idarray.map(blabla) array into the surrounding array - without the spread it's a 2-element array, where each element is again an array, and getAttrs doesn't know what to do with it. EDIT: By the way, you can use getSectionIDsOrdered() from the  wiki to get the IDs in display order instead, if you want to.
Thank you!