Making new colors in JExcelApi - excel

I'm using JExcelApi for generating XLS files. From jxl.format.Colour, I see how to get any of the colors in the "standard Excel colour palette", but not how to create a new color (say, given its RGB).
But in Excel itself, I can pick any color at all.
Am I just missing it? Is there a way in JExcelApi to select an arbitrary color? I'm using a simple find-the-closest-standard-color method right now, which is OK but not great.

The way to override a palette index in JExcel API is to use the method [setColourRGB][1] on the writable workbook. For instance:
myWorkbook.setColourRGB(Colour.LIGHT_TURQUOISE2, 14, 67, 89);
if you want to change the color value in the palette entry where there is by default the second light turquoise. Or, more easily in some cases, directly with the palette index:
myWorkbook.setColourRGB(Colour.getInternalColour(myPaletteIdx), 14, 67, 89);
A small update: only the indexes from 8 to 64 can be customized according to a comment inside the source code of jxl.biff.PaletteRecord.
[1]: http://jexcelapi.sourceforge.net/resources/javadocs/current/docs/jxl/write/WritableWorkbook.html#setColourRGB(jxl.format.Colour, int, int, int)

Excel versions before 2007 have a standard palette, and given that the API you are using does not support the 2007 format, you may be stuck with that. The reason you can choose any colour you want is probably because you are using a new version of Excel.
See this information on the Microsoft site.
I don't see how you could override the standard colour palette in the API you are using, but in Apache POI (which also lets you write Excel files) you can: see this link. Basically, what you need to do there is: assign certain standard colours (green, etc) to your cells; then override these colours with whatever custom colour you need.

WritableCellFormat cellFormat = new WritableCellFormat();
Colour customColor = new Colour(10000, "1", 255, 0, 0){
};
cellFormat.setBackground(customColor);
writableCell.setCellFormat(cellFormat);

Related

Adding global font size styling to Axlsx sheet

I would like to set the sheet-wide default font size set to 8pt, instead of the default 11pt. Is there a way to set a global style using the Axlsx gem (implemented with Axlsx-Rails)?
I've looked at the axlsx_styler gem, which looks like it solves a few other styling limitations that are present in Axlsx out-the-box, however, it still doesn't seem to cater for global styles.
I realize of course that an option would be to use Axlsx's style_name = wb.styles.add_style(...) to set up a style and call it on every single row the sheet generates, but give there are other once-off styles that also need drizzling around the sheet, that doesn't seem like a great solution.
Are there any other ideas on how to achieve the setting of global styles on an Axlsx sheet?

How to set the global font of word file via Apache poi?

I'm a newbie of apache poi, and am using poi to write some data to docx file. Recently I encountered a problem of font, hope any body can give me some help.
I began the writing with an empty word file named empty.docx, as following.
InputStream input = getClass().getClassLoader().getResourceAsStream("empty.docx");
XWPFDocument document = new XWPFDocument(input);
In the empty.docx the default font is 'Arial', that means if you add any content they will inherit this font as long as you don't change the font manually.
But after I filled the content and write it to a new docx file,the default font was changed to 'Century'.
OutputStream output = new FileOutputStream("output.docx");
document.write(output);
I want to change it back to 'Arial', but after a lot of search only found the methods to set font of XWPFRun.
run.getCTR().getRPr().getRFonts().setEastAsia(eastAsiaFontName);
run.getCTR().getRPr().getRFonts().setHAnsi(normalFontName);
I want to know if there is any method to change the global font of the whole document?
You can set the default font of the document like this (using your variable names):
XWPFStyles styles = document.createStyles();
CTFonts fonts = CTFonts.Factory.newInstance();
fonts.setEastAsia(eastAsiaFontName);
fonts.setHAnsi(normalFontName);
styles.setDefaultFonts(fonts);
I am currently stuck at how I set the default font size. That's why I found this question... The XWPFStyles class has a member "CTStyles ctStyles" with which it would be possible to define any property defined in the XML spec. But unfortunately it has only a public setter, but no getter.

What does <color indexed="81"> mean?

