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

Sheetworker; convert text input to integer

1636657379
vÍnce
Pro
Sheet Author
I'm considering changing an existing text input to type="number".  While sheetworkers can be used to test/convert these values as need for sheet calculations, converting them to numbers would be beneficial to take advantage of min, max, step, etc. upon input and help stem some possible macro-related issues (ie calcs and rolls from non-integer attribute values). Issue;  While you can change an input from "number to text" without issue, IME, changing from "text to number" wipes the attribute data. Ugh.  ;-( Thought process if I'm going to convert; existing attribute with input type="text" that may not contain non-integer data. - Probably a number, but users could have entered anything here because it's been a text input. existing data would need to be converted. - I'm guessing this would need to done using parseInt(), parseFloat(), etc. depending the attribute? the new "sanitized" value would then need to be saved. ie setAttr() inputs would then need to be set to type="number" - This needs to hard coded in the HTML, so I don't think this can be done from sheetworkers, correct?  So, would this need to done on the next sheet update with the possibility that some users may "corrupt" those text fields interim? Still debating if this is necessary. Thoughts/Examples/Suggestions all appreciated.  TIA
1636658725

Edited 1636658777
GiGs
Pro
Sheet Author
API Scripter
For your #4, yes, inputs need to be set to type=number in html. The thing is, if you do that, it will immediately (on sheet open) erase all text values, and sheet workers wont have time to operate on them. You dont have to do anything in sheet workers, no need to use parseInt Float, etc., unless that attributes are actualy used in sheet workers, and in that case you probably already have that code set up. So, if you dont care about the text value and dont use that attribute in any sheet workers, just change the type to number, and voila, your job is done. If you do care about the text value, things get very tricky. The only way I can think of handling this in sheet code, is to add a new attribute as your input, and hide the old one. Then have a sheet worker version script that basically processes the old attribute, does whatever conversion you want, and saves it to the new attribute. In that case the text value still exists, and if people need to see what it was before, they can grab its value from the attributes & abilities tab (assuming its not in a repeating section).
1636661419

Edited 1636661741
vÍnce
Pro
Sheet Author
GiGs said: If you do care about the text value Who doesn't care about attribute values?  lol Possibility #1 (OP above); Probably going to leave these inputs as-is then. Possibility #2; test these inputs on a sheet update(versioning) and also on:change post update, for validity(is it a number)? If true, do nothing, else set an error attribute that can trigger CSS to style the suspect field accordingly. (red text, outline, etc.)  This would indicate the value is not valid and "may" cause problems but also allows the user to keep the value depending on if/how they plan to process the attribute. Possibility #3; just ignore them. It's up to the end-user to enter the appropriate values and it's up to sheetworkers and/or api scripts to test/convert all data as required... Possibility #4+...? Thoughts? 
1636665661
GiGs
Pro
Sheet Author
API Scripter
If possibility #2 isnt changing the values or type, it would be simple enough, and you don't need a version script (especially since the attribute value can be changed again, and a new text value might be entered). Just create a sheet worker to run on sheet:opened and attribute change, check if its a valid number or not, set a hidden error attribute to 0 or 1, and use CSS to check that value and style the original input to show a warning value. error checking: Then in your sheet workers where you actually use the attribute value, have your number check to see if the worker can proceed (parseInt, Number(), whatever), and end it if not. Practically speaking, #3 is the same as #2, except you dont do the first paragraph. You still need to do the error checking paragraph. For version #4, do what I earlier suggested with an addition: 1) create a second attribute, which is number type, and hide or remove the first 2) create an error attribute, and have a worker that converts the original text attribute to a number attribute. If the first value isnt valid as a number, set the error attribute to 1, otherwise set it to 0. 3) Have a CSS rule, where if error is 0, it hides the old attribute and shows the new one, and if 1, it hides the new attribute and shows the original text input and prompts the user to change its value to a legal value. (And when they do,  that value is captured and passed over to the new attribute, the error is set to 0, and that attribute is hidden never to be shown again. In your sheet worker where you actually use the attribute, you still need to error check if the value is a number-  but you'd probably be doing that anyway.