| View previous topic :: View next topic |
| Author |
Message |
zjcboy General User

Joined: 26 Nov 2003 Posts: 8
|
Posted: Wed Nov 26, 2003 6:04 am Post subject: shortcut key to enter current date? |
|
|
| what's it? Thanks! |
|
| Back to top |
|
 |
carl Super User


Joined: 21 Apr 2003 Posts: 920 Location: Germany
|
Posted: Wed Nov 26, 2003 6:25 am Post subject: |
|
|
date() _________________ carl
Using OpenOffice.org 2 on XP sp2 |
|
| Back to top |
|
 |
carl Super User


Joined: 21 Apr 2003 Posts: 920 Location: Germany
|
Posted: Wed Nov 26, 2003 6:27 am Post subject: |
|
|
sorry its NOW()
which shows how quickly I can forget any and everything _________________ carl
Using OpenOffice.org 2 on XP sp2 |
|
| Back to top |
|
 |
zjcboy General User

Joined: 26 Nov 2003 Posts: 8
|
Posted: Thu Nov 27, 2003 2:55 am Post subject: |
|
|
well, using now() is ok but here's a problem.
I just want to enter the date in a cell, then let it be when next time I open the file. but if I use now(), it will change from time to time I open the file.
any idea? thanks! |
|
| Back to top |
|
 |
