[Libreoffice-commits] core.git: wizards/source

Jean-Pierre Ledure (via logerrit) logerrit at kemper.freedesktop.org
Sat May 22 15:46:26 UTC 2021


 wizards/source/scriptforge/SF_PythonHelper.xba   |   35 ++++++++++++++++++++++-
 wizards/source/scriptforge/python/scriptforge.py |    5 +++
 2 files changed, 39 insertions(+), 1 deletion(-)

New commits:
commit 14d62503e951bd26ae531071b031ee362ac7307a
Author:     Jean-Pierre Ledure <jp at ledure.be>
AuthorDate: Sat May 22 16:46:17 2021 +0200
Commit:     Jean-Pierre Ledure <jp at ledure.be>
CommitDate: Sat May 22 17:45:47 2021 +0200

    ScriptForge - (SF_Basic) Add new CDate() method
    
    The CDate() method replicates in Python the behavior of the Basic
    builtin function with the same name, except that it returns
    its unique input argument when it could not be recognized as
    a valid date, while Basic raises an DataType error.
    
    The method is introduced to make it easy to Python devs to
    convert numbers stored in Calc cells and representing dates
    to their corresponding datetime.datetime instance.
    
    Change-Id: Ifd8fbd4bf7a97bf6b91cf481c09d75ec14159916
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115989
    Tested-by: Jean-Pierre Ledure <jp at ledure.be>
    Tested-by: Jenkins
    Reviewed-by: Jean-Pierre Ledure <jp at ledure.be>

diff --git a/wizards/source/scriptforge/SF_PythonHelper.xba b/wizards/source/scriptforge/SF_PythonHelper.xba
index 52ccc1827e52..71cdab87a7cb 100644
--- a/wizards/source/scriptforge/SF_PythonHelper.xba
+++ b/wizards/source/scriptforge/SF_PythonHelper.xba
@@ -57,6 +57,39 @@ End Property	'	ScriptForge.SF_PythonHelper.ServiceName
 
 REM ============================================================== PUBLIC METHODS
 
+REM -----------------------------------------------------------------------------
+Public Function PyCDate(ByVal DateArg As Variant) As Variant
+'''	Convenient function to replicate CDate() in Python scripts
+'''	Args:
+'''		DateArg: a date as a string or as a double
+'''	Returns:
+'''		The converted date as a UNO DateTime structure
+'''		If the input argument could not be recognized as a date, return the argument unchanged
+'''	Example: (Python code)
+'''		a = bas.CDate('2021-02-18')
+
+Dim vDate As Variant				'	Return value
+Const cstThisSub = "Basic.CDate"
+Const cstSubArgs = "datearg"
+
+	On Local Error GoTo Catch
+	vDate = Null
+
+Check:
+	SF_Utils._EnterFunction(cstThisSub, cstSubArgs)
+
+Try:
+	vDate = CDate(DateArg)
+
+Finally:
+	If VarType(vDate) = V_DATE Then PyCDate = CDateToUnoDateTime(vDate) Else PyCDate = DateArg
+	SF_Utils._ExitFunction(cstThisSub)
+	Exit Function
+Catch:
+	On Local Error GoTo 0
+	GoTo Finally
+End Function	'	ScriptForge.SF_PythonHelper.PyCDate
+
 REM -----------------------------------------------------------------------------
 Public Function PyConvertFromUrl(ByVal FileName As Variant) As String
 '''	Convenient function to replicate ConvertFromUrl() in Python scripts
@@ -295,7 +328,7 @@ Public Function PyDateValue(ByVal DateArg As Variant) As Variant
 '''	Args:
 '''		DateArg: a date as a string
 '''	Returns:
-'''		The converted date as a string in iso format
+'''		The converted date as a UNO DateTime structure
 '''	Example: (Python code)
 '''		a = bas.DateValue('2021-02-18')
 
diff --git a/wizards/source/scriptforge/python/scriptforge.py b/wizards/source/scriptforge/python/scriptforge.py
index bb8635936ed5..39eb548032cf 100644
--- a/wizards/source/scriptforge/python/scriptforge.py
+++ b/wizards/source/scriptforge/python/scriptforge.py
@@ -624,6 +624,11 @@ class SFScriptForge:
         MB_OK, MB_OKCANCEL, MB_RETRYCANCEL, MB_YESNO, MB_YESNOCANCEL = 0, 1, 5, 4, 3
         IDABORT, IDCANCEL, IDIGNORE, IDNO, IDOK, IDRETRY, IDYES = 3, 2, 5, 7, 1, 4, 6
 
+        @classmethod
+        def CDate(cls, datevalue):
+            cdate = cls.SIMPLEEXEC(cls.module + '.PyCDate', datevalue)
+            return cls.CDateFromUnoDateTime(cdate)
+
         @staticmethod
         def CDateFromUnoDateTime(unodate):
             """


More information about the Libreoffice-commits mailing list