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 .
×

Problems with Sheet Worker script

Hi all, recently I wanted to update Charsheet for Fallout PnP with some macroses/calculations and got stuck adding Sheet Workers. I use adopted version of the script from this thread -&nbsp; <a href="https://app.roll20.net/forum/post/5876049/sum-total-of-column-in-repeating-fields" rel="nofollow">https://app.roll20.net/forum/post/5876049/sum-total-of-column-in-repeating-fields</a> . I want to get item_total_weight from repeating sections, SUM it and add to the header table in attribute @{current_carry_weight}. Obviously, script does not work due to my failing hands.dll. Here is a html page code &lt;div class="sheet-tab-content sheet-tab5"&gt; &lt;h3 style="width:98%"&gt;Inventory&lt;/h3&gt; &lt;table class="sheet-center" style="width:96%"&gt; &lt;tr&gt; &lt;td&gt;Maximum&lt;br&gt;Carry Weight&lt;/td&gt; &lt;td colspan="2"&gt;&lt;input type="number" class="sheet-number" name="attr_max_carry_weight" value='(@{carry_weight})' disabled="true" /&gt;&lt;/td&gt; &lt;td&gt;Current&lt;br&gt;Carry Weight&lt;/td&gt; &lt;td colspan="2"&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_current_carry_weight" value="0" readonly="true" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br&gt; &lt;fieldset class="repeating_inventory"&gt; &lt;table class="sheet-center" style="width:96%"&gt; &lt;tr&gt; &lt;td&gt;Item&lt;/td&gt; &lt;td&gt;Weight&lt;/td&gt; &lt;td&gt;Amount&lt;/td&gt; &lt;td&gt;Cost&lt;/td&gt; &lt;td&gt;Total Weight&lt;/td&gt; &lt;td&gt;Total Cost&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" class="sheet-number" name="attr_item_name" style="width:100px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_item_weight" value="0" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" name="attr_item_amount" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_item_cost" value="0" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_item_total_weight" value='(@{item_weight}*@{item_amount})' style="width:60px" disabled="true" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_item_total_cost" value='(@{item_cost}*@{item_amount})' style="width:60px" disabled="true" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="6"&gt;&lt;textarea name="attr_item_text" style="width:97%" &gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br&gt; &lt;/fieldset&gt; &lt;/div&gt; Here is worker script &lt;script type="text/worker"&gt; on("change:repeating_inventory:item_weight remove:repeating_inventory", function() { getSectionIDs("repeating_inventory", function(IDArray) { var fieldNames = []; for (var i=0; i &lt; IDArray.length; i++) { fieldNames.push("repeating_inventory_" + IDArray[i] + "_item_total_weight"); } var total = 0; getAttrs(fieldNames, fieldAmount function(values) { for (var i=0; i &lt; IDArray.length; i++) { total += parseInt(values["repeating_inventory_" + IDArray[i] + "_item_total_weight"])||0; } setAttrs({ "current_carry_weight": total }); }); }); }); &lt;/script&gt; Could you please tell me what am I doing wrong with it?)
1585625803

