HtmlDialog. User's system expects a comma delimiter. Number.to_l throws an error

.innerHTML= may not be the correct JS method. Prefer .value= or innerText=.

But even better would be to use %{} parameter replacement to stuff the values into the HTML file before you send it to the dialog.

Rememeber this example topic ?

You do similar with HTML text in Ruby:

# form_data is a hash of values for the HTML form
# (you can use another reference name if you like)

# Load the HTML template file into a string:
html = File.read(File.join(__dir__,"my_dialog.html"))

# Replace the %{} parameters in the HTML with data from the hash:
@dlg.set_html( html % form_data )

# Set other dialog options and show it:
@dlg.show

So you load an HTML file into a html string object.
This file is setup as a template with %{fieldname} parameters that match the keynames of your Ruby value hash.

IE in the HTML template file …

<input type="text" id="wPlus_width" name="Width" value='%{width}'>

… And in Ruby you load up a hash of values like …

form_data = {
  width: wPlus_width.to_s # Assume this is a SketchUp Length object
  # other data fields
}

Now the call to html % form_data would replace the %{width} substring in the HTML file string with the value marked with the :width key from the hash.

As Julia said, if you are using Length objects on the Ruby side, it’s class’ #to_s method will automagically use the correct decimal delimiter for the numeric string in the form_data hash.
When the string is sent back to Ruby, SketchUp’s String#to_l method automagically knows what the decimal separator is.


ADD: I also explained the above %{} replacement concept here …

… and (further down that topic) …

You are getting closer!

You need to interpolate wPlus_width into the js_command string. Otherwise JavaScript will wonder what wPlus_width is. Also you need quotation marks around it since it’s a string, not a number.

In most cases, this would work, but not if the model unit is set to inches with the unit displayed. That would cause the quotation mark used inside the string to clash with the syntax itself.

js_command = "document.getElementById('id').value= \"#{wPlus_width}\" "

Instead, use this where the JSON formatting automatically escapes special characters for you. This is a general approach that can be used for sending any string rom Ruby to JavaScript.

js_command = "document.getElementById('id').value= #{wPlus_width.to_s.to_json} "

I’m looking at this. It will take a little time to digest. Thank you for the help.

I’m going over your suggestions. It will take a little time to see where it goes. Thank you for the help!

I’ve rewritten some code from An example … to implement Dan’s suggestions. It works when I change the type of length, breadth and depth in the html form from number to text. If I leave the type as number the html won’t show the locale numbers with their comma decimal separator. The fields in the HtmlDialog form show as blank fields. I guess this is a fundamental of html that it only recognises points as decimal separators and so I must ‘type’ floating point numbers as ‘text’. When I change the type to text the HtmlDialog form fields show the locale numbers correctly and the data is correctly interpreted on the Ruby side and I get a box modeled in my SketchUp model.

The code below doesn’t work -

<form>
	length: <input id='id1' type='number' name='length' value='%{length}' required><br>
	breadth: <input id='id2' type='number' name='breadth' value='%{breadth}' required><br>
	depth: <input id='id3' type='number' name='depth' value='%{depth}' required>
</form>

but this does work

<form>
	length: <input id='id1' type='text' name='length' value='%{length}' required><br>
	breadth: <input id='id2' type='text' name='breadth' value='%{breadth}' required><br>
	depth: <input id='id3' type='text' name='depth' value='%{depth}' required>
</form>

The only drawback is that the user loses the incremental arrows on the right hand side of the input field.

I could post the entire example if the forum thought it would be useful. Just let me know.

Thank you to every one who helped with this. Cultures and their diversity make programming interesting.

1 Like

Thank you. That is a possibility. I’ve tried Dan and ene_su’s way and it works fine. It uses only .to_l and .to_s without the need of a custom method. Interesting that it can be done that way to though.

This isn’t unique to HTML. A float can’t express a specific decimal separator or other characters. 5 m, 300mm, 6' 6 1/2" etc must be expressed as strings. Going from Length to Float and then to String removes the characters and only keep numbers. You need to go from Length directly to String, without going through a Float.

That is true. I take the point. I was thinking of numbers in a much too mathematical way, and not in the way they are used in life. Currency is yet another branch of the number representation tree.

Thanks for the help.

1 Like

I’ve flushed out so many bugs in different software because I use non-US locales and non-English characters. Even in SketchUp. It’s an area that is non-trivial and many makes assumptions that doesn’t hold up in the wild.

1 Like