Mike Guest
|
Posted: Thu Nov 27, 2003 7:00 am Post subject: Answer ;-) |
|
|
Hi!
Create a macro:
Extras/Macro/Macro...
Enter a name for the new macro (for example 'InsertDateIntoCell')
Press the New-Button
Use the following code for the macro:
sub InsertDateIntoCell
'Macro to insert current time into selected cell
'Edited version of the "InsertDateIntoCell" macro from Andrew Pitonyak's Macro document
'Andrew Pitonyak's Macro document can be found at http://www.pitonyak.org/AndrewMacro.sxw (andrew@pitonyak.org)
'Edited by Russ Phillips, 2003-08-13 (avantman42@users.sourceforge.net)
Dim oDesktop As Object, oController As Object, oSelection As Object
Dim doc As Object
oDesktop = createUnoService("com.sun.star.frame.Desktop")
oController = oDesktop.CurrentFrame.Controller
doc = oController.Model
If doc.SupportsService("com.sun.star.sheet.SpreadsheetDocument") Then
oSelection = oController.Selection
'Set the date value
oFunction = CreateUnoService("com.sun.star.sheet.FunctionAccess")
oFunction.NullDate = doc.NullDate
Dim aEmpty()
oSelection.Value = oFunction.callFunction("NOW", aEmpty())
'Set cell format
oFormats = doc.NumberFormats
dim aLocale as new com.sun.star.lang.Locale
oSelection.NumberFormat = oFormats.getStandardFormat(com.sun.star.util.NumberFormat.DATE, aLocale)
Else
MsgBox "This macro must be run in a spreadsheet document"
End If
end sub
Close the Window (called 'BASIC ....')
Now you can tell OpenOffice to execute your new macro when pressing a keyboard-shortcut. To do so, go to Extras/Macro/Macro..., select your macro and then press the third button (it is called 'Zuordnen...' in the german version, sorry I don't know its english name) and select the tab 'Keyboard'. Then select your macro from the list on the bottom left. You should find it under 'OpenOffice.org/Standard/Module1'. Then select your macro (InsertDateIntoCell) from the list in the middle. Finally assign it any keyboard-shortcut you like.
Greetings
Mike |
|
| Back to top |
|
 |
zjcboy General User

Joined: 26 Nov 2003 Posts: 8
|
Posted: Fri Nov 28, 2003 6:08 am Post subject: |
|
|
great !!!!!!!!!!!
that's exactly what I want. Thanks!!!
one more question: when I set a shortcut key to the macro, I found I have to choose any of the given ones in the list above. I wonder if I could set any key I'd like ( for example, like in Excel , press Ctrl + ; ) . If so, how can I ? |
|
| Back to top |
|
 |
zjcboy General User

Joined: 26 Nov 2003 Posts: 8
|
Posted: Fri Nov 28, 2003 6:12 am Post subject: an improvement in the macro |
|
|
I delete these a few lines:
'Set cell format
oFormats = doc.NumberFormats
dim aLocale as new com.sun.star.lang.Locale
oSelection.NumberFormat = oFormats.getStandardFormat(com.sun.star.util.NumberFormat.DATE, aLocale)
because I don't want it to change the format of a certain cell I've already set.  |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Nov 28, 2003 1:00 pm Post subject: |
|
|
glad to help you.
I had the same trouble with the impossibility of assigning the 'good old' excel shortcut-key, so I already posted a question asking if this is possible somehow, but I didn't get an answer till now.
If I get one, I'll let you know.
Mike |
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8978 Location: Lexinton, Kentucky, USA
|
Posted: Fri Nov 28, 2003 2:25 pm Post subject: |
|
|
| I have never seen anyone indicate that you can set up shortcut keys other than those shown on the Keyboard tab. Right or wrong, at least you now have an answer. FWIW, I think it's right unless you do some heavy duty programing of your own. |
|
| Back to top |
|
 |
sinkingfast Newbie

Joined: 07 Mar 2010 Posts: 1 Location: Toronto, ON
|
Posted: Mon Nov 22, 2010 7:55 am Post subject: Macro to set and keep todays date in sheet |
|
|
And here we are 7 years later using openoffice.org and this macro still works as is. The trick was to get it into a macro because OO doesn't allow you to create a macro a text directly. However you can use the record macro tool to create a test macro & then go tools->macros->organize...->OpenOffice Basic Macros, find the 'test' you created and replace it with the code shown above. Settings to assign it to a shortcut key are different but can easily be done. Many thanks to Mike.
JA |
|
| Back to top |
|
 |
dirkocker Newbie

Joined: 10 Jan 2012 Posts: 1
|
Posted: Tue Jan 10, 2012 1:46 am Post subject: Another solution from a german oo tutorial i found |
|
|
Hi everybody,
I was also looking for a shortcut to insert Date and Time in Calc in OpenOffice 3.3.0
The solution of Mike is working well for Date. Thanks Mike.
The solution on this site shows some makro code to do in it in a mor flexible way.
http://www.openoffice.org/de/doc/howto/tutorials/howto_insert_date_de.html
Now I just do a copy past from the other site.
Cheers
DO
| Code: | Sub Insert_Date_As_Text
Insert_As_Text( Date )
End Sub
Sub Insert_Date_As_Value
Insert_As_Value( Date, "TT. MMM JJJJ", com.sun.star.util.NumberFormat.DATE )
End Sub
Sub Insert_Time_As_Text
Insert_As_Text( Time )
End Sub
Sub Insert_Time_As_Value
Insert_As_Value( Time, "HH:MM:SS", com.sun.star.util.NumberFormat.TIME )
End Sub
|
| Code: | '-------------------------------------------------
' Inserts the passed string into a document.
'
' parameter aValue
' The string to be inserted into the document.
'-------------------------------------------------
Sub Insert_As_Text( aValue As String )
oDesktop = createUnoService( "com.sun.star.frame.Desktop" )
oController = oDesktop.CurrentFrame.Controller
oDocument = oController.Model
' insert as text for Calc ----------------------
If oDocument.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) Then
oSelection = oController.Selection
oSelection.Formula = "'" & aValue ' apostroph forces string
' insert as text for Writer --------------------
ElseIf oDocument.supportsService( "com.sun.star.text.TextDocument" ) Then
oSelection = oDocument.CurrentSelection( 0 )
oSelection.Text.insertString( oSelection, aValue, True )
EndIf
End Sub |
| Code: |
'-------------------------------------------------
' Sets the passed number format as property of an object.
'
' parameter oController
' The controller that contains a number formatter.
'
' parameter oDestObj
' The destination object. Format is set at its NumberFormat property.
'
' parameter aFormat
' The string representation of the number format,
' or empty string to use the passed default format.
'
' parameter nStdFormat
' Index of a default number format (com.sun.star.util.NumberFormat
' constants), if no string is specified.
'-------------------------------------------------
Sub Set_Number_Format(_
oController As Object,_
oDestObj As Object,_
aFormat As String,_
nStdFormat As Integer )
Dim aLocale As New com.sun.star.lang.Locale
oFormatter = oController.Model.NumberFormats
If aFormat = "" Then
oDestObj.NumberFormat = oFormatter.getStandardFormat( nStdFormat, aLocale )
Else
Dim nKey As Long
nKey = oFormatter.queryKey( aFormat, aLocale, True )
If nKey < 0 Then
nKey = oFormatter.addNew( aFormat, aLocale )
EndIf
oDestObj.NumberFormat = nKey
EndIf
End Sub
|
| Code: |
'-------------------------------------------------
' Inserts the passed string as value into a document.
'
' parameter aValue
' The string representation of a value (date or time).
'
' parameter aFormat
' The string representation of the number format,
' or empty string to use the passed default format.
'
' parameter nStdFormat
' Index of a default number format (com.sun.star.util.NumberFormat
' constants), if no string is specified.
'-------------------------------------------------
Sub Insert_As_Value(_
aValue As String,_
aFormat As String,_
nStdFormat As Integer )
oDesktop = createUnoService( "com.sun.star.frame.Desktop" )
oController = oDesktop.CurrentFrame.Controller
oDocument = oController.Model
' insert as value for Calc ---------------------
If oDocument.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) Then
oSelection = oController.Selection
oSelection.FormulaLocal = aValue
Set_Number_Format( oController, oSelection, aFormat, nStdFormat )
' insert as value for Writer -------------------
ElseIf oDocument.supportsService( "com.sun.star.text.TextDocument" ) Then
' create a text field
oField = oDocument.createInstance( "com.sun.star.text.TextField.DateTime" )
oField.IsFixed = True
Set_Number_Format( oController, oField, aFormat, nStdFormat )
' insert into current selection
oSelection = oDocument.CurrentSelection( 0 )
oSelection.Text.insertTextContent( oSelection, oField, True )
End If
End Sub
|
|
|
| Back to top |
|
 |
Villeroy Super User


Joined: 04 Oct 2004 Posts: 10065 Location: Germany
|
Posted: Tue Jan 10, 2012 3:29 am Post subject: |
|
|
The above macro code does not really work. It happens to work if your are running the office under a German locale setting and if you happen to have a single cell selected.
The easiest macro gets the currently active input cell using a helper function getActiveCell. You find it on this forum when you manage to hit the [Search] link on top.
It simply inserts the correct cell value which is a number. It does not touch the formatting, leaving it up to you.
| Code: | Sub InsertNow()
oCell = getActiveCell(ThisComponent.getCurrentController())
oCell.setFormula("=NOW()")
oCell.setValue(oCell.getValue())
End Sub |
Many more methods: http://user.services.openoffice.org/en/forum/viewtopic.php?f=21&t=12575
The more elegant ones do not use any macro at all. _________________ Rest in peace, oooforum.org
Get help on http://forum.openoffice.org |
|
| Back to top |
|
 |
|