I'm sure the setWithWorker experts like Jakob and Lucian have already thought of this, but thought I'd throw this function out there. It is very nice that we can cause changes on the sheet that will be propagated by the sheetworkers, but one thing that I found myself doing repeatedly was checking to see if the attribute I wanted to set was made already, then making it if it didn't exist and finally setting it regardless of what its start state was. Since I got tired of typing what amounted to the same code repeatedly, I made this: String charid array value value
createAttrWithWorker = function(nam,id,attributes,curr,mx){
attributes = attributes ? attributes : findObjs({type:'attribute',characterid:id});
var attribute = _.find(attributes,(a)=>{return a.toLowerCase().get('name')===nam.toLowerCase()}),
retValue = new Promise((resolve,reject)=>{
onSheetWorkerCompleted(()=>{
resolve(attribute);
});
});
if(!attribute){
attribute = createObj('attribute',{characterid:id,name:nam});
attributes.push(attribute);
}
if(curr && mx){
attribute.setWithWorker({current:curr,max:mx});
}else if(curr || mx){
attribute.setWithWorker((mx ? 'max' : 'current'),(mx ? mx : curr));
}
return retValue;
},
It will look for the attribute you want to create by name, create it if it doesn't exist, and set it via sheet worker. Finally it returns a promise of the attribute that you can await in your calling function to continue working with once it has been set. You can pass it an array of attributes to search for the attribute, or it will look through all attributes associated with the character id.