diff options
author | Jean-Pierre Ledure <jp@ledure.be> | 2021-05-22 16:46:17 +0200 |
---|---|---|
committer | Jean-Pierre Ledure <jp@ledure.be> | 2021-05-22 17:45:47 +0200 |
commit | 14d62503e951bd26ae531071b031ee362ac7307a (patch) | |
tree | d7d53959f03f8b9b7392681e2a9b8284bedf1a17 /wizards | |
parent | c1bf2a3751b6a602f2571f8642b36504b04ca3fd (diff) |
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@ledure.be>
Tested-by: Jenkins
Reviewed-by: Jean-Pierre Ledure <jp@ledure.be>
Diffstat (limited to 'wizards')
-rw-r--r-- | wizards/source/scriptforge/SF_PythonHelper.xba | 35 | ||||
-rw-r--r-- | wizards/source/scriptforge/python/scriptforge.py | 5 |
2 files changed, 39 insertions, 1 deletions
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 @@ -58,6 +58,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 ''' Args: @@ -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): """ |