Hello code wizards! I'd appreciate any guidance here. So here's the scoop. I've been maintaining this PTU sheet for a while now, and the main complaint has simply been that most people use an automated Google Drive sheet already, and updating two character sheets is a pain. So, I had the idea to make a bit o' API code, so at least Pro users could save on some hassle. The basic idea is that, if I can create a copyable string serving as a JSON file in the original sheet, I could have the API put it into a R20 sheet. Here's what I have so far: charImport = function(imp,play) {
var char = JSON.parse(imp);
var x = createObj("character", {
name: char.nickname,
inplayerjournals: play,
controlledby: play
});
for (var property in char) {
if (char.hasOwnProperty(property)) {
createObj("attribute",{
name:property,
current:char[property],
characterid:x.id
});
}
}
};
charUpdate = function(imp,play,name) {
var char = JSON.parse(imp);
if(!name){
var name = char.nickname;
}
var list = filterObjs(function(obj) {
if (obj.get("_type")=="character"&&obj.get("name")==name&&(obj.get("controlledby")=="all"||obj.get("controlledby").indexOf(play)!=-1)) {return true;}
else {return false;}
});
var report = findObjs({_id: play})[0];
if(list.length == 0){
sendChat("Importer", "/w ".concat(report.get("_displayname").concat(" There's no character by that name. Try charImport instead.")));
} else if (list.length > 1){
sendChat("Importer", "/w ".concat(report.get("_displayname").concat(" There's more than one character by that name. I'm not sure which one should be edited.")));
} else {
var sheet = list[0];
sheet.set({name: name});
var attrs = findObjs({
_type:"attribute",
_characterid:sheet.id
});
var names = [];
for (var i = 0; i < attrs.length; i++) {
names.push(attrs[i].get("name"));
}
for (var property in char) {
if (char.hasOwnProperty(property)) {
if (names.indexOf(property)==-1){
createObj("attribute",{
name:property,
current:char[property],
characterid:sheet.id});}
else {
var pos = names.indexOf(property);
attrs[pos].set({current:char[property]});
}
}}
}}
on("chat:message", function(msg) {
//This allows players to enter !sr <number> to roll a number of d6 dice with a target of 4.
if(msg.type == "api" && msg.content.indexOf("!newchar ") !== -1) {
var imported = msg.content.replace("!newchar ", "");
charImport(imported,msg.playerid);
} else if(msg.type == "api" && msg.content.indexOf("!updatechar ") !== -1) {
var imported = msg.content.replace("!updatechar ", "");
charUpdate(imported,msg.playerid);
}
});
Now, for the problem at hand: repeating sections. I'm not sure how to add new repeating sections or edit existing ones without foreknowledge of ids. Any ideas how I could build a JSON to incorporate them into this code?