Page 1 of 1

[Solved] Use Cell.CharLocale in Calc document

Posted: Wed Nov 19, 2025 12:05 pm
by VBandOoffice
Hi everyone,

I want to use different Language settings within same Calc document.
Every cell has a property Cell.CharLocale. This I want to use to be set according to the actual country. In principal it is working, but if I change this CharLocale there is no effect. For example the value is 1,234 (German decimal point) and I set the CharLocale to "US-en" it looks the same. If I edit this cell language manually it will change to 1.234
How to do same by macro code?

Code: Select all

        Dim Cell As Object
	Dim Locale As New com.sun.star.lang.Locale
	
	cell = ThisComponent.GetSheets().getByIndex(2).getCellByPosition(44,32)
	xray Cell.CharLocale
	
	Locale.Country = "US" 'ISO 3166 (must be "" or valid, or Language may not set)
	Locale.Language = "en" 'ISO 639
	Locale.Variant = ""
	Cell.CharLocale = Locale


Thanks for your time!

Re: Use Cell.CharLocale in Calc document

Posted: Wed Nov 19, 2025 11:55 pm
by karolus
Hallo

You have also to deal with Numberformats, Example in python:

Code: Select all

from com.sun.star.lang import Locale

def set_numberformat(*_):

    loc = Locale( Country="US", Language="en")
    doc = XSCRIPTCONTEXT.getDocument()
    numbers = doc.NumberFormats
    if (nf:=numbers.queryKey("General", loc, True)) == -1:
        nf = numbers.addNew("General", loc)
    cell = doc.Sheets[2][7,2] #C8
    cell.NumberFormat = nf

Re: Use Cell.CharLocale in Calc document

Posted: Thu Nov 20, 2025 9:09 am
by VBandOoffice
Hello Karolus,
this helped me very much - Thank you!
But with every solved problem a new one appears:
Do you know a simple solution to get the decimal separator according to "loc"?
I need it to set the number format. For German I need something like "##,##" but for English it must be "##.##".
Best regards,
VBandOoffice

Re: Use Cell.CharLocale in Calc document

Posted: Thu Nov 20, 2025 12:25 pm
by karolus
VBandOoffice wrote: Thu Nov 20, 2025 9:09 am Hello Karolus,
this helped me very much - Thank you!
But with every solved problem a new one appears:
Do you know a simple solution to get the decimal separator according to "loc"?
I need it to set the number format. For German I need something like "##,##" but for English it must be "##.##".
Best regards,
VBandOoffice
Dont know how to do in BASIC … in python its just like:

Code: Select all

import locale
loc = locale.getlocale()
locale.setlocale(locale.LC_NUMERIC, 'en_US')
number_code = locale.format_string('%5.1f', 1111.1, grouping=True).replace('1','#') 
locale.setlocale(loc)
print( number_code )  # shows »#,###.#« 

Re: Use Cell.CharLocale in Calc document

Posted: Thu Nov 20, 2025 12:33 pm
by VBandOoffice
Hello Karolus,
I was thinking about something similar.
It looks, like there is no better way.

Thank's again
VBandOoffice