[Libreoffice-commits] core.git: scripting/examples scripting/Package_ScriptsPython.mk

2020-10-10 Thread Kevin Suo (via logerrit)
 scripting/Package_ScriptsPython.mk |2 
 scripting/examples/python/Capitalise.py|  102 +++--
 scripting/examples/python/HelloWorld.py|   25 ++-
 scripting/examples/python/InsertText.py|   46 +++--
 scripting/examples/python/NamedRanges.py   |7 
 scripting/examples/python/SetCellColor.py  |   27 +++
 scripting/examples/python/TableSample.py   |  131 +
 scripting/examples/python/pythonSamples/TableSample.py |  116 ---
 8 files changed, 264 insertions(+), 192 deletions(-)

New commits:
commit 5fb0e0f68bc9d37d43e0782b903919630852904c
Author: Kevin Suo 
AuthorDate: Sat Oct 10 14:43:55 2020 +0800
Commit: Thorsten Behrens 
CommitDate: Sat Oct 10 14:11:27 2020 +0200

Improve example python code in scripting.

* Modified the code to be more pythonic.
* If "len(theString) == 0", then "not theString" evaluates to True.
* "theString[0].isupper()" and "theString[1].isupper()" can be combined.
* Remove unused imported string module
* Wrap long lines
* run autopep8 to prettify
* ...

Change-Id: Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/104136
Tested-by: Jenkins
Reviewed-by: Thorsten Behrens 

diff --git a/scripting/Package_ScriptsPython.mk 
b/scripting/Package_ScriptsPython.mk
index 0c48839ca42d..9d401d38349b 100644
--- a/scripting/Package_ScriptsPython.mk
+++ b/scripting/Package_ScriptsPython.mk
@@ -15,7 +15,7 @@ $(eval $(call 
gb_Package_add_files_with_dir,scripting_ScriptsPython,$(LIBO_SHARE
python/InsertText.py \
python/NamedRanges.py \
python/SetCellColor.py \
-   python/pythonSamples/TableSample.py \
+   python/TableSample.py \
 ))
 
 # vim: set noet sw=4 ts=4:
diff --git a/scripting/examples/python/Capitalise.py 
b/scripting/examples/python/Capitalise.py
index 05e82a37ad32..64d29a51343a 100644
--- a/scripting/examples/python/Capitalise.py
+++ b/scripting/examples/python/Capitalise.py
@@ -16,61 +16,77 @@
 #   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 #
 
-# helper function
-def getNewString( theString ) :
-if( not theString or len(theString) ==0) :
+def getNewString(theString):
+"""helper function
+"""
+if (not theString):
 return ""
+
 # should we tokenize on "."?
-if theString[0].isupper() and len(theString)>=2 and theString[1].isupper() 
:
-# first two chars are UC => first UC, rest LC
-newString=theString[0:1].upper() + theString[1:].lower();
+if len(theString) >= 2 and theString[:2].isupper():
+# first two chars are UC => first UC, rest LC
+newString = theString[0].upper() + theString[1:].lower()
+
 elif theString[0].isupper():
-# first char UC => all to LC
-newString=theString.lower()
-else: # all to UC.
-newString=theString.upper()
-return newString;
+# first char UC => all to LC
+newString = theString.lower()
+
+else:
+# all to UC.
+newString = theString.upper()
 
-def capitalisePython( ): 
-"""Change the case of a selection, or current word from upper case, to 
first char upper case, to all lower case to upper case..."""
-import string
+return newString
 
+
+def capitalisePython():
+"""Change the case of the selected or current word(s).
+If at least the first two characters are "UPpercase, then it is changed
+to first char "Uppercase".
+If the first character is "Uppercase", then it is changed to
+all "lowercase".
+Otherwise, all are changed to "UPPERCASE".
+"""
 # The context variable is of type XScriptContext and is available to
 # all BeanShell scripts executed by the Script Framework
 xModel = XSCRIPTCONTEXT.getDocument()
 
-#the writer controller impl supports the css.view.XSelectionSupplier 
interface
+# the writer controller impl supports the css.view.XSelectionSupplier
+# interface
 xSelectionSupplier = xModel.getCurrentController()
 
-#see section 7.5.1 of developers' guide
+# see section 7.5.1 of developers' guide
 xIndexAccess = xSelectionSupplier.getSelection()
-count = xIndexAccess.getCount();
-if(count>=1):  #ie we have a selection
-i=0
-while i < count :
-xTextRange = xIndexAccess.getByIndex(i);
-#print "string: " + xTextRange.getString();
-theString = xTextRange.getString();
-if len(theString)==0 :
-# sadly we can have a selection where nothing is selected
-# in this case we get the XWordCursor and make a selection!
-xText = xTextRange.getText();
-xWordCursor = xText.createTextCursorByRange(xTextRange);
-if not xWordCursor.isStartOfWord():
-xWordCursor.gotoStartOfWord(False);
-

[Libreoffice-commits] core.git: scripting/examples scripting/Package_ScriptsPython.mk

2018-03-27 Thread Tor Lillqvist
 scripting/Package_ScriptsPython.mk|1 +
 scripting/examples/python/SetCellColor.py |   15 +++
 2 files changed, 16 insertions(+)

New commits:
commit aa27a25d152ab70f60fedcea3bd4cd99d68103a0
Author: Tor Lillqvist 
Date:   Thu Jan 18 18:27:50 2018 +0200

Add another sample Python script

Change-Id: I542a8b36a097d8961dc76fdcc3d25a3d7b6eb526
Reviewed-on: https://gerrit.libreoffice.org/51966
Tested-by: Jenkins 
Reviewed-by: Tor Lillqvist 

diff --git a/scripting/Package_ScriptsPython.mk 
b/scripting/Package_ScriptsPython.mk
index 1d0de559bd20..8cc8a85472ff 100644
--- a/scripting/Package_ScriptsPython.mk
+++ b/scripting/Package_ScriptsPython.mk
@@ -12,6 +12,7 @@ $(eval $(call 
gb_Package_Package,scripting_ScriptsPython,$(SRCDIR)/scripting/exa
 $(eval $(call 
gb_Package_add_files_with_dir,scripting_ScriptsPython,$(LIBO_SHARE_FOLDER)/Scripts,\
python/Capitalise.py \
python/HelloWorld.py \
+   python/SetCellColor.py \
python/pythonSamples/TableSample.py \
 ))
 
diff --git a/scripting/examples/python/SetCellColor.py 
b/scripting/examples/python/SetCellColor.py
new file mode 100644
index ..743a6daa948b
--- /dev/null
+++ b/scripting/examples/python/SetCellColor.py
@@ -0,0 +1,15 @@
+def SetCellColor(x, y, color):
+"""Sets the background of the cell at (x,y) (zero-based column and row
+   indices, for example (2,3) == C4) on the first sheet and
+   returns the contents of the cell as a string.
+"""
+#get the doc from the scripting context which is made available to all 
scripts
+desktop = XSCRIPTCONTEXT.getDesktop()
+model = desktop.getCurrentComponent()
+#check whether there's already an opened document
+if not hasattr(model, "Sheets"):
+return ""
+sheet = model.Sheets.Sheet1
+cell = sheet.getCellByPosition(x, y)
+cell.CellBackColor = color
+return cell.String
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits