[Solved] Use Cell.CharLocale in Calc document

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
VBandOoffice
Posts: 36
Joined: Tue Jul 31, 2018 10:11 am

[Solved] Use Cell.CharLocale in Calc document

Post 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!
Last edited by VBandOoffice on Thu Nov 20, 2025 12:34 pm, edited 1 time in total.
LibreOffice 25.8, Windows 10, C# VS2022
User avatar
karolus
Volunteer
Posts: 1236
Joined: Sat Jul 02, 2011 9:47 am

Re: Use Cell.CharLocale in Calc document

Post 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
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
VBandOoffice
Posts: 36
Joined: Tue Jul 31, 2018 10:11 am

Re: Use Cell.CharLocale in Calc document

Post 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
LibreOffice 25.8, Windows 10, C# VS2022
User avatar
karolus
Volunteer
Posts: 1236
Joined: Sat Jul 02, 2011 9:47 am

Re: Use Cell.CharLocale in Calc document

Post 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 »#,###.#« 
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
VBandOoffice
Posts: 36
Joined: Tue Jul 31, 2018 10:11 am

Re: Use Cell.CharLocale in Calc document

Post by VBandOoffice »

Hello Karolus,
I was thinking about something similar.
It looks, like there is no better way.

Thank's again
VBandOoffice
LibreOffice 25.8, Windows 10, C# VS2022
Locked