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] So I made a function and I'd like to know if it's useless or not

1616326517

Edited 1616327018
CKenny
Sheet Author
Hello everyone, Like mentioned in the title, I made a function to get attributes from a repeating section in the form of an object, and I was doubting that I am the first person to have this idea So anyway here is the function : /** * get the attributes from a repeating section and pass them in an array of objects * @param {String} section - name of the repeating section * @param {Array} fields - list of the attributes * @param {Function} callback - function to pass the array of objects */ const getSectionObjects = function (section, fields, callback) {     if (!Array.isArray(fields)) fields = [fields];     getSectionIDs(`repeating_${section}`, idArray => {         const attrArray = idArray.reduce( (m,id) => [...m, ...(fields.map(field => `repeating_${section}_${id}_${(!Array.isArray(field)) ? field : field[1]}`))],[]);         getAttrs(attrArray, v => {     const objects = [];     idArray.forEach(id => {         let obj = {}; fields.forEach( field => {     if (!Array.isArray(field)) field = [field, field]     let [key, name] = field;     obj[`${key}`] = v[`repeating_${section}_${id}_${name}`]; }); objects.push(obj)     });     callback(objects);         });     }); } // a structure example const GEAR = [ ['carry','gear_carry_r'], ['name','gear_name_r'], ['craftsmanship','gear_crafts_r'], ['quantity','gear_quantity_r'], ['availability','gear_avail_r'], ['weight','gear_weight_r'] ] // to test if it's working on('sheet:opened', () => {getSectionObjects('gear', GEAR, values => {console.log(values)})}); What do you think? Is it useless, does it need to be optimised?
1616376347
GiGs
Pro
Sheet Author
API Scripter
If it works, its definitely not useless. How do you see people using it? Roll20 sheets tend to be very ad-hoc, and authors will use any techniques that work if you a show a good use case, but there are no standardised design patterns so everyone has to basically figure out things for themselves. A good example of how to set up the script block to use, and ways to use it once you have the object would definitely help.
1616417468

Edited 1616417920
CKenny
Sheet Author
well one example that I am thinking to use is to filter the results easily with the Array.filter() method here is an example to calculate the inventory weight where you have a carried option const GEAR = [ ['carry','gear_carry_r'], ['name','gear_name_r'], ['craftsmanship','gear_crafts_r'], ['quantity','gear_quantity_r'], ['availability','gear_avail_r'], ['weight','gear_weight_r'] ] // calculate inventory weight on('sheet:opened change:repeating_gear:carry ...', () => {getSectionObjects('gear', GEAR, items => {     setAttrs({ weight_total: items.filter(item => item.carry).reduce((total, item) => total + (item.weight * item.quantity),0), }); })}); because if you do not carry something it should not weight you down
1616476680
GiGs
Pro
Sheet Author
API Scripter
I can think of ways to use it and it looks useful. I was hoping you'd post a few practical (and real) examples to show people how to use it, targeted at people who dont know much javascript (most sheet authors). If you just post the function, it will get largely ignored, because most people wont know how to exploit it.
1616496204
CKenny
Sheet Author
yeah I wish I could post a few practical (and real) applications, but unfortunately I am on another project right now. the function just popped in my mind while I was working on a JSON file I will try to post concrete application once I have the time to experiment with the function
1616522039
Andreas J.
Forum Champion
Sheet Author
Translator
Adding them to one of the the sheetworker wiki pages with examples could be a good place, so they stay more easy to find. The forums search function is not great, and after your thread falls of the subforum's first page, few people will able to find your advice