So the behaviour is as follows: When EDGE changes, edge-points get changed to that value, regardless of what they were before and when edge points change, they cant go above edge, or below zero. They are several different ways to do this. The easiest is to use two separate workers, the most efficient is to use a single worker (this requires a new property, eventInfo). So, as two workers, you would keep the original worker and add a new one. Though I just realised the original worker had a mistake in the first line (not a problem, just unnecessary part), so I'll post that here too // removed one of the change statements, not needed here
on(' change:edge-points', () => {
getAttrs(['edg', 'edge-points'], values => {
let edge = +values['edg'] ||0; let points = +values['edge-points'] || 0;
let newpoints = points;
if(newpoints > edge) newpoints = edge;
else if (newpoints < 0) newpoints = 0;
if (newpoints !== points) setAttrs({ 'edge-points': newpoints }); }); }); Then the second worker monitors edg and changes edge-points, like so on(' change:edg', () => {
// only need to get edg this time, becuase it's going to overwite edge-points,
getAttrs(['edg'], values => {
let edge = +values['edg'] ||0; setAttrs({ 'edge-points': edge }, {silent:true});
});
}); This is much shorter (and could be shorter still, but I want to make every step as easy to understand as possible). The addition to the setAttrs function is {silent:true}. This stops any changes made by this worker from triggering other workers. Without it, when you change edge-points, it would trigger the other worker (which has change:edge-points, so is watching for changes), and go through that function. But that function is irrelevant in this case, because edg and edge-points have just been set to the same value, so we stop it from triggering. Another approach is with a single sheet worker, where you monitor both attributes, and identify which one changed, and take action appropriately. // here we add a reference to eventInfo, so we can identify what triggered the change.
on('change:edg change:edge-points', (eventInfo) => {
getAttrs(['edg', 'edge-points'], values => {
let edge = +values['edg'] ||0; let points = +values['edge-points'] || 0;
// we want a variable to hold the output (you'll see how this is used)
let output = {};
//check which attribute triggered this sheet worker
let trigger = eventInfo.triggerName;
// the trigger must be either 'edg' or 'edge-points' so we can do an if/else statement
// since the if statement contains more than a single line of instruction it has to be enclosed in {}
if(trigger === 'edge-points') {
// do the edge-points function from before, but this time save the result in the output object.
let newpoints = points;
if(newpoints > edge) newpoints = edge;
else if (newpoints < 0) newpoints = 0;
if (newpoints !== points) output['edge-points'] = newpoints
} else {
// if trigger is not edge-points, it must be edg, so we do the other worker
output['edge-points'] = edge;
}
// now we have a a value to save, we update the sheet. setAttrs({ 'edge-points': newpoints }); }); });