nss: upgrade to release 3.73
[LibreOffice.git] / scripting / examples / python / NamedRanges.py
blob0e47cb406745e4725de00c1a5a10a9f7d8f7fd82
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import traceback
11 import uno
14 def GetNamedRanges():
15 """Returns a list of the named ranges in the document.
16 """
17 try:
18 desktop = XSCRIPTCONTEXT.getDesktop()
19 model = desktop.getCurrentComponent()
20 rangeNames = model.NamedRanges.ElementNames
21 result = []
22 for i in rangeNames:
23 range = model.NamedRanges.getByName(i).Content
24 result.append((i, range))
25 return result
26 except Exception as e:
27 print("Caught Exception: " + str(e))
28 tb = e.__traceback__
29 traceback.print_tb(tb)
30 return None
33 def DefineNamedRange(sheet, x0, y0, width, height, name):
34 """Defines a new (or replaces an existing) named range on a sheet,
35 using zero-based absolute coordinates
36 """
37 desktop = XSCRIPTCONTEXT.getDesktop()
38 model = desktop.getCurrentComponent()
39 # FIXME: Is there some Python-callable API to turn a row and column into an A1 string?
40 # This obviously works only for the first 26 columns.
41 abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
42 content = "$" + sheet + "." + "$" + \
43 abc[x0: x0+1] + "$" + str(y0+1) + ":" + "$" + abc[x0+width -
44 1: x0+width] + "$" + str(y0+height)
45 position = uno.createUnoStruct('com.sun.star.table.CellAddress')
46 position.Sheet = 0
47 position.Column = 0
48 position.Row = 0
49 model.NamedRanges.addNewByName(name, content, position, 0)
50 return None
53 def DeleteNamedRange(name):
54 try:
55 desktop = XSCRIPTCONTEXT.getDesktop()
56 model = desktop.getCurrentComponent()
57 model.NamedRanges.removeByName(name)
58 except Exception as e:
59 print("Caught Exception: " + str(e))
60 tb = e.__traceback__
61 traceback.print_tb(tb)
62 return None
64 # vim: set shiftwidth=4 softtabstop=4 expandtab: