使用单个快捷方式将当前日期和时间(DATETIME 函数)插入 LibreOffice Calc 的单元格中

con*_*tti 4 linux keyboard-shortcuts libreoffice-calc

我知道有是CTRL+;快捷键来插入当前日期和CTRL+ SHIFT+;插入当前时间。

但是我有两个问题:

1)我想为两者都有一个快捷方式

2) 我希望能够使用自定义日期时间格式 ( YYYY-MM-DD HH:MM:SS)

我的语言默认格式是MM/DD/YY HH:MM:SS pm/am-我希望改变这种情况。我想专门为该快捷方式使用自定义格式,最好不要涉及xdotool或类似的外部宏软件或全局系统范围的快捷方式的解决方案。

里面的功能Tools -> Customize -> Keyboard似乎没有提供任何帮助。

为什么我不想使用 xdotool;直接在 LibreOffice 中的解决方案是最好的。)


我在这里找到了以下 OpenOffice 宏代码,但它说它只适用于 Writer 文档。我将如何修改此宏以将格式化的 DATE-TIME 插入到 Calc 中当前选定的单元格中?

'Author: Andrew Pitonyak
'email:   andrew@pitonyak.org 
'uses:  FindCreateNumberFormatStyle
Sub InsertDateField
  Dim oDoc
  Dim oText
  Dim oVCurs
  Dim oTCurs
  Dim oDateTime
  Dim s$

  oDoc = ThisComponent
  If oDoc.SupportsService("com.sun.star.text.TextDocument") Then
    oText = oDoc.Text
    oVCurs = oDoc.CurrentController.getViewCursor()
    oTCurs = oText.createTextCursorByRange(oVCurs.getStart())
    oText.insertString(oTCurs, "Today is ", FALSE)
    ' Create the DateTime type.
    s = "com.sun.star.text.TextField.DateTime"
    ODateTime = oDoc.createInstance(s)
    oDateTime.IsFixed = TRUE
    oDateTime.NumberFormat = FindCreateNumberFormatStyle(_
      "DD. MMMM YYYY", oDoc)

    oText.insertTextContent(oTCurs,oDateTime,FALSE)
    oText.insertString(oTCurs," ",FALSE)
  Else
    MsgBox "Sorry, this macro requires a TextDocument"
  End If
End Sub
Run Code Online (Sandbox Code Playgroud)

小智 7

在快捷方式之前更改单元格的格式是最简单的解决方案?格式化单元格

快捷方式后格式化单元格不起作用。

宏方式...

Sub Main

    Dim Doc As Object
    Dim Sheet As Object
    Dim Cell As Object
    Dim NumberFormats As Object
    Dim NumberFormatString As String
    Dim NumberFormatId As Long
    Dim LocalSettings As New com.sun.star.lang.Locale

    Doc = ThisComponent
    Sheet = Doc.Sheets(0)
    Cell = Doc.getCurrentSelection
    Column = Cell.CellAddress.Column
    Row = Cell.CellAddress.Row

    Cell.Value = Now()

    LocalSettings.Language = "en"
    LocalSettings.Country = "us"

    NumberFormats = Doc.NumberFormats
    NumberFormatString = "YYYY-MM-DD HH:MM:SS"

    NumberFormatId = NumberFormats.queryKey(NumberFormatString, LocalSettings, True)
    If NumberFormatId = -1 Then
    NumberFormatId = NumberFormats.addNew(NumberFormatString, LocalSettings)
    End If

    MsgBox NumberFormatId
    Cell.NumberFormat = NumberFormatId

End Sub
Run Code Online (Sandbox Code Playgroud)