Edit: 2009-06-08: Found some interesting new options with formatted fields setting FormulaLocal rather than the text value. See comments near the end of the code. Added 2 more examples to the attached document. |
Edit: 2015-08-13: Added LibreOffice compatibility. |
Date controls and time controls can not be bound to sheet cells. I think this is because both types are not directly supported by spreadsheets. Only databases have distinct field types for dates, times and time-stamps.
I have no idea why the currency fields (producing double floats) and formatted text fields are not supported.
The following macro can write the value of the above mentioned 4 control types into a certain cell. The cell is supposed to be named according to the respective control, so the macro writes the value of control "myDate" into a named cell "myDate".
The code is designed to work independent from the formatting of either the control or the cell. It just puts the right value into the right cell. Format the cell to your liking.
Code: Select all
REM ***** BASIC *****
Sub FormattedControlToCell(oEv)
REM Link named cells to values of date, time, currency and pattern controls.
REM Name the cells according to the respective control names.
REM Assign the "Text modified" event of the control to this macro.
REM The macro just puts the right value leaving all formatting to the user.
obj = oEv.Source
'mymri obj
sName = obj.Model.Name
oName = ThisComponent.NamedRanges.getByName(sName)
oCell = oName.getReferredCells()
if HasUnoInterfaces(obj, "com.sun.star.awt.XDateField")then
'OpenOffice getDate() returns ISO-date as integer YYYYMMDD, for instance: 19991231
'LibreOffice getDate() returns an UNO struct
vD = obj.getDate()
if isUnoStruct(vD) then
REM this is LibreOffice
sVal = format(vD.Year,"0000") & format(vD.Month, "00") & format(vD.Day, "00")
else 'OpenOffice
sVal = Format(vD,"00000000")
endif
' we let the spreadsheet calculate the date from the split digits
oCell.setFormula("=DATE("& left(sVal,4) &";"& mid(sVal,5,2) &";"& right(sVal,2) &")")
oCell.setValue(oCell.getValue())
elseif HasUnoInterfaces(obj, "com.sun.star.awt.XTimeField")then
'OpenOffice getTime() returns time as integer HHMMSS00, for instance: 23304599
'LibreOffice getTime() returns an UNO struct
vT = obj.getTime()
if isUnoStruct(vT) then
REM this is LibreOffice
sVal = format(vT.Hours,"00") & format(vT.Minutes, "00") & format(vT.Seconds, "00") & format(vT.NanoSeconds, "00")
else 'OpenOffice
sVal = Format(vT,"00000000")
endif
' spreadsheet times are fractions of a day
hh = cDbl(left(sVal,2))/24 ' hours
mm = cDbl(mid(sVal,3,2))/1440 'minutes
ss = cDbl(right(sVal,4))/8640000 '1/100th sec.
oCell.setValue(hh + mm + ss)
elseif HasUnoInterfaces(obj, "com.sun.star.awt.XCurrencyField")then
oCell.setValue(obj.getValue()) 'it's a double
elseif HasUnoInterfaces(obj, "com.sun.star.awt.XPatternField")then
REM FormulaLocal treats the string like input in the formula bar,
REM including numeric evaluation and formula-input.
oCell.FormulaLocal = obj.getString()
REM Apply special number format "Text" to avoid evaluation or
REM use this line instead of FormulaLocal:
REM oCell.setString(obj.getString())
endif
End Sub