summaryrefslogtreecommitdiff
path: root/wizards
diff options
context:
space:
mode:
authorJean-Pierre Ledure <jp@ledure.be>2021-05-22 16:46:17 +0200
committerJean-Pierre Ledure <jp@ledure.be>2021-05-22 17:45:47 +0200
commit14d62503e951bd26ae531071b031ee362ac7307a (patch)
treed7d53959f03f8b9b7392681e2a9b8284bedf1a17 /wizards
parentc1bf2a3751b6a602f2571f8642b36504b04ca3fd (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.xba35
-rw-r--r--wizards/source/scriptforge/python/scriptforge.py5
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 &apos; ScriptForge.SF_PythonHelper.ServiceName
REM ============================================================== PUBLIC METHODS
REM -----------------------------------------------------------------------------
+Public Function PyCDate(ByVal DateArg As Variant) As Variant
+&apos;&apos;&apos; Convenient function to replicate CDate() in Python scripts
+&apos;&apos;&apos; Args:
+&apos;&apos;&apos; DateArg: a date as a string or as a double
+&apos;&apos;&apos; Returns:
+&apos;&apos;&apos; The converted date as a UNO DateTime structure
+&apos;&apos;&apos; If the input argument could not be recognized as a date, return the argument unchanged
+&apos;&apos;&apos; Example: (Python code)
+&apos;&apos;&apos; a = bas.CDate(&apos;2021-02-18&apos;)
+
+Dim vDate As Variant &apos; Return value
+Const cstThisSub = &quot;Basic.CDate&quot;
+Const cstSubArgs = &quot;datearg&quot;
+
+ 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 &apos; ScriptForge.SF_PythonHelper.PyCDate
+
+REM -----------------------------------------------------------------------------
Public Function PyConvertFromUrl(ByVal FileName As Variant) As String
&apos;&apos;&apos; Convenient function to replicate ConvertFromUrl() in Python scripts
&apos;&apos;&apos; Args:
@@ -295,7 +328,7 @@ Public Function PyDateValue(ByVal DateArg As Variant) As Variant
&apos;&apos;&apos; Args:
&apos;&apos;&apos; DateArg: a date as a string
&apos;&apos;&apos; Returns:
-&apos;&apos;&apos; The converted date as a string in iso format
+&apos;&apos;&apos; The converted date as a UNO DateTime structure
&apos;&apos;&apos; Example: (Python code)
&apos;&apos;&apos; a = bas.DateValue(&apos;2021-02-18&apos;)
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):
"""