OpenOffice/LibreOffice Calc Macros(4)

填日期的方法,參考這篇:Calc Examples跟這篇Les champs Date / Calendriers

簡單的說,日期要先丟 “d/m/y” 的值進去,或者是用設定公式的方式去設定日期,然後再去設定 format。

[python]
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.Sheets.getByIndex(0)

oSheet.getCellByPosition( 0, 0 ).setFormula( “=DATE(2004;09;30)” )
oSheet.getCellByPosition( 0, 1 ).setFormula( “10/31/2004” )
oSheet.getCellRangeByName( “A3” ).setFormula( “12/31/2004” )

#—–
# Format the date cells as dates.
com_sun_star_util_NumberFormat_DATE = uno.getConstantByName( “com.sun.star.util.NumberFormat.DATE” )
oFormats = oDoc.getNumberFormats()
oLocale = uno.createUnoStruct( “com.sun.star.lang.Locale” )
nDateKey = oFormats.getStandardFormat( com_sun_star_util_NumberFormat_DATE, oLocale )
oCell = oSheet.getCellRangeByName( “A1:A3” )
oCell.NumberFormat = nDateKey
[/python]

至於要改格式為 YYYY/MM/DD 的方法,我還沒研究出來。