diff options
author | Tor Lillqvist <tml@collabora.com> | 2018-01-19 18:00:46 +0200 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2018-04-03 14:13:32 +0200 |
commit | c440e599b4adfc06e4c3c40ab195c1215b077224 (patch) | |
tree | b849d441eae1d65bdc55bfb58b8f75412386ec82 /scripting | |
parent | 5c06f1eb53f818bbf1d25eeac10201168a67e52e (diff) |
Add another sample Python script, to handle named ranges in spreadsheets
Change-Id: Ibe11ab2c3513a05b9aec574602b24df70270908c
Reviewed-on: https://gerrit.libreoffice.org/51968
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Tor Lillqvist <tml@collabora.com>
(cherry picked from commit e32b4c8a079e4b51b1028d2467b872ff5b8cdd3a)
Reviewed-on: https://gerrit.libreoffice.org/52092
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Miklos Vajna <vmiklos@collabora.co.uk>
Diffstat (limited to 'scripting')
-rw-r--r-- | scripting/examples/python/NamedRanges.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/scripting/examples/python/NamedRanges.py b/scripting/examples/python/NamedRanges.py new file mode 100644 index 000000000000..abdef141f397 --- /dev/null +++ b/scripting/examples/python/NamedRanges.py @@ -0,0 +1,37 @@ +import traceback +import uno + +def GetNamedRanges(): + """Returns a list of the named ranges in the document. + """ + try: + desktop = XSCRIPTCONTEXT.getDesktop() + model = desktop.getCurrentComponent() + rangeNames = model.NamedRanges.ElementNames + result = [] + for i in rangeNames: + range = model.NamedRanges.getByName(i).Content + result.append((i, range)) + return result + except Exception as e: + print("Caught Exception: " + str(e)) + tb = e.__traceback__ + traceback.print_tb(tb) + return None + +def DefineNamedRange(sheet, x0, y0, width, height, name): + """Defines a new (or replaces an existing) named range on a sheet, + using zero-based absolute coordinates + """ + desktop = XSCRIPTCONTEXT.getDesktop() + model = desktop.getCurrentComponent() + # FIXME: Is there some Python-callable API to turn a row and column into an A1 string? + # This obviously works only for the first 26 columns. + abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + content = sheet +"!" + "$" + abc[x0 : x0+1] + "$" + str(y0+1) + ":" + "$" + abc[x0+width-1 : x0+width] + "$" + str(y0+height) + position = uno.createUnoStruct('com.sun.star.table.CellAddress') + position.Sheet = 0 + position.Column = 0 + position.Row = 0 + model.NamedRanges.addNewByName(name, content, position, 0) + return None |