Edit: 2009-03-28: How to insert a time stamp into Calc without any macro |
Using a shortcut to NOW() (a tiny little bit shorter):
Call menu:Insert>Names>Define...[Ctrl+F3]
Name: n
Refers to: NOW()
Hit buttons [Add], [OK]
Type into a cell: =n F9,Enter,Enter
NOW() returns the full date and time info as a formatted number. Having such a full stamp in a A1, =INT(A1) returns the day-portion, MOD(A1;1) returns the time-fraction only. You can use number formats to display only the date or the time without changing the value.
Edit: 2010-12-29: How to insert a time stamp depending on another cell value into Calc without any macro |
Code: Select all
Cell B1: =IF(A1="";"";IF(B1="";NOW();B1))
Thanks to this forum's most valued member Robert Tucker pointing us to http://www.mcgimpsey.com/excel/timestamp.html
Several macros to write time stamps into spreadsheet cells. It writes naked numeric values, leaving all formatting up to you. Straight ahead, no gimmicks, no error-handling. Just tested against the assumed scenarios.
1. NowToActiveCell
Writes the time stamp to the currently active input cell. Best used with a shortcut.
2a. NowToTimeStampCell
Writes the time stamp to a named cell "TimeStamp" anywhere in the current document.
2b. NowToEmptyTimeStampCell
Same as above, leaving alone an already filled stamp-cell.
3.a NowToColumnAByValidation
Triggered by a failed validation. Writes the time stamp to the same row's column "A" when a validated cell has been modified. Set validation to "Text Length = 0" with action set to "Macro". The validation fails, when you put something into the cell (length becomes >0), which triggers the macro.
3b. NowToEmptyColumnAByValidation
Same as above, leaving alone an already filled stamp-cell.
Python Install
Click the install button in the attached document. See edit notes of 2021-07-21.
Basic Install
Re: [Tutorial] How to install a code snippet
Edit: 2008-12-21 Replaced Python attachment. It can be called from a menu or button now. |
Edit: 2010-09-24: Simplified the Python module, added NowToRightNeigbour |
Code: Select all
REM ***** BASIC *****
REM I won't update this Basic code. You may refer to the latest Python
Sub test_splitStringAddress()
REM instead of splitting the string address you may simply use the active cell,
REM ignoring the strings from the validation trigger. See updated Python code
Dim s$,sSh$,sRg$
a=Array("'my sheet'.A1", _
"$'my sheet'.A", _
"'file:///doc.ods'#'my sheet'.A1", _
"$'file:///doc.ods'#$'my sheet'.A")
for i= 0 to uBound(a())
s = a(i)
splitStringAddress(s,sSh,sRg)
print sSh,sRg
next
End Sub
sub splitStringAddress(s$, sSheet$, sRange$)
dim sURL$, linkPos%, sheetPos%
' remove leading $ in any case:
if left(s, 1) = "$" then s = mid(s, 2)
linkPos = instr(s, "'#")
if linkPos > 0 then
sheetPos = instr(linkPos, s, ".")
sURL = left(s, linkPos +1)
sSheet = mid(s, linkPos +2, sheetPos -linkPos -2)
sRange = mid (s, sheetPos +1)
else
sheetPos = instr(s, ".")
if sheetPos > 0 then
sSheet = left(s, sheetPos -1)
sRange = mid (s, sheetPos +1)
else
sRange = s
endif
endif
if left(sSheet, 1) = "$" then sSheet = mid(sSheet, 2)
if (left(sSheet, 1) = "'") and (right(sSheet, 1) = "'") then
sSheet = mid(sSheet, 2, len(sSheet) -2)
endif
sSheet = sURL & sSheet
end sub
'-------usefull helper-function, returning focussed cell
'by UROS > http://www.oooforum.org/forum/viewtopic.phtml?t=19348
REM 2006-08-09: fixed error when row > 8191
' ;sh; ;lSheet +3
'100/60/0;1;tw:309;2/2/0/0/0/0/2/0/0/0/0;253/8191/0/0/0/0/2/246/0/0/8158;0/0/0/0/0/0/2/0/0/0/0
'100/60/0;1;tw:309;2/2/0/0/0/0/2/0/0/0/0;253+8192+0+0+0+0+2+246+0+0+8158;0/0/0/0/0/0/2/0/0/0/0
Function getActiveCell(oView)
Dim as1(), lSheet&,lCol&,lRow$, sDum as String,bErr as Boolean
as1() = Split(oView.ViewData, ";")
lSheet = CLng(as1(1))
sDum = as1(lSheet +3)
as1() = Split(sDum, "/")
on error goto errSlash
lCol = CLng(as1(0))
lRow = CLng(as1(1))
on error goto 0
getActiveCell = oView.Model.getSheets.getByIndex(lSheet).getcellByPosition(lCol,lRow)
exit Function
errSlash:
if NOT(bErr) then
bErr = True
as1() = Split(sDum, "+")
resume
endif
End Function
Sub printStamp(oCell)
'''Put the current time into the active cell.
'No formatting intended. Apply any date/time formatting you like.'''
oCell.setFormula("=NOW()")
oCell.setValue(oCell.getValue())
End Sub
Sub _NowToColumnAByValidation(sAddr, bOverwrite)
Dim s$,r$
iCol = 0
doc = thisComponent
splitStringAddress(sAddr,s,r)
sh = doc.Sheets.getByName(s)
rg = sh.getCellRangeByName(r)
iRow = rg.RangeAddress.StartRow
c = sh.getCellByPosition(iCol, iRow)
if bOverwrite or (len(c.getFormula())=0)then printStamp(c)
End Sub
Sub _NowToTimeStampCell(bOverwrite)
oNames = thisComponent.NamedRanges
oName = oNames.getByName("TimeStamp")
oRg = oName.getReferredCells()
c = oRg.getCellByPosition(0,0)
if bOverwrite or (len(c.getFormula())=0)then printStamp(c)
End Sub
Sub NowToEmptyColumnAByValidation(sFormula, sAddr):
'''Current time to empty column A, triggered by failing validation'''
_NowToColumnAByValidation(sAddr, False)
End Sub
Sub NowToColumnAByValidation(sFormula, sAddr):
'''Current time to column A, triggered by failing validation'''
_NowToColumnAByValidation(sAddr, True)
End Sub
Sub NowToTimeStampCell():
'''Put current time into a cell named "TimeStamp"'''
_NowToTimeStampCell(True)
End Sub
Sub NowToEmptyTimeStampCell():
'''Put current time into an empty cell named "TimeStamp"'''
_NowToTimeStampCell(False)
End Sub
Sub NowToActiveCell():
'''Put current time into the currently active input cell'''
oDoc = thisComponent
oView = oDoc.getCurrentController()
oCell = getActiveCell(oView)
printStamp(oCell)
End Sub
Edit: 2009-03-28: Adding a link to another approach involving c.s.s.util.XModifyListener. Not as easy to implement since the listener needs to be attached to the respective range(s). Trouble with race conditions when used with volatile functions such as NOW(). This is why I suggest the NowToColumnAByValidation with the validation-trick: Re: Run a python macro when cell contents change |
Edit: 2016-11-04: Made the code Python3 compatible. Works with recent versions of LibreOffice |
Edit: 2021-07-21: Added milliseconds to time stamp. Apply date-time format in case of default format. Wrapped the Python code into an installer document. |