Edited 1585625858
GiGs
Pro
Sheet Author
API Scripter
You are using autocalc fields in the total_weight entry. Sheet workers dont work with autocalc fields, you;ll need to replace that with another sheet worker, or (better) combine them into a single sheet worker, so you have one that calclates the totals in the row, and the total for the whole worker.&nbsp; There's a few syntax errors too. Here's a version that should work (barring any typos): on('change:repeating_inventory:item_weight&nbsp;change:repeating_inventory:item_cost&nbsp;change:repeating_inventory:item_amount&nbsp;remove:repeating_inventory',&nbsp;function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;getSectionIDs('repeating_inventory',&nbsp;function(IDArray)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;fieldNames&nbsp;=&nbsp;[]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(var&nbsp;i=0;&nbsp;i&nbsp;&lt;&nbsp;IDArray.length;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fieldNames.push('repeating_inventory_'&nbsp;+&nbsp;IDArray[i]&nbsp;+&nbsp;'_item_weight'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fieldNames.push('repeating_inventory_'&nbsp;+&nbsp;IDArray[i]&nbsp;+&nbsp;'_item_cost'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fieldNames.push('repeating_inventory_'&nbsp;+&nbsp;IDArray[i]&nbsp;+&nbsp;'_item_amount'); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getAttrs(fieldNames,&nbsp;function(values)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;since&nbsp;we&nbsp;are&nbsp;doing&nbsp;the&nbsp;whole&nbsp;repeating&nbsp;section,&nbsp;we&nbsp;need&nbsp;a&nbsp;variable&nbsp;to&nbsp;hold&nbsp;the&nbsp;values&nbsp;of&nbsp;each&nbsp;row &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;output&nbsp;=&nbsp;{}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;we&nbsp;also&nbsp;need&nbsp;to&nbsp;count&nbsp;the&nbsp;total&nbsp;weight&nbsp;as&nbsp;we&nbsp;go &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;total&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(var&nbsp;i=0;&nbsp;i&nbsp;&lt;&nbsp;IDArray.length;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;we&nbsp;need&nbsp;to&nbsp;go&nbsp;through&nbsp;each&nbsp;row,&nbsp;and&nbsp;get&nbsp;the&nbsp;weight,&nbsp;cost,&nbsp;and&nbsp;amount &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;and&nbsp;calculate&nbsp;the&nbsp;totals&nbsp;for&nbsp;that&nbsp;row &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;weight&nbsp;=&nbsp;parseFloat(values['repeating_inventory_'&nbsp;+&nbsp;IDArray[i]&nbsp;+&nbsp;'_item_weight'])&nbsp;||&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;cost&nbsp;=&nbsp;parseFloat(values['repeating_inventory_'&nbsp;+&nbsp;IDArray[i]&nbsp;+&nbsp;'_item_cost'])&nbsp;||&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;amount&nbsp;=&nbsp;parseInt(values['repeating_inventory_'&nbsp;+&nbsp;IDArray[i]&nbsp;+&nbsp;'_item_amount'])&nbsp;||&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;calculate&nbsp;the&nbsp;totals&nbsp;for&nbsp;THIS&nbsp;row &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;row_weight&nbsp;=&nbsp;weight&nbsp;*&nbsp;amount; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;row_cost&nbsp;=&nbsp;cost&nbsp;*&nbsp;amount; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;save&nbsp;those&nbsp;values&nbsp;for&nbsp;updating&nbsp;the&nbsp;sheet&nbsp;later &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output['repeating_inventory_'&nbsp;+&nbsp;IDArray[i]&nbsp;+&nbsp;'_item_total_weight']&nbsp;=&nbsp;row_weight; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output['repeating_inventory_'&nbsp;+&nbsp;IDArray[i]&nbsp;+&nbsp;'_item_total_cost']&nbsp;=&nbsp;row_cost; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;update&nbsp;the&nbsp;total&nbsp;weight &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total&nbsp;+=&nbsp;row_weight; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;now&nbsp;we&nbsp;have&nbsp;looped&nbsp;through&nbsp;the&nbsp;entire&nbsp;set,&nbsp;we&nbsp;need&nbsp;to&nbsp;save&nbsp;the&nbsp;total&nbsp;weight &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output['current_carry_weight']&nbsp;=&nbsp;total; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;now&nbsp;we&nbsp;can&nbsp;save&nbsp;the&nbsp;stats&nbsp;to&nbsp;the&nbsp;sheet. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setAttrs(output); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;}); }); VERY IMPORTANT: delete the disabled=true &nbsp;section from each of your inputs. Sheet workers cant update those types of inputs. You can remove the contents of the value="" section too, but this will overwrite whatever's there anyway.
Thanks a lot, GiGs, the script you provided works perfectly. Also will keep in mind the limitations you stated.
1585637629
GiGs
Pro
Sheet Author
API Scripter
Glad to hear it :)
1586034029

Edited 1586035517
I'm afraid I need help with scripts once again( I have 2 repeating section for personal equipment and for vehicle equipment. For character equipment - &lt;fieldset class="repeating_inventory"&gt; &lt;table class="sheet-center" style="width:1100px"&gt; &lt;tr&gt; &lt;td&gt;Item Name&lt;/td&gt; &lt;td&gt;Weight&lt;/td&gt; &lt;td&gt;Amount&lt;/td&gt; &lt;td&gt;Cost&lt;/td&gt; &lt;td&gt;Total Weight&lt;/td&gt; &lt;td&gt;Total Cost&lt;/td&gt; &lt;td&gt;Notes&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" class="sheet-number" name="attr_item_name" title="item_name" style="width:300px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_item_weight" title="item_weight" value="0" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" name="attr_item_amount" title="item_amount" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_item_cost" title="item_cost" value="0" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_item_total_weight" title="item_total_weight" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_item_total_cost" title="item_total_cost" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="sheet-number" name="attr_item_notes" title="item_notes" style="width:400px" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br/&gt; &lt;/fieldset&gt; For vehicle equipment - &lt;fieldset class="repeating_vehicle_inventory"&gt; &lt;table class="sheet-center" style="width:1100px"&gt; &lt;tr&gt; &lt;td&gt;Item Name&lt;/td&gt; &lt;td&gt;Weight&lt;/td&gt; &lt;td&gt;Amount&lt;/td&gt; &lt;td&gt;Cost&lt;/td&gt; &lt;td&gt;Total Weight&lt;/td&gt; &lt;td&gt;Total Cost&lt;/td&gt; &lt;td&gt;Notes&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="text" class="sheet-number" name="attr_vehicle_item_name" title="vehicle_item_name" style="width:300px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_vehicle_item_weight" title="vehicle_item_weight" value="0" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" name="attr_vehicle_item_amount" title="vehicle_item_amount" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_vehicle_item_cost" title="vehicle_item_cost" value="0" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_vehicle_item_total_weight" title="vehicle_item_total_weight" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="number" class="sheet-number" step="0.1" name="attr_vehicle_item_total_cost" title="vehicle_item_total_cost" style="width:60px" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input type="text" class="sheet-number" name="attr_vehicle_item_notes" title="vehicle_item_notes" style="width:400px" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/fieldset&gt; I have working script from GiGs for character equipment which I duplicated for vehicle section. With changed field names script starting to look like this - &lt;script type="text/worker"&gt; on("change:repeating_inventory:item_weight change:repeating_inventory:item_cost change:repeating_inventory:item_amount remove:repeating_inventory", function() { getSectionIDs("repeating_inventory", function(IDArray) { var fieldNames = []; for (var i=0; i &lt; IDArray.length; i++) { fieldNames.push("repeating_inventory_" + IDArray[i] + "_item_weight"); fieldNames.push("repeating_inventory_" + IDArray[i] + "_item_cost"); fieldNames.push("repeating_inventory_" + IDArray[i] + "_item_amount"); } getAttrs(fieldNames, function(values) { // since we are doing the whole repeating section, we need a variable to hold the values of each row var output = {}; // we also need to count the total weight as we go var total = 0; for (var i=0; i &lt; IDArray.length; i++) { // we need to go through each row, and get the weight, cost, and amount // and calculate the totals for that row var weight = parseFloat(values["repeating_inventory_" + IDArray[i] + "_item_weight"]) || 0; var cost = parseFloat(values["repeating_inventory_" + IDArray[i] + "_item_cost"]) || 0; var amount = parseInt(values["repeating_inventory_" + IDArray[i] + "_item_amount"]) || 0; // calculate the totals for THIS row var row_weight = weight * amount; var row_cost = cost * amount; // save those values for updating the sheet later output["repeating_inventory_" + IDArray[i] + "_item_total_weight"] = row_weight; output["repeating_inventory_" + IDArray[i] + "_item_total_cost"] = row_cost; // update the total weight total += row_weight; } // now we have looped through the entire set, we need to save the total weight output["current_carry_weight"] = total; // now we can save the stats to the sheet. setAttrs(output); }); }); }); on("change:repeating_vehicle_inventory:vehicle_item_weight change:repeating_vehicle_inventory:vehicle_item_cost change:repeating_vehicle_inventory:vehicle_item_amount remove:repeating_vehicle_inventory", function() { getSectionIDs("repeating_vehicle_inventory", function(IDArray) { var fieldNames = []; for (var i=0; i &lt; IDArray.length; i++) { fieldNames.push("repeating_vehicle_inventory_" + IDArray[i] + "_vehicle_item_weight"); fieldNames.push("repeating_vehicle_inventory_" + IDArray[i] + "_vehicle_item_cost"); fieldNames.push("repeating_vehicle_inventory_" + IDArray[i] + "_vehicle_item_amount"); } getAttrs(fieldNames, function(values) { // since we are doing the whole repeating section, we need a variable to hold the values of each row var vehicle_output = {}; // we also need to count the total weight as we go var vehicle_total = 0; for (var i=0; i &lt; IDArray.length; i++) { // we need to go through each row, and get the weight, cost, and amount // and calculate the totals for that row var vehicle_weight = parseFloat(values["repeating_vehicle_inventory_" + IDArray[i] + "_vehicle_item_weight"]) || 0; var vehicle_cost = parseFloat(values["repeating_vehicle_inventory_" + IDArray[i] + "_vehicle_item_cost"]) || 0; var vehicle_amount = parseInt(values["repeating_vehicle_inventory_" + IDArray[i] + "_vehicle_item_amount"]) || 0; // calculate the totals for THIS row var vehicle_row_weight = vehicle_weight * vehicle_amount; var vehicle_row_cost = vehicle_cost * vehicle_amount; // save those values for updating the sheet later vehicle_output["repeating_vehicle_inventory_" + IDArray[i] + "_vehicle_item_total_weight"] = vehicle_row_weight; vehicle_output["repeating_vehicle_inventory_" + IDArray[i] + "_vehicle_item_total_cost"] = vehicle_row_cost; // update the total weight vehicle_total += vehicle_row_weight; } // now we have looped through the entire set, we need to save the total weight vehicle_output["vehicle_current_carry_weight"] = vehicle_total; // now we can save the stats to the sheet. setAttrs(vehicle_output); }); }); }); &lt;/script&gt; Amd the problem is that first part of the script works, but second don't. I thought the problem is in function names, which are duplicated - but it does not help. So I again ask for help to fix it.
1586038241

Edited 1586038287
Andreas J.
Forum Champion
Sheet Author
Translator
The number one rule of Repeating sections is that you can't use underscores in it's name or it will break . You should have a name like: &lt;fieldset class="repeating_vehicle-inventory"&gt; It's a common mistake to make.
1586058518

Edited 1586058609
GiGs
Pro
Sheet Author
API Scripter
Andreas is spot on. I would suggest avoiding hyphens in attribute names if they are going to sheet workers. In this one it wont matter, but there are two ways of getting data out of objects, like the values object: let something = values.attribute_name; let something = values['attribute_name']; The second is very useful when you are building attribute names from parts (like in the scripts above). The first allows simpler syntax when you know the name and dont have to build out of parts. But it breaks of the name has a hyphen in it. This: let something = values.attribute-name; would break, and if you use hyphens in attribute names, you can trip over this and lose some time figuring out why your script is breaking before you remember :) Since using capital letters is also a bad idea in repeating section names, you have to decide the best way for readability. In this case a hyphen is fine, but so is making it a single word repeating_vehicleinventory
1586061052
GiGs
Pro
Sheet Author
API Scripter
Just because I can't help myself (and like a task like this while I'm having my breakfast), here's a sheet worker that will replace both the sheet workers above, and can be expanded if you add any more repeating sections that track inventory (unlikely as that seems): // first create a variable which holds the values that each section has in common, and their specific names. const&nbsp;inventories&nbsp;=&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;inventory:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;weight:&nbsp;'item_weight', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cost:&nbsp;'item_cost', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amount:&nbsp;'item_amount', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total_weight:&nbsp;'item_total_weight', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total_cost:&nbsp;&nbsp;'item_total_cost', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;carry:&nbsp;'current_carry_weight' &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;"vehicle-inventory":&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;weight:&nbsp;'vehicle_item_weight', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cost:&nbsp;'vehicle_item_cost', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amount:&nbsp;'vehicle_item_amount', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total_weight:&nbsp;'vehicle_item_total_weight', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total_cost:&nbsp;&nbsp;'vehicle_item_total_cost', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;carry:&nbsp;'vehicle_current_carry_weight' &nbsp;&nbsp;&nbsp;&nbsp;} } // forEach is a nicer syntax than For, and allows us to loop through the two inventory types Object.keys(inventories).forEach(section&nbsp;=&gt;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;// we need to build on on(changes) event line, which is a bit tricky. this variable does it. &nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;changes&nbsp;=&nbsp;Object.values(inventories[section]).map(value&nbsp;=&gt;&nbsp;`change:repeating_${section}:${value}`).join('&nbsp;'); &nbsp;&nbsp;&nbsp;&nbsp;on(changes,&nbsp;() =&gt;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSectionIDs(`repeating_${section}`,&nbsp;IDArray =&gt;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// changed from var to const: both do the same thing, create a variable &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;fieldNames&nbsp;=&nbsp;[]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// switched to use forEach syntax since we are already using it above &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IDArray.forEach(id&nbsp;=&gt;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fieldNames.push(`repeating_${section}_${id}_${inventories[section].weight}`); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fieldNames.push(`repeating_${section}_${id}_${inventories[section].cost}`); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fieldNames.push(`repeating_${section}_${id}_${inventories[section].amount}`); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getAttrs(fieldNames,&nbsp;function(values)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;since&nbsp;we&nbsp;are&nbsp;doing&nbsp;the&nbsp;whole&nbsp;repeating&nbsp;section,&nbsp;we&nbsp;need&nbsp;a&nbsp;variable&nbsp;to&nbsp;hold&nbsp;the&nbsp;values&nbsp;of&nbsp;each&nbsp;row &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const output&nbsp;=&nbsp;{}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;we&nbsp;also&nbsp;need&nbsp;to&nbsp;count&nbsp;the&nbsp;total&nbsp;weight&nbsp;as&nbsp;we&nbsp;go &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let total&nbsp;=&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IDArray.forEach(id&nbsp;=&gt;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;we&nbsp;need&nbsp;to&nbsp;go&nbsp;through&nbsp;each&nbsp;row,&nbsp;and&nbsp;get&nbsp;the&nbsp;weight,&nbsp;cost,&nbsp;and&nbsp;amount &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;and&nbsp;calculate&nbsp;the&nbsp;totals&nbsp;for&nbsp;that&nbsp;row &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// getting these variables is a bit trickier in this version! He we use template literal syntax. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const weight&nbsp;=&nbsp;parseFloat(values[`repeating_${section}_${id}_${inventories[section].weight}`])&nbsp;||&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const cost&nbsp;=&nbsp;parseFloat(`repeating_${section}_${id}_${inventories[section].cost}`)&nbsp;||&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const amount&nbsp;=&nbsp;parseInt(`repeating_${section}_${id}_${inventories[section].amount}`)&nbsp;||&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;calculate&nbsp;the&nbsp;totals&nbsp;for&nbsp;THIS&nbsp;row &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const row_weight&nbsp;=&nbsp;weight&nbsp;*&nbsp;amount; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const row_cost&nbsp;=&nbsp;cost&nbsp;*&nbsp;amount; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;save&nbsp;those&nbsp;values&nbsp;for&nbsp;updating&nbsp;the&nbsp;sheet&nbsp;later &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output[`repeating_${section}_${id}_${inventories[section].total_weight}`]&nbsp;=&nbsp;row_weight; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output[`repeating_${section}_${id}_${inventories[section].total_cost}`]&nbsp;=&nbsp;row_cost; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;update&nbsp;the&nbsp;total&nbsp;weight &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total&nbsp;+=&nbsp;row_weight; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;now&nbsp;we&nbsp;have&nbsp;looped&nbsp;through&nbsp;the&nbsp;entire&nbsp;set,&nbsp;we&nbsp;need&nbsp;to&nbsp;save&nbsp;the&nbsp;total&nbsp;weight &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output[inventories[section].carry]&nbsp;=&nbsp;total; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;now&nbsp;we&nbsp;can&nbsp;save&nbsp;the&nbsp;stats&nbsp;to&nbsp;the&nbsp;sheet. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setAttrs(output); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;}); });
Thank you)&nbsp;
1586642868
GiGs
Pro
Sheet Author
API Scripter
you're welcome :)