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

Need help with changing the repeating section for custom sheet

1718515302

Edited 1718515337
라브
Pro
Translator
Hi there, I'm working on a custom sheet and require some help on changing the attribute under the repeating section. Here is a simplified code for example: <div class="non_repeating_section"> <select name="attr_op1"> <option value></option> <option value="0">Option 1</option> <option value="1">Option 2</option> <option value="2">Option 3</option> </select> <button type="roll" value="&{template:custom} {{roll=@{new_value}}}"></button> </div> <fieldset class="repeating_section"> <select name="attr_op2"> <option value></option> <option value="0">Option 1</option> <option value="1">Option 2</option> <option value="2">Option 3</option> </select> <button type="roll" value="&{template:custom} {{roll=@{new_value}}}"></button> </fieldset> <script type="text/worker"> // for non-repeating section. This works fine when the repeating script is erased on("change:op1 sheet:opened", function() { getAttrs(["op1"], function(values) { let op1 = parseInt(values.op1)||0; if (op1 == 0) ChangeTo = "@{different_value}"; else if (op1 == 1) ChangeTo = "Some Text"; else if (op1 == 2) ChangeTo = "Other Text"; setAttrs({ "new_value": ChangeTo }); }); }); // I would like to apply the same function as above for the repeating section. This doesn't work as intended... on("change:repeating_section:op2 sheet:opened", function() { getAttrs(["repeating_section_attr_op2"], function(values) { let op2 = parseInt(values.op2)||0; if (op2 == 0) ChangeTo = "@{different_value}"; else if (op2 == 1) ChangeTo = "Some Text"; else if (op2 == 2) ChangeTo = "Other Text"; setAttrs({ "new_value": ChangeTo }); }); }); </script> Could someone please give me an insight as to how to change this? I have very limited knowledge in JS so I may not understand everything, but thanks in advance for the help! 
1718559707

