TinyButStrong - the PHP Template Engine
Categories > OpenTBS with ODS >

conditional style in the template

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: giorg
Date: 2011-02-02
Time: 12:16

conditional style in the template

Hi all,

is it possible to define a condition inside the ods template, something like: "if this value='ciao' then this cell must be bold"?

Thank you very much

Andrea
By: Skrol29
Date: 2011-02-02
Time: 16:10

Re: conditional style in the template

Hi Gior,

It's possible to manage conditional styles in OpenOffice / LibreOffice Workbooks.
OpenOffice/LibreOffice Calc workbooks manage both automatic styles and explicit styles.
When you change manually the lookup of a cell, then OpenOffice creates an internal "automatic style" and it doesn't let you know the name of this style unless you go and see the inner XML content of the workbook.
In the other hand, you can create explicit custom styles for your workbook. (Menu "Format", "Styles and formating").

Knowing this, you can create your own custom styles and play with TBS for changing the style of a cell.
Example if code put ina cell:
[onshow.x;att=table:table-cell#table:style-name;if [val]=1;then 'MyCustomStyle'; else 'Default']

Or if you want to delete the style instead of applying the Default one:
[onshow.x;att=table:table-cell#table:style-name;if [val]=1;then 'MyCustomStyle'; else '';magnet=#]


In order to understand the principle, here are examples for XML source for workbooks:
--------------------------------------------------------------------------------------------------

Example of custom style applied on a cell:
<table:table-cell table:style-name="MyCustomStyle" office:value-type="float" office:value="29.0">
  <text:p>29,0</text:p>
</table:table-cell>

Example of custom style applied on a row:
<table:table-row table:style-name="ro2">
  <table:table-cell table:style-name="MyCustomStyle" table:number-columns-repeated="1024"/>
</table:table-row>
Note that the style is defined on the first cell of the row even if this cell is empty.

Example of custom style applied on a column:
<table:table ...>
  <table:table-column table:style-name="co1" table:default-cell-style-name="MyCustomStyle"/>
  <table:table-cell ...>
    ...
  </table:table-cell>
</table:table>
Note that the style is defined on single <table:table-column> element. Cells of the column are still defined inside <table:table-cell> elements.
By: giorg
Date: 2011-02-02
Time: 16:18

Re: conditional style in the template

Hi Skrol,

thank you very much for your detailed explanation. In my template I have this:

[data.set; block=table:table-row]    [data.rep]    [data.srep]    [data.label]    [data.desc]    [data.pvm]    [data.pzkg]    [data.ga]    [data.margine]    [data.marperc]    [data.stk]    [data.et]

(obviusoly it is one variable per cell). So, what I would like to do, for example, is to have data.marperc in red if the substituted value is a negative number.
What is not clear to me is the TBS syntax you suggested: what's that onshow.x? Where should I put that code, inside [data.marperc]? before or after?

Thanks a lot
By: Skrol29
Date: 2011-02-02
Time: 17:36

Re: conditional style in the template

Just had a TBS field in the cell, before the [data.marperc] field. Like this:
[data.marperc;att=table:table-cell#table:style-name;if [val]-+0;then 'MyRedStyle'; else 'Default'][data.marperc]

For more readability, you can move the first TBS tag into a comment of the cell. It will do its job and delete the comment if you code it like this:
[data.marperc;att=table:table-cell#table:style-name;if [val]-+0;then 'MyRedStyle'; else 'Default';comm=office:annotation]

I have to say that in fact the two solutions above are not quite clean if you expect to use formulas that refer to this cell.
This is because the cell you're coding in your template is necessarily considered by OpenOffice to be a string content. That is because the TBS fields is actually a string content.
But at the end, you're probably expecting to have a numerical content.
This can be changed using two ugly TBS tags:
[onload;att=table:table-cell#office:value-type;ifempty=float][data.marperc;att=table:table-cell#office:value]
The first TAG tag will change the type of the cell, and the second one will merge the  [data.marperc] into the value attribute of the cell.
When you have this, you can use formulas on this cell, and also use a Numerical format on the cell. OpenOffice support negative/positive conditional formats.
This is tested and should work nice.

I'm currently working on a new version of OpenTBS, and it should include facilities for merging Calc cells.
By: giorg
Date: 2011-02-02
Time: 17:50

Re: conditional style in the template

Thanks again,

I did use the second notation, but now all the cell of that column are empty. Is that [val] a variable that indicates the value that will be substituted inside the cell?
And if so, can I test also a string, let say if [val]='string'?

Thank you very much, your work is greatly appreciated

[UPDATE]
Using the first notation the values are there, but still the style not applied... One more question: is it possible to have more complicated conditions, like if this value is <0 but data.fam = 'fam.' then aplly another style?
Thanks and sorry for my many questions :)

[RE-UPDATE]
Sorry I was wrong, with the first notation everything is fine, it was my mistake in the style :) Still I keep the other questions :)
By: Skrol29
Date: 2011-02-02
Time: 18:04

Re: conditional style in the template

Hi Giorg,

Yes [val] is the joker that you can use inside the TBS field's parameters and which represents the value currently merged.
Some parameters supports [val], some don't.
See http://www.tinybutstrong.com/manual.php#html_field_prm_if
You can use string values with it.

> now all the cell of that column are empty
Did you created your custom format ?
In the merged result, what is the format applied on the merged cells (open the format dialog box, it should highlight the format of the current selection).

If you don't succeed, you can send to me a piece of the template and a piece of the result.

By: giorg
Date: 2011-02-02
Time: 18:10

Re: conditional style in the template

Hi,

I tried this in the data.fam cell:

[data.fam;att=table:table-cell#table:style-name;if [val]='fam.';then 'inve_bold'][data.fam]

and this in another cell:

[data.sr;att=table:table-cell#table:style-name;if [data.fam]='fam.';then 'inve_bold'][data.sr]

but none of them works.

Thank you very much for your support
By: giorg
Date: 2011-02-03
Time: 10:24

Re: conditional style in the template

Hey Skrol,

is it possible that I found a bug? The code above was right, but I just put it in the wrong cell. When I realized that, then I did correct it, and now the produced ods cannot be opened because the content.xml file seems to be corrupted. In fact, there is a unclosed <table:table-cell> tag... But this sounds strange to me, because the code is the same as the one for the red color, just the condition and the applied style are differents...

Will try to debug a little further...
By: giorg
Date: 2011-02-03
Time: 10:51

Re: conditional style in the template

Alright,

I solved putting an else: don't know why the condition was succeeding in the very last row, it shouldn't because the value of the cell wasn't "fam.", but with an else 'Default' everything is fine. But I still need help with this: can I say something like "if the merged value of *that* cell is 'fam.' then also this cell has to be bold?

[data.sr;att=table:table-cell#table:style-name;if [data.label]='fam.';then 'inve_bold'][data.sr]

Thanks a lot for your help
By: Skrol29
Date: 2011-02-03
Time: 16:20

Re: conditional style in the template

Is your problem fixed?
By: giorg
Date: 2011-02-03
Time: 16:23

Re: conditional style in the template

I still need some help with the code above: as u see inside the if condition I tried data.label, because I need to apply a style if *another* merged cell has a certain value. Is that possible?

thanks