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

Textarea for Repeating Field

1635611665
Pktome
Pro
Marketplace Creator
Sheet Author
Compendium Curator
Is it possible for a sheetworker to recognize a field in text and convert it to data AND automatically create a new line in a repeating field and include it in the sheet with each field filled in? Something like the Compendium, but without using them, just the token alone.
1635620423

Edited 1635620527
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Yes, this is possible. I'd recommend using JSON as the data structure for your textarea input. That's pretty much what you have with some minor styling tweaks: { "name" : "Sword" , "dmg" : "1d6+0" , "atr" : "@{STR}" } Then you just have your sheetworker grab the value of the textarea and attempt to JSON.parse it to turn it into a regular JS object or array: on ( 'clicked:import' ,( event ) => {   getAttrs ([ 'data' ],( attributes ) => {     try { //We're wrapped in a try/catch in case the data that is copied into the textarea is not valid JSON, which would cause an error and a crash.       const setObj = {};       attributes .data = JSON. parse ( attributes .data);       Object . keys ( attributes .data). forEach (( key ) => {         setObj[ key ] = attributes .data[ key ]; //you'd almost certainly want to validate the data, but for this demo I'm just taking it on faith       });       setAttrs (setObj,{silent: true }); //I nearly always setAttrs with {silent:true} as cascades of setAttrs is one of the chief causes of sheet lag     } catch (err){       console. log ( 'Invalid Data, import aborted' )     }   }); }) An example of this in a live sheet is Medieve's Mutants and Masterminds 3e sheet, which allows importing Hero lab created characters via JSON.
1635647725
Pktome
Pro
Marketplace Creator
Sheet Author
Compendium Curator
Thank you for your response, and the sheet you mentioned is a great source of ideas. Scott C. said: Yes, this is possible. I'd recommend using JSON as the data structure for your textarea input. That's pretty much what you have with some minor styling tweaks: { "name" : "Sword" , "dmg" : "1d6+0" , "atr" : "@{STR}" } Then you just have your sheetworker grab the value of the textarea and attempt to JSON.parse it to turn it into a regular JS object or array: on ( 'clicked:import' ,( event ) => {   getAttrs ([ 'data' ],( attributes ) => {     try { //We're wrapped in a try/catch in case the data that is copied into the textarea is not valid JSON, which would cause an error and a crash.       const setObj = {};       attributes .data = JSON. parse ( attributes .data);       Object . keys ( attributes .data). forEach (( key ) => {         setObj[ key ] = attributes .data[ key ]; //you'd almost certainly want to validate the data, but for this demo I'm just taking it on faith       });       setAttrs (setObj,{silent: true }); //I nearly always setAttrs with {silent:true} as cascades of setAttrs is one of the chief causes of sheet lag     } catch (err){       console. log ( 'Invalid Data, import aborted' )     }   }); }) An example of this in a live sheet is Medieve's Mutants and Masterminds 3e sheet, which allows importing Hero lab created characters via JSON. What about being able to generate a repeatable field via button? Do you have a record that does this or do you know where I can look for this information? I would like to be able to create this auto field at the same time as I fill in the JSON information
1635653844
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
You'd simply add in a rowid generation right before that .forEach and set the attribute names as repeating_section_${rowID}_${key}. On phone at the moment, but I can post exact code tomorrow if you need it.
1635713243
Pktome
Pro
Marketplace Creator
Sheet Author
Compendium Curator
Scott C. said: You'd simply add in a rowid generation right before that .forEach and set the attribute names as repeating_section_${rowID}_${key}. On phone at the moment, but I can post exact code tomorrow if you need it. I would appreciate it. If you can build this example: I have an Equipment List with a repeat field. I can add a new Equipment normally and put the values manually OR I can create a new row (in this list) with default values according to a Selector/button... In this case I would like that when clicking on these buttons, the line was generated with these default values, but manipulating the "Repeat Field" is still a bit difficult for me. I'm going to join this information with JSON to assemble "specific types"
1635716550

Edited 1635726745
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
So, given this repeating section: <textarea name='attr_data'></textarea> <button type='action' name='act_import'>Import</button> <fieldset class='repeating_equipment'>   <input type='text' name='attr_type' value=''>   <input type='text' name='attr_name' value=''>   <input type='text' name='attr_qtd' value=''> </fieldset> You're sheetworker would look something like: on('clicked:import',(event)=>{   getAttrs(['data'],(attributes)=>{     try{//We're wrapped in a try/catch in case the data that is copied into the textarea is not valid JSON, which would cause an error and a crash.       const setObj = {};       attributes.data = JSON.parse(attributes.data); const rowID = generateRowID();//generate a new row ID using the function provided by Roll20       Object.keys(attributes.data).forEach((key)=>{         setObj[`repeating_equipment_${rowID}_${key}`] = attributes.data[${key}];//We create the new row by just setting the attributes for it       });       setAttrs(setObj,{silent:true});     }catch(err){       console.log('Invalid Data, import aborted')     }   }); })
1635720169
Pktome
Pro
Marketplace Creator
Sheet Author
Compendium Curator
Scott C. said: So, given this repeating section: <textarea name='attr_json'></textarea> <button type='action' name='act_import'>Import</button> <fieldset class='repeating_equipment'>   <input type='text' name='attr_type' value=''>   <input type='text' name='attr_name' value=''>   <input type='text' name='attr_qtd' value=''> </fieldset> You're sheetworker would look something like: on('clicked:import',(event)=>{   getAttrs(['data'],(attributes)=>{     try{//We're wrapped in a try/catch in case the data that is copied into the textarea is not valid JSON, which would cause an error and a crash.       const setObj = {};       attributes.data = JSON.parse(attributes.data); const rowID = generateRowID();//generate a new row ID using the function provided by Roll20       Object.keys(attributes.data).forEach((key)=>{         setObj[`repeating_equipment_${rowID}_${key}`] = attributes.data[${key}];//We create the new row by just setting the attributes for it       });       setAttrs(setObj,{silent:true});     }catch(err){       console.log('Invalid Data, import aborted')     }   }); }) thank you very much, now i can do my crazy things here
1635726769
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Realized I had misnamed the textarea for the data in my latest example. Fixed that so it's properly called data.
1635776239
GiGs
Pro
Sheet Author
API Scripter
Out of curiousity, how are you constructing the JSON to paste in to that attribute? I'm just wondering if it would be easier to replace it with the inputs (one for type, name, and qtd), and a button to save them to the repeating section. This is just a lot easier for people to enter data in, needs less validation, and doesnt need JSON formatting. You can also use selects to make sure dropdowns for name and type only have valid values, and hnumber type for quantity to make sure users can only enter a number. But if youre generating thoe code via computer, it makes sense to use your approach. Either way, you might want to run some extra validation on the input: check if an item with the same name and type exists, and add its quantity to the existing row, for example.
1635780171
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
GiGs said: Either way, you might want to run some extra validation on the input: check if an item with the same name and type exists, and add its quantity to the existing row, for example. Yep, can't stress this enough and my comments on it in the original code could be easily overlooked. If you do this, you'll want to make sure the data is what it should be before you try applying it. What exactly that validation is going to look like will depend on what your full end goal is, but at a minimum I'd be checking to make sure that 1) required attributes are present in the data and 2) the data only contains valid keys.
1635793848

Edited 1635793931
Pktome
Pro
Marketplace Creator
Sheet Author
Compendium Curator
I use Excel or even database to get this information. If I can show them in a list, I can create Json so that the player can automatically fill in those fields. In fact, while a user can input values ​​manually, I would like them to be able to input this "code" so that the token mounts for them automatically. For example, I tend to have systems with A LOT of spells in a long list. So, for each spell, the player just “copy” the code so that the token fills all fields automatically. This can be used for many things. Let's suppose I have a "shop" where in the form itself I can add or remove values, as well as change item descriptions. At some point the "item existed" in its default form, but it was changed in-game. With this token, I can just "click" on the token button to chat that item. There is no need to create notes on the outside if everything is on the file. Another example is a science fiction system or one that has many derivative fields. A single spell or attack can be overwhelming to place each field individually, with this code, just by, import and if necessary, later change some information. That's why I want to use this logic in the file. For example, I tend to have systems with A LOT of spells in a long list. So, for each spell, the player just "copy" the code so that the token fills all the fields automatically. There are no "ordinary systems" here, just personality systems that are crazy about themselves. Examples: <a href="https://imgur.com/a/3SyFNrr?fbclid=IwAR2OZ6B2H94SJjrR2ZIS5cAA5JdCx4cvJ8K4pFHciTiXxshwKL6HhILJy-o" rel="nofollow">https://imgur.com/a/3SyFNrr?fbclid=IwAR2OZ6B2H94SJjrR2ZIS5cAA5JdCx4cvJ8K4pFHciTiXxshwKL6HhILJy-o</a>
1635797457
GiGs
Pro
Sheet Author
API Scripter
If you are generating the code externally, and know players will use that code its fine the way you are doing it. It's still a good idea to include some validation in your worker just to avoid mistakes, but since its for your own use, you can get by with minimal validation.