Edited 1718559918
GiGs
Pro
Sheet Author
API Scripter
In your final sheet worker you have this: getAttrs(["repeating_section_attr_op2"] let op2 = parseInt(values.op2)||0; That should be getAttrs(["repeating_section_op2"] let op2 = parseInt(values.repeating_section_op2)||0; The attr_ part is not part of the attribute name, its just a prefix used by roll2-'s back-end to let it know this is an attribute. The only time you ever use it is when creating the attribute in html. Secondly, you have sheet:opened in that worker. You'll need to either replace that or change the sheet worker. There are two syntaxes ofr accessing attributes in repeating sections, one is compatible with sheet:opened, and one is npt. You are using the version that is not. Note: If setAttrs is trying to change an attribute inside the repeating section, its name will need changing too. Here's something ypou might be missing.op2 is not the name of an attribute. A fieldset is like a construction kit: it tells roll20 how to build each attribute in a repeating section. Remember a repeating section can be multiple rows, and a full attribute name is unique, and identifes every attribute in the section. The full name of every attribute in a repeating sectio is made of three parts and really looks like this: repeating_section_-gy5uw8wg_op2 The three parts of the repeating section attribute are: The repeating section name The row id (this is randomly generated, so looks weird like my example) The field name: this is the name of sapecific sub-attributes in the repeating section, like op2. Roll20 needs the full name of the attribute to do anything with it and you can get that with the getSectionIDs function (which gives you each row id). But in the abbreviated form you have used, roll20 always knows which row the worker is being triggered from (its the one where you just manually chnaged an attribute),so it supplies the row id for you - you don't need to know it. Thi is why sheet:opened cant be used with this kind of worker (well, it can, but it willr eport an error). shet:opened tries to run the worker on every row in the section, but this syntax only works with one row at a time. If you want to learn more: <a href="https://cybersphere.me/roll20-sheet-author-master-list/" rel="nofollow">https://cybersphere.me/roll20-sheet-author-master-list/</a> (I really need to shorten that url),
1718562793

Edited 1718562828
라브
Pro
Translator
GiGs said: In your final sheet worker you have this: getAttrs(["repeating_section_attr_op2"] let op2 = parseInt(values.op2)||0; That should be getAttrs(["repeating_section_op2"] let op2 = parseInt(values.repeating_section_op2)||0; The attr_ part is not part of the attribute name, its just a prefix used by roll2-'s back-end to let it know this is an attribute. The only time you ever use it is when creating the attribute in html. Secondly, you have sheet:opened in that worker. You'll need to either replace that or change the sheet worker. There are two syntaxes ofr accessing attributes in repeating sections, one is compatible with sheet:opened, and one is npt. You are using the version that is not. Note: If setAttrs is trying to change an attribute inside the repeating section, its name will need changing too. Here's something ypou might be missing.op2 is not the name of an attribute. A fieldset is like a construction kit: it tells roll20 how to build each attribute in a repeating section. Remember a repeating section can be multiple rows, and a full attribute name is unique, and identifes every attribute in the section. The full name of every attribute in a repeating sectio is made of three parts and really looks like this: repeating_section_-gy5uw8wg_op2 The three parts of the repeating section attribute are: The repeating section name The row id (this is randomly generated, so looks weird like my example) The field name: this is the name of sapecific sub-attributes in the repeating section, like op2. Roll20 needs the full name of the attribute to do anything with it and you can get that with the getSectionIDs function (which gives you each row id). But in the abbreviated form you have used, roll20 always knows which row the worker is being triggered from (its the one where you just manually chnaged an attribute),so it supplies the row id for you - you don't need to know it. Thi is why sheet:opened cant be used with this kind of worker (well, it can, but it willr eport an error). shet:opened tries to run the worker on every row in the section, but this syntax only works with one row at a time. If you want to learn more: <a href="https://cybersphere.me/roll20-sheet-author-master-list/" rel="nofollow">https://cybersphere.me/roll20-sheet-author-master-list/</a> (I really need to shorten that url), Hi GiGs, thank you always for your input, it's very much appreciated!&nbsp; I accidently forgot to delete the part about attr_ when I was creating the example. I managed to get this working by referencing the repeating section article and by trial and error. I know my code will look messy to programmers, but here is my final result (leaving this here in case anyone else has the same question!): Code for non-repeating section &lt;select class="spell-type" name="attr_spell_type"&gt; &lt;option value="7"&gt;&lt;/option&gt; &lt;option value="0"&gt;SP&lt;/option&gt; &lt;option value="1"&gt;STR&lt;/option&gt; &lt;option value="2"&gt;DEX&lt;/option&gt; &lt;option value="3"&gt;CON&lt;/option&gt; &lt;option value="4"&gt;INT&lt;/option&gt; &lt;option value="5"&gt;WIS&lt;/option&gt; &lt;option value="6"&gt;CHA&lt;/option&gt; &lt;option value="7"&gt;ETC&lt;/option&gt; &lt;/select&gt; on("change:spell_type_01 sheet:opened", function() { getAttrs(["spell_type_01"], function(values) { let spell_type_01 = parseInt(values.spell_type_01) || 0; let spl_atk, spl_dc; switch (spell_type_01) { case 0: spl_atk = "@{spell_atk}"; spl_dc = ""; break; case 1: spl_atk = "0"; spl_dc = "DC STR @{spell_dc}"; break; case 2: spl_atk = "0"; spl_dc = "DC DEX @{spell_dc}"; break; case 3: spl_atk = "0"; spl_dc = "DC CON @{spell_dc}"; break; case 4: spl_atk = "0"; spl_dc = "DC INT @{spell_dc}"; break; case 5: spl_atk = "0"; spl_dc = "DC WIS @{spell_dc}"; break; case 6: spl_atk = "0"; spl_dc = "DC CHA @{spell_dc}"; break; case 7: spl_atk = "0"; spl_dc = ""; break; default: spl_atk = "@{spell_atk}"; spl_dc = ""; break; } setAttrs({ "foo_spl_atk": spl_atk, "foo_spl_dc": spl_dc }); }); }); code for repeating section under fieldset class "repeating_spell" &lt;input class='hidden' type='text' name='attr_rep_atk' readonly /&gt; &lt;input class='hidden' type='text' name='attr_rep_dc' readonly /&gt; &lt;select class="spell-type" name="attr_spell_type"&gt; &lt;option value="7"&gt;&lt;/option&gt; &lt;option value="0"&gt;SP&lt;/option&gt; &lt;option value="1"&gt;STR&lt;/option&gt; &lt;option value="2"&gt;DEX&lt;/option&gt; &lt;option value="3"&gt;CON&lt;/option&gt; &lt;option value="4"&gt;INT&lt;/option&gt; &lt;option value="5"&gt;WIS&lt;/option&gt; &lt;option value="6"&gt;CHA&lt;/option&gt; &lt;option value="7"&gt;ETC&lt;/option&gt; &lt;/select&gt; on('sheet:opened change:repeating_spell:spell_type', function() { getSectionIDs('spell', function(idarray) { // first get the attribute names for all rows in put in one array const fieldnames = []; idarray.forEach(id =&gt; fieldnames.push(`repeating_spell_${id}_spell_type`)); getAttrs(fieldnames, values =&gt; { // create a variable to hold all the attribute values youre going to create. const attr = {}; // now loop through the rows again idarray.forEach(id =&gt; { let row = 'repeating_spell_' + id; let sptype = values[`${row}_spell_type`] || 0; if (sptype == 0) { attr[`${row}_rep_atk`] = "@{spell_atk}"; attr[`${row}_rep_dc`] = ""; } else if (sptype == 1) { attr[`${row}_rep_atk`] = "0"; attr[`${row}_rep_dc`] = "DC STR @{spell_dc}"; } else if (sptype == 2) { attr[`${row}_rep_atk`] = "0"; attr[`${row}_rep_dc`] = "DC DEX @{spell_dc}"; } else if (sptype == 3) { attr[`${row}_rep_atk`] = "0"; attr[`${row}_rep_dc`] = "DC CON @{spell_dc}"; } else if (sptype == 4) { attr[`${row}_rep_atk`] = "0"; attr[`${row}_rep_dc`] = "DC INT @{spell_dc}"; } else if (sptype == 5) { attr[`${row}_rep_atk`] = "0"; attr[`${row}_rep_dc`] = "DC WIS @{spell_dc}"; } else if (sptype == 6) { attr[`${row}_rep_atk`] = "0"; attr[`${row}_rep_dc`] = "DC CHA @{spell_dc}"; } else if (sptype == 7) { attr[`${row}_rep_atk`] = "0"; attr[`${row}_rep_dc`] = ""; } else { attr[`${row}_rep_atk`] = "@{spell_atk}"; attr[`${row}_rep_dc`] = ""; } }); console.log(values,attr) setAttrs(attr); }); }); });
1718651954
GiGs
Pro
Sheet Author
API Scripter
I'm glad to got it working. One suggestion - that select at the start should probably be: &lt;select class="spell-type" name="attr_spell_type"&gt; &lt;option value="7" selected &gt;&lt;/option&gt; &lt;option value="0"&gt;SP&lt;/option&gt; &lt;option value="1"&gt;STR&lt;/option&gt; &lt;option value="2"&gt;DEX&lt;/option&gt; &lt;option value="3"&gt;CON&lt;/option&gt; &lt;option value="4"&gt;INT&lt;/option&gt; &lt;option value="5"&gt;WIS&lt;/option&gt; &lt;option value="6"&gt;CHA&lt;/option&gt; &lt;option value="7"&gt;ETC&lt;/option&gt; &lt;/select&gt; Before anything is selected, it looks like the first item is selected, but it is not - nothing is. If players actally want to select that first item, they have to select something else, then select that first item. Addung selected means it is actually selected.