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

[Sheet Workers] Using variable names in setAttrs?

1516478275

Edited 1516478398
Missingquery
Pro
Sheet Author
API Scripter
I am attempting to set an attribute dynamically, but it seems like the compiler does not want to parse the combined string as a string, and instead stops when I get to "+". How do I fix this? I considered storing the string in a variable, but then realized that the setAttrs function would not look for the variable, but a (nonexistant) attribute of that name. Here is my code (keep in mind that most of it is omitted for brevity's sake; while referenced in here, it doesn't pertain to my problem at the moment): on("change:repeating_weapons:Uses", function(eventInfo) {     console.log(eventInfo.newValue)     if (eventInfo.newValue === "0"){         console.log("Broken")         let RID = eventInfo.sourceAttribute.slice(18,38) //This gets the repeating row ID of the changed attribute         getAttrs(["repeating_weapons_"+RID+"WName"], function(v){             setAttrs({                 "repeating_weapons_"+RID+"_WRank": "UU", //here is where the error is                 "repeating_weapons_"+RID+"_WName": "Broken " + v["repeating_weapons_"+RID+"_WName"]             });         }     } });
1516486519

Edited 1516486614
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
When setting the properties of an object the way you are doing, you must use static keys. To use dynamic keys, try this: on("change:repeating_weapons:Uses", function(eventInfo) {     console.log(eventInfo.newValue); setObj = {}; //create an empty object     if (eventInfo.newValue === "0"){         console.log("Broken")         let RID = eventInfo.sourceAttribute.slice(18,38) //This gets the repeating row ID of the changed attribute getAttrs(["repeating_weapons_"+RID+"WName"], function(v){ setObj["repeating_weapons_"+RID+"_WRank"]="UU"; //set the "repeating_weapons_"+RID+"_WRank" key of that object to "UU" - It was undefined before. setObj["repeating_weapons_"+RID+"_WName"]="Broken " + v["repeating_weapons_"+RID+"_WName"]; //same as above             setAttrs( setObj );         }     } });
1516492183
GiGs
Pro
Sheet Author
API Scripter
Can you just use square brackets like so: ? on("change:repeating_weapons:Uses", function(eventInfo) {     console.log(eventInfo.newValue)     if (eventInfo.newValue === "0"){         console.log("Broken")         let RID = eventInfo.sourceAttribute.slice(18,38) //This gets the repeating row ID of the changed attribute         getAttrs(["repeating_weapons_"+RID+"WName"], function(v){             setAttrs({                 ["repeating_weapons_"+RID+"_WRank"]: "UU", //here is where the error is                 ["repeating_weapons_"+RID+"_WName"]: "Broken " + v["repeating_weapons_"+RID+"_WName"]             });         }     } });
1516541312
Jakob
Sheet Author
API Scripter
G G's method with square brackets works (though your original code is missing a closing bracket after the getAttrs, which is still present in both Scott's and G G's corrected code, bolded in my version). However, if you only need to set attributes in the same repeating row, you do not need to reference the rowid explicitly at all: on("change:repeating_weapons:uses", function(eventInfo) {     console.log(eventInfo.newValue);     if (eventInfo.newValue === "0"){         console.log("Broken");         getAttrs(["repeating_weapons_WName"], function(v){             setAttrs({                 "repeating_weapons_WRank": "UU",                 "repeating_weapons_WName": "Broken " + v["repeating_weapons_WName"]             });         } );     } });
1516647924
Missingquery
Pro
Sheet Author
API Scripter
That really helps. Thank you!
1516649534
Lithl
Pro
Sheet Author
API Scripter
Another point: You can use template literals as property keys in an object literal, eg: setAttrs({ `repeating_weapons_${RID}_WRank`: ..., `repeating_weapons_${RID}_WName`: ..., });
1516650397
Jakob
Sheet Author
API Scripter
Oh, that's nice, didn't know that Brian!