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

Pre Populate a repeating section when sheet opens for first time.

I am trying to add rows to a repeating section on first opening the character sheet. I have a field named ‘attr_opened’ which is set to 0 on creation, and then set to 1 ofter the rows are added so that they are no added again. Having read through the various forums on sheet creation and sheetworks, I have been led to believe that it is possible. I created a listener to check for first opening and then create the entry in the repeating section. on('sheet:opened', function() { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getAttrs(['opened'],function(v) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var op = parseInt(v.opened); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(op !==0) return; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;constrid1 = generateRowID(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;varnewTalentid = "repeating_talents_" + rid1 + "_talentname" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setAttrs({opened:1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newTalentid:'speak(common)' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); }); &nbsp; It correctly identifies if the sheet has been opened before and if not changes the value of opened to 1. The repeating section I am trying to add to is called repeating_talents and the primary field within it is attr_talentname. There are other fields in the repeat section I will wish to add to, but for now I will be happy if I can get it to add just the name of the talent. I have output the variable newTalentid to a text field to check it is being generated correctly – it is. &nbsp;&nbsp;&nbsp;&nbsp;For example: repeating_talents_-NdyY-9tYB3o0VLarmnU_talentname I have also tried the following code (taken from a forum post <a href="https://wiki.roll20.net/Sheet_Worker_Scripts" rel="nofollow">https://wiki.roll20.net/Sheet_Worker_Scripts</a> and <a href="https://help.roll20.net/hc/en-us/articles/360037773513#getsectionids-section-name-callback-asynchronous-0-18" rel="nofollow">https://help.roll20.net/hc/en-us/articles/360037773513#getsectionids-section-name-callback-asynchronous-0-18</a> ) on('sheet:opened', function() { &nbsp;&nbsp;&nbsp;&nbsp; getAttrs(['opened'], function(v) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;varop = parseInt(v.opened); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (op !==0) return; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const rid1 = generateRowID(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const newTalent ={}; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newTalent["repeating_talents_" + rid1 + "_talentname"] = "speak(common)"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setAttrs({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opened: 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setAttrs(newTalent); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); }); But this just broke the shhet altogether. Can anyone point out where I am going awry?
1694347879

Edited 1694349097
GiGs
Pro
Sheet Author
API Scripter
I don't know why the second one should "break your sheet altogether" - that worker looks fine. That suggests to me that the problem is somewhere else and is just being exposed by this worker. For the first worker, I'm not sure if it's a copy paste error but there are missing spaces. It should be on('sheet:opened', function() { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getAttrs(['opened'],function(v) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var op = parseInt(v.opened); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(op) return; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const rid1 = generateRowID(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var newTalentid = "repeating_talents_" + rid1 + "_talentname" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setAttrs({opened:1, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [newTalentid]:'speak(common)' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); }); I'm curious about this arrangement, because a player could open the same sheet multiple times in a single session, and so that repeating sdection could be flooded with identical entries. Assuming the op is ever reset to 0 - I'm not sure how that happens.
1694349085

Edited 1694349778
GiGs
Pro
Sheet Author
API Scripter
There were more syntax errors, fixed above. Now the worker works properly. Inside the setAttrs, when an attribute is a variable, you need to wrap it in [ ] brackets. By the way, your if statement should work - I just used a more compact version of the same thing. When you are testing that something doesn't equal a false value like 0, you can simply do what I did. You might want to look up truthy and falsy in javascript. Ignore my earlier comment about the second worker. This line would indeed break things: &nbsp; setAttrs(newTalent); If you want to use that syntax, you must first save the value unto an object variable, like this: const newTalent = {}; newTalent["repeating_talents_" + rid1 + "_talentname"] = "speak(common)"; &nbsp; setAttrs(newTalent);
Thank you very much. I've got it working now. I'm curious about this arrangement, because a player could open the same sheet multiple times in a single session, and so that repeating sdection could be flooded with identical entries. Assuming the op is ever reset to 0 - I'm not sure how that happens regarding the above, the attr_opened is only tempory, whilst I work on getting the correct info in to the fields. Easy for me to reset. Once all is working the way I want I will look at searching the repeding rows to see if the talent exists or just that any talent repeating row exists in which case do not add the talent. (A player may have decided to delete the row as their character doesn't speak&nbsp; common.) once again thank you for your very quick response. ps. some of the missing spaces were due to copy and paste errors.
1694361394
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I would recommend replacing opened with a an attribute called sheet_version. This then allows you to easily identify a brand new sheet, and keep track of sheet versions so you can apply update functions if needed.