
Axel said: Thank you, that's a helpful explanation, but sadly not enough for me to actually write the code myself. Is something like this what I'm looking for? <a href="https://app.roll20.net/forum/post/5798542/help-explain-this-working-sheet-worker-to-me/?pageforid=5798542#post-5798542" rel="nofollow">https://app.roll20.net/forum/post/5798542/help-explain-this-working-sheet-worker-to-me/?pageforid=5798542#post-5798542</a> That thread does illustrate some concepts, but I dont think its helpful enough for people who havent created a version script before - there's a bit too much complexity there. It doesnt have to be that complex. Here's a simple example, that is imperfect (it only handles one version change, and ideally you want future proofing for multiple updates). First, you need a standalone function that does the actual work. You said some data might be lost - to avoid that, you'd create a function to list the attributes that are being lost, and grab those values and transfer to the equivalent attributes used by your sheet. Here's the basic framework for that: function transferAttributes() { getAttrs(['name list of old attributes'], function(values) { // some code to swap the old values to the new attributes, including any processing of data necessary setAttrs({
newattributes:newvalues // this could be a very long list of attributes and new values
})}; }) }; Then your versioning macro would look something like (this is for illustrating the process, I'll post a better version below): on('sheet:opened', function() { getAttrs(['version'], function (values) { let version = +values.version || 0; if(version < 1.0) { // version is less than current version, this sheet hasnt been updated, so update now transferAttributes();
setAttrs({version: 1.0});
} }); }): So this function checks the version value, and if it is below the current value, it runs the macro that changes values, then sets the version to 1.0, so that the next time the sheet is run, it doesnt trigger again. The version above is easy to read and understand, and is what most of the versions on the github do, but it has a serious flaw, so I don't recommend using it exactly. Here's a simple upgrade: function transferAttributes(newversion) { // now has a parameter for the new version number getAttrs(['name list of old attributes'], function(values) { // some code to swap the old values to the new attributes, including any processing of data necessary setAttrs({
newattributes:newvalues ,
version: newversion // and now sets the new version number at the same time as updating the other stats.
})}; // you might or might not want {silent:true} here, depending on how your sheet works. }) }; on('sheet:opened', function() { getAttrs(['version'], function (values) { let version = +values.version || 0; if(version < 1.0) { transferAttributes(1.0); // here we send the new version number to the function
} }); }): Again its not perfect, since it handles only one version change and ideally you need to handle multiple version upgrades over the course of sheet's life. I've been meaning to post a generic solution for that, hopefully I'll find time for it soon. But in the meantime, the problem this fixes: in the first version above, if something breaks in your upgrading function, all the attributes won't update, but the version number will still be changed, so the function wont run again and some data will be lost. In this version, the version number doesnt change until the data is updated, so you can be sure that if the version number has updated, the update function has worked. Hope this helps!