Java -> Javascript type casting

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

Java -> Javascript type casting

#1

Postby Segi » Thu Aug 23, 2018 9:45 pm

I am working with the Apache POI and using the POI API page for reference. This is the API page I am using right now for reference

https://poi.apache.org/apidocs/index.ht ... mmary.html

I want to pass a byte array to a POI function but am running into an issue with Javascript types.

This is the code:

Code: Select all

var a=[];

a[0]=new java.lang.String("82").getBytes();
a[1]=new java.lang.String("162").getBytes();
 a[2]=new java.lang.String("240").getBytes();

custColor = new XSSFColor(a);

return custColor.getIndex();
My issue is with the call to new XSSFColor(a). The documentation says that it expects a byte array.

When I run this, I get the error

Code: Select all

Cannot convert org.mozilla.javascript.NativeArray@4aafdaff to org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor (GLOBAL_FUNCTIONS#43)
because the array is a Javascript native array and the API expects a byte array. I know that Java is not strict with data types and doesn't really support type casting. Is there another way around this ?

Update: I have been looking into this and found a solution that works.

I've been searching the web some more on how to implement custom colors in POI for XSSF and found this example which works in a normal Java dev environment to create a custom color based on an RGB valueI:

Code: Select all

XSSFColor a = new XSSFColor(new java.awt.Color(128, 0, 128)))
or re-written for Verj like this:

Code: Select all

var a = new XSSFColor(new java.awt.Color(128, 0, 128)))
In Verj, thiis gives the error

Code: Select all

Wrapped java.lang.IllegalArgumentException: Color parameter outside of expected range: Red Blue (
After looking at the docs for java.awt.Color https://docs.oracle.com/javase/7/docs/a ... Color.html, there are a few constructors, one which accepts 3 float values which must have a value between 0.0 to 1.0. I believe that this constructor is incorrectly getting called instead of the constructor that takes 3 integers (which can have a range of 0-255). I tried to force the 3 parameters to an int by calling parseInt(value) and also trying to do java.lang.Integer.valueOf(value) but I kept getting this error.

Eventually, I was able to fix it by doing this:

Code: Select all

var colorArr=color.split(","); // Color is provided as a csv string like "82,162,240"
     	    
return new XSSFColor(new java.awt.Color(parseFloat(colorArr[0]/255),parseFloat(colorArr[1]/255),parseFloat(colorArr[2]/255)))  
This works as expected now
0 x

Steve
Moderator
Moderator
Posts: 414
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

Re: Java -> Javascript type casting

#2

Postby Steve » Wed Aug 29, 2018 11:34 am

Hi Segi,

you should be able to do this:

Code: Select all


//create byte array buffer
var buffer = new java.nio.ByteBuffer.allocate(3 * 4);

buffer.putInt(82);
buffer.putInt(162);
buffer.putInt(240);


custColor = new XSSFColor(buffer.array(), new org.apache.poi.xssf.usermodel.DefaultIndexedColorMap());

return custColor.getIndex();
I think that you require a DefaultIndexedColorMap in the Constructor also.

Kind regards

Steve Upton
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

Re: Java -> Javascript type casting

#3

Postby Segi » Wed Aug 29, 2018 3:45 pm

Thanks,

I'l try that out
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

Re: Java -> Javascript type casting

#4

Postby Segi » Thu Sep 20, 2018 4:31 pm

Steve,

This does not work as expected. It always returns an RGB value of #000000 which is black.

I verified this by doing

Code: Select all

var a=custColor.getRGB();
               
print ("RGB[0]="+a[0]);
print ("RGB[1]="+a[1]);
print ("RGB[2]="+a[2]);
which prints 0 for all indexes. By the way, I am using POI 4 which has deprecated some of the XSSF constructors that were available in POI 3

https://poi.apache.org/apidocs/org/apac ... Color.html

Edit: I'm doing some further testing and noticed something unusual

Code: Select all

var buffer = new java.nio.ByteBuffer.allocate(3 * 4);
     	         buffer.putInt(0,colorArr[0]);
               buffer.putInt(1,colorArr[1]);
               buffer.putInt(2,colorArr[2]);

print ("RGB[0]="+buffer.getInt(0));
print ("RGB[1]="+buffer.getInt(1));
print ("RGB[2]="+buffer.getInt(2));
prints out

Code: Select all

RGB[0]=0
RGB[1]=0
RGB[2]=240
it seems that the buffer isnt populating correctly
0 x

Segi
Ebase User
Posts: 649
Joined: Mon Dec 09, 2013 6:37 pm

Re: Java -> Javascript type casting

#5

Postby Segi » Thu Sep 20, 2018 9:35 pm

This works

Code: Select all

// Split RGB string in format "82,162,240" 
var colorArr=color.split(",");
                
return new XSSFColor(new java.awt.Color(parseFloat(colorArr[0]/255),parseFloat(colorArr[1]/255),parseFloat(colorArr[2]/255)), new org.apache.poi.xssf.usermodel.DefaultIndexedColorMap());
0 x

Steve
Moderator
Moderator
Posts: 414
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

Re: Java -> Javascript type casting

#6

Postby Steve » Fri Sep 21, 2018 1:35 pm

Hi Segi,

Sorry I had missed your response.
Segi wrote:
Thu Sep 20, 2018 9:35 pm
// Split RGB string in format "82,162,240"
var colorArr=color.split(",");

return new XSSFColor(new java.awt.Color(parseFloat(colorArr[0]/255),parseFloat(colorArr[1]/255),parseFloat(colorArr[2]/255)), new org.apache.poi.xssf.usermodel.DefaultIndexedColorMap());

Great that this works :-)

Steve
0 x


Who is online

Users browsing this forum: No registered users and 14 guests