I have a document created by Excel 2007:
<fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4506" codeName="{B7FE6334-C1A2-E50D-BD3D-5F4D41BBC2E3}"/>
... which contains the following color in a font definition in xl/styles.xml:
<color indexed="81"/>
I understand from the ECMA standard that this colour index refers to the <indexedColors> collection in xl/styles.xml if there is such a collection, otherwise it refers to the default palette shown in the standard. My problem is that this document contains no <indexedColors> element, and the default palette only has 66 entries, so I do not know what 81 refers to. Does anybody else?
Interestingly a google search for color indexed="81" returns some sample OpenXML snippets containing the same thing, but alas no explanation.
MSDN Documentation specifies the indexed property of class Color in OpenXML as:
Indexed color value. Only used for backwards compatibility. References a color in indexedColors.
The possible values for this attribute are defined by the W3C XML Schema unsignedInt datatype.
It is part of the larger DocumentFormat.OpenXml.Spreadsheet namespace.
The file you're describing was built via source code which contained the 81 value. It probably looked something like this Java code, defining a Color() instance with 81U from an unrelated color index.
If you're needing to find out why, I'd create an account at MSDN and reply to Jack9999's post with an inquiry as to why he used that value. I'm guessing it's a bug on his part, being familiar with a separate and possibly JAVA-related color index.
Excel--not recognizing it--is just using their default comment color values.
Cheers
Index 0x51 is the system tooltip text color. (i.e. ::GetSysColor(COLOR_INFOTEXT) ).
NECRO answer:
From Vincent Tan's SpreadsheetOpenXmlFromScratch:
For colours, if you're dealing with the DocumentFormat.OpenXml.Color class,
there are 3 ways of setting the colour value:
Indexed colour
RGB colour
Themed colour
There's a property called Auto of the Color class. I didn't find a use for it, and you can probably ignore it. Excel won't choke on errors if you don't set it in any caseā€¦
Indexed colours are for backwards compatibility, so I'm not going to teach you how. Basically it's an index value to the palette of colours stored within the spreadsheet's stylesheet. We'll deal with the Stylesheet class in the next chapter. You can explore the IndexedColors class on your own, which is a child class of the Colors class, which is in turn a child class of Stylesheet.

Translating column values in a view control

I have an application that uses a classic frontend for the Notes users and now has a XPages frontend for web users.
I've used the Localization feature for making the application multilangual. But this doesn't help me on translating column values in a view.
I've come up with a solution via text files which works well in non-categorized views.
I've created a text file for each supported language and linked them as a bundle ressource to the XPages. In the view column I used this code :
var cat = #ReplaceSubstring(rowData.getColumnValue("Category")," ","");
var catStr = String(cat);
return categories[catStr];
If I use the same method in a categorized column, I get an error :
Error while executing JavaScript computed expression
Script interpreter error, row=1, column=5: unknown element '' in Java-class 'java.util.PropertyResourceBundle'
Does anyone know what this means ? Or are there better solutions for getting a view columns value localized ?
Interesting approach. Since you categorize in a single language this will determine your sort order. How do users react when they see:
blue, yellow, green, red, black
instead of
black, blue, green, red, yellow
since your categories in the view would be
blau, gelb, gruen, rot, schwarz
Content internationalization is a tricky topic.
You could do the translation in an xe:objectData control and return the objects for a datatable as a sorted list

How do I get tag info from text?

I have written an application using Python 2.7 and Tkinter that edits *.docx files. In the Text control where the user is able to write text, I can change the font family and fontsize with tag_add and tag_config. Now I need to write this to new *.docx file. How can I retrieve fontfamily and fontsize that were set in several of the text ranges?
My best guess is to get tag from range and later try to get font from this, but I'm not sure.
I have just solved my problem myself :)
Maybe anyone will need it somewhen. You can read any applied attribute from a tag. To do this, you have to use tag_cget method:
selectedFont = textBox.tag_cget("tagName", 'font')
Now, when we got the font from specially tag, we can get from it other attributes by the same way:
fontFamily = selectedFont.cget('family')
It works! ;)

Resources