使用單一捷徑將目前日期和時間(DATETIME 函數)插入 LibreOffice Calc 的儲存格中

使用單一捷徑將目前日期和時間(DATETIME 函數)插入 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 文件。如何修改此巨集以將格式化的日期時間插入 Calc 中目前選取的儲存格?

'Author: Andrew Pitonyak
'email:   [email protected] 
'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

答案1

在快捷方式之前更改單元格格式是最簡單的解決方案?設定單元格格式

快捷鍵不起作用後設定單元格格式。

宏觀方式...

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

答案2

對於 Excel(針對 Windows 的 MS Excel 2010 進行了測試):將其插入一個儲存格中,無需巨集或其他軟體,唯一的解決方案是依序使用 3 個捷徑:

  • CTRL+;
  • space
  • CTRL+ SHIFT+;

至少這會給你DD-MM-YYYY HH:MM

從那裡您只需將用戶定義的格式更改為您的選擇:YYYY-MM-DD HH:MM:SS

相關內容