[Solved] Put date in cell with CellContentType as Value

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
luofeiyu
Posts: 55
Joined: Thu Sep 14, 2017 2:11 am

[Solved] Put date in cell with CellContentType as Value

Post by luofeiyu »

With the following code, “2025-09-01” was shown in message box,but the content in cell “a1” is “2025” , its CellContentType is value.

Code: Select all

Sub WriteFormattedDateTime()
    Dim oSheet As Object
    Dim oCell As Object
    Dim sFormattedDate As String
    oSheet = ThisComponent.Sheets.getByName("Sheet1")
    oCell = oSheet.getCellByPosition(0, 0)  
    sFormattedDate = Format(Now(), "YYYY-MM-DD")
    msgbox sFormattedDate
    oCell.value = sFormattedDate
End Sub
With the following code, “2025-09-01” was shown in message box,but the content in cell “a1” is “2025-09-01” , its CellContentType is text.

Code: Select all

Sub WriteFormattedDateTime()
    Dim oSheet As Object
    Dim oCell As Object
    Dim sFormattedDate As String
    oSheet = ThisComponent.Sheets.getByName("Sheet1")
    oCell = oSheet.getCellByPosition(0, 0)  
    sFormattedDate = Format(Now(), "YYYY-MM-DD")
    msgbox sFormattedDate
    oCell.string = sFormattedDate
End Sub
How can write whole date “2025-09-01” into cell and set CellContentType as value ?
Last edited by luofeiyu on Mon Sep 01, 2025 5:54 pm, edited 1 time in total.
LibreOffice 24.8 on Debian 12
FJCC
Moderator
Posts: 9563
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Write date in cell and set CellContentType as value?

Post by FJCC »

This code sets the value of A1 to today's date value and sets the number format to YYYY-MM-DD. Is that what you want?

Code: Select all

Sub WriteFormattedDateTime()
    Dim oSheet As Object
    Dim oCell As Object
    Dim sFormattedDate As String
    Dim Locale as New com.sun.star.lang.Locale
    oSheet = ThisComponent.Sheets.getByName("Sheet1")
    oCell = oSheet.getCellByPosition(1, 0)  
 
    DateVal = int(Now())
    
    Locale.Country = "US"
    Locale.Language = "en"
    
    FormatNumber = ThisComponent.NumberFormats.queryKey("YYYY-MM-DD", Locale, True)
    'msgbox FormatNumber
    oCell.value = DateVal
    oCell.NumberFormat = FormatNumber
End Sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Zizi64
Volunteer
Posts: 11493
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] Put date in cell with CellContentType as Value

Post by Zizi64 »

...Or you can set a predefined cell style by your macro:

Code: Select all

Sub WriteFormattedDateTime()
 Dim oSheet As Object
 Dim oCell As Object
 
	oSheet = ThisComponent.Sheets.getByName("Sheet1")
	oCell = oSheet.getCellByPosition(0, 0)  
	oCell.value =  Now()
	oCell.CellStyle = "MyDateTime"
End Sub
I suggest you to use Cell Styles instead of the direct formatting method.
SetCellStyle.ods
(10.27 KiB) Downloaded 2 times
Last edited by Zizi64 on Mon Sep 01, 2025 8:07 pm, edited 2 times in total.
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
karolus
Volunteer
Posts: 1231
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] Put date in cell with CellContentType as Value

Post by karolus »

Crosspost without Reference
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (Bookworm) (on RaspberryPI5)
Post Reply