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

Calculating the quantity*value total on a complex inventory sheetworker

Hello, I used a tutorial on how to build an inventory sheetworker, and it works fine but my problem is I don't really understand the mechanisms and I need modify something. const section_attribute = (section, id, field) => `repeating_${section}_${id}_${field}`; on('change:repeating_inventaire:prix change:repeating_inventaire:poids change:repeating_inventaire:quantite change:repeating_inventaire:emplacement sheet:opened', () => {    getSectionIDs('repeating_inventaire', id_inventaire => {       const fields = [];       id_inventaire.forEach(id => {          fields.push(section_attribute('inventaire', id, 'prix'));          fields.push(section_attribute('inventaire', id, 'poids'));          fields.push(section_attribute('inventaire', id, 'quantite'));          fields.push(section_attribute('inventaire', id, 'emplacement'));       });       getAttrs(fields, values => {          const output = {};          const stats = ['prix', 'porté', 'sac', 'sacrapide', 'coffre', 'bandoulière', 'sacoche', 'carquois', 'monture', 'ceinture', 'poids'];          stats.forEach(stat => output[`total_${stat}`] = 0);          id_inventaire.forEach(id => {             const poids = +values[section_attribute('inventaire', id, 'poids')] || 0;             const quantite = +values[section_attribute('inventaire', id, 'quantite')] || 0;             const emplacement = values[section_attribute('inventaire', id, 'emplacement')];             output[`total_${emplacement.toLowerCase()}`] += poids * quantite;             output.total_prix += (+values[section_attribute('inventaire', id, 'prix')] || 0);          });          stats.forEach(stat => {             if(stat != 'prix' && stat != 'poids') {                 output.total_poids += output[`total_${stat}`];             }          });          setAttrs(output);       });    }); }); I would like my total_prix (the total value) to show the addition of each item's  quantite*prix value, but it only shows the addition of all item's prix, without taking into account the quantite of items i have for each row. I don't know if that's clear but basically my sheetworker doesn't take into account the number of objects when calculating the global value of the inventory. It does it for the total weight and separates it into the containers, which is perfect, but I don't know what to modify in the code and how to have my quantite*prix taken into account. Thank you for reading me !
1704898174

Edited 1704912818
GiGs
Pro
Sheet Author
API Scripter
It sounds like you need to change this section id_inventaire.forEach(id => {             const poids = +values[section_attribute('inventaire', id, 'poids')] || 0;             const quantite = +values[section_attribute('inventaire', id, 'quantite')] || 0;             const emplacement = values[section_attribute('inventaire', id, 'emplacement')];             output[`total_${emplacement.toLowerCase()}`] += poids * quantite;             output.total_prix += (+values[section_attribute('inventaire', id, 'prix')] || 0); }); If I understand what you're trying to do, this should do it. id_inventaire.forEach(id => {             const poids = +values[section_attribute('inventaire', id, 'poids')] || 0;             const quantite = +values[section_attribute('inventaire', id, 'quantite')] || 0;             const prix = + values[section_attribute('inventaire', id, 'prix')] || 0 ;             const emplacement = values[section_attribute('inventaire', id, 'emplacement')];             output[`total_${emplacement.toLowerCase()}`] += poids * quantite;             output.total_prix += prix * quantite; });
1704913083

Edited 1704916652
GiGs
Pro
Sheet Author
API Scripter
By the way, that section gets the emplacement value, but doesn't use it for anything. If you aren't planning to use that, you can remove it from there, and everywhere else its mentioned in this worker. Ignore my previous text - I see where it used now :)
Yes thats exactly what I wanted ! Thank you very much !