Create color scale in a range of cells in Calc

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
turulomio
Posts: 1
Joined: Thu Nov 11, 2021 7:36 pm

Create color scale in a range of cells in Calc

Post by turulomio »

Hi. This is my first post
I'm trying to make a conditional format color scale in a range of cells, programatically
I'm trying to set in range "B1:B8" the same as in "A1:A8" in attachment example
Any help, please
Thanks

Code: Select all

from os import path
from uno import getComponentContext, systemPathToFileUrl
from com.sun.star.beans import PropertyValue
localContext = getComponentContext()

resolver = localContext.ServiceManager.createInstance('com.sun.star.bridge.UnoUrlResolver')
ctx = resolver.resolve('uno:socket,host=127.0.0.1,port=2002;urp;StarOffice.ComponentContext')
desktop = ctx.ServiceManager.createInstance('com.sun.star.frame.Desktop')

args=(
    PropertyValue('AsTemplate',0,True,0),
)
template=systemPathToFileUrl(path.abspath("colorscale.ods"))
document=desktop.loadComponentFromURL(template,'_blank', 8, args)

sheet=document.getSheets().getByIndex(0)

# I create and select a range of cells
oRangos = document.createInstance("com.sun.star.sheet.SheetCellRanges")
oRangos.addRangeAddress( sheet.getCellRangeByName( "B1:B8" ).getRangeAddress() ,False )
document.getCurrentController().select(oRangos)
        
#I create a conditional format range in the sheet
cfs=sheet.ConditionalFormats.createByRange(oRangos)
cf=sheet.ConditionalFormats.getConditionalFormats()[cfs-1]

# I Create a colorscale conditional format
cf.createEntry(1, 0)   #1 iS COLORSCALE

# I don't know how to follow
#new = createUnoStruct("com.sun.star.sheet.XColorScaleEntry")


save_args=(
    PropertyValue('FilterName', 0, 'calc8', 0),
)
document.storeAsURL(systemPathToFileUrl(path.abspath("output.ods")), (save_args))
document.dispose()
Attachments
colorscale.ods
Example template
(9 KiB) Downloaded 161 times
LibreOffice on Gentoo
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Create color scale in a range of cells in Calc

Post by Zizi64 »

- The LibreOffice has more than 3 conditions inthe Conditional Format feature. Therefore you can apply lots of the cell styles based on the cell content.

- The LibreOffice has Color Scale type method in the CF feature.

Try the LibreOffice.


And you can use the additional function STYLE() in the Cell formulas, what can apply various cell styles based on the composed style names like the

Code: Select all

CalculatingFormula+STYLE"MyCellStyle"&TEXT(CalculatingFormula;"0")
Based on the cell values 1, 2, 3, 4, 5, 6, the applied Cell Styles will be:
MyCellStyle1
MyCellStyle2
MyCellStyle3
MyCellStyle4
MyCellStyle5
MyCellStyle6
...and the cell styles must contain the desired background colors.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Create color scale in a range of cells in Calc

Post by Villeroy »

If it can't be done in the GUI, it is very unlikely that it could be done by macro code.
If you can to it in the GUI, simply run MRI to inspect what you have done in the GUI.
[Tutorial] Introduction into object inspection with MRI
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply