Kraynic said: I saw that, but then I took the explanations inside the code as directions. /* ===== PARAMETERS ==========
destination = the name of the attribute that stores the total quantity
section = name of repeating fieldset, without the repeating_
fields = the name of the attribute field to be summed
can be a single attribute: 'weight'
or an array of attributes: ['weight','number','equipped']
multiplier (optional) = a multiplier to the entire fieldset total. For instance, if summing coins of weight 0.02, might want to multiply the final total by 0.02.
*/ Seemed like it contradicted the other, so I went with what I thought this was telling me. So all I should need for totaling the different sections will be various versions of the 3 line function bit. I'll give it a go in a little bit. That's explaining what the function's parameters are. It tells you what those variables in function are standing in for. So when the sumRepeating function is called, the destination variable you supply in your sheet worker gets substituted in everywhere the variable destination occurs in the sumRepeating function . The same happens with the other parameters there. Kraynic said: Ok, I have it set up, and it is working. All it took was someone hitting me over the head with the obvious. I looked over some other scripts on my sheet and cobbled this up to add them together: on("change:total_weapon change:total_armor change:total_gear change:total_consumables change:total_wealth", function () {
getAttrs(['total_weapon','total_armor','total_gear','total_consumables','total_wealth'], function (values) {
const total_weapon = parseInt(values['total_weapon'])||0;
const total_armor = parseInt(values['total_armor'])||0;
const total_gear = parseInt(values['total_gear'])||0;
const total_consumables = parseInt(values['total_consumables'])||0;
const total_wealth = parseInt(values['total_wealth'])||0;
const total_weight = (total_weapon+total_armor+total_gear+total_consumables+total_wealth);
setAttrs({
total_weight: total_weight
});
});
}); Is there a better (or more condensed) way to do this? It seems to work fine, but just thought I would ask. You could compactify (new word) that by getting rid of the variable declarations, like so on("change:total_weapon change:total_armor change:total_gear change:total_consumables change:total_wealth", function () {
getAttrs(['total_weapon','total_armor','total_gear','total_consumables','total_wealth'], function (values) {
setAttrs({
total_weight: (parseInt(values['total_weapon'])||0) + (parseInt(values['total_armor'])||0) + (parseInt(values['total_gear'])||0)
+ (parseInt(values['total_consumables'])||0) + (parseInt(values['total_wealth'])||0) });
});
}); Personally I like to keep the variables declared, so I wouldn't - the way you've done it is the way I would do it. But there's nothing wrong with the approach above either. If you do go that way, the brackets around each term are important because of the || 0 part - you need each calculation isolated. parseInt function One technique you can do to make the code easier to write is to create a parseInt function to save you writing that out over and over. You could do this: on("change:total_weapon change:total_armor change:total_gear change:total_consumables change:total_wealth", function () {
getAttrs(['total_weapon','total_armor','total_gear','total_consumables','total_wealth'], function (values) {
const int = function(stat) {
return parseInt(values[stat])||0; }
const total_weapon = int('total_weapon');
const total_armor = int('total_armor');
const total_gear = int('total_gear');
const total_consumables = int('total_consumables');
const total_wealth = int('total_wealth');
const total_weight = total_weapon + total_armor + total_gear + total_consumables + total_wealth;
setAttrs({
total_weight: total_weight
});
});
}); Since you are doing the same operation over and over (parseInt, default 0), it's a perfect situation to create a reusable function. This function: const int = function(stat) {
return parseInt(values[stat])||0; } can be streamlined, using arrow functions, to: const int = stat => parseInt(values[stat])||0; This is for all intents and purposes identical to the previous one. Now if you want to make a universal function like this that works for all sheet workers, you'll hit a snag, because it depends on the variable values . Values is not just a label, it's a variable which contains data, and e.g. values[ 'total_armor'] extracts a specific value from that variable. If you want to call this function in all sheet workers, you would need to also pass the values variable to it. You would do that like this: const int = (stat, values) => parseInt(values[stat])||0; That's it, that's the only change you need to make. Then the sheet worker would look like this: const int = (stat, values) => parseInt(values[stat])||0; on("change:total_weapon change:total_armor change:total_gear change:total_consumables change:total_wealth", function () {
getAttrs(['total_weapon','total_armor','total_gear','total_consumables','total_wealth'], function (values) {
const total_weapon = int('total_weapon', values);
const total_armor = int('total_armor', values);
const total_gear = int('total_gear', values);
const total_consumables = int('total_consumables', values);
const total_wealth = int('total_wealth', values);
const total_weight = total_weapon + total_armor + total_gear + total_consumables + total_wealth;
setAttrs({
total_weight: total_weight
});
});
}); Notice the change in the way the function is called, and the fact that the int function itself is outside the sheet worker. You should place it at the very start of your script block, then you can use it in all your sheet workers.