Basic to Python

Good Evening

from the OpenOffice forum link:

https://forum.openoffice.org/en/forum/viewtopic.php?t=104760

I got this sub to get the current region of the range in a sheet

Function getCurrentRegion(oRange)
Dim oCursor
	oCursor = oRange.getSpreadSheet.createCursorByRange(oRange)
	oCursor.collapseToCurrentRegion
	getCurrentRegion = oCursor
End Function

What is the python translation of this?

Thank you again.

def getCurrentRegion(oRange):
	oCursor = oRange.getSpreadsheet().createCursorByRange(oRange)
	oCursor.collapseToCurrentRegion()
	return oCursor

EDIT: added the missing () after collapseToCurrentRegion
Without the parentheses you get the method itself as an object instead of calling the method.
EDIT2: getSpreadSheet → getSpreadsheet()

Good Morning,

Thank you for your help. It is much appreciated. I am looking at the suggested code:

from __future__ import unicode_literals
import uno
import cx_Oracle
def checkDB():
	wb = XSCRIPTCONTEXT.getDocument()
	sht =  wb.Sheets['Sheet1']
	#connection = cx_Oracle.connect(user='????',password='????',dsn='????')
	#cursor = connection.cursor()
	oRange = wb.Sheets['sql'].getCellRangeByName('A1')
	sqls= getCurrentRegion(oRange)
	print(sqls)
	# sht.getCellRangeByName('A1:AMJ1048576').clearContents(7)
	# startCol = 0
	# startRow = 0
	# listSize=10000
	# cursor.execute(sql)
	# desc = cursor.description
	# desc=list(zip(*desc))
	# desc=desc[0]
	# lastCol =len(desc)-1
	# desc=[desc]
	# sht.getCellRangeByPosition(startCol,startRow,lastCol,startRow ).setDataArray(desc)
	# startRow=1
	# while True:
		# result= cursor.fetchmany(listSize)
		# if result:
			# lastRow=len(result)+startRow-1
			# sht.getCellRangeByPosition(startCol,startRow,lastCol,lastRow).setDataArray(result)
			# startRow=lastRow+1
			# wb.store()
		# else:
			# break
	# cursor.close()
	# connection.close()

	
def getCurrentRegion(oRange):
   oCursor = oRange.getSpreadSheet.createCursorByRange(oRange)
   oCursor.collapseToCurrentRegion()
   return oCursor

However I get the popup message:

Error during invoking function checkDB in module vnd.sun.star.tdoc:/1/Scripts/python/ModuleGetData.py (<class 'AttributeError'>: getSpreadSheet
  File "D:\Program Files\LibreOffice\App\libreoffice\program\pythonscript.py", line 915, in invoke
    ret = self.func( *args )
  File "vnd.sun.star.tdoc:/1/Scripts/python/ModuleGetData.py", line 11, in checkDB
  File "vnd.sun.star.tdoc:/1/Scripts/python/ModuleGetData.py", line 39, in getCurrentRegion
)

I modified getCurrentRegion to:

def getCurrentRegion(oRange):
   oCursor = oRange.getSpreadSheet().createCursorByRange(oRange)
   oCursor.collapseToCurrentRegion()
   return oCursor

And I still get the same error. Thank you.

Please upload the whole code and a working sample file here. We can guessing only without them.

Note that unlike Basic, Python is case sensitive. So you must pay attention to the case in API specification.

(Which is why it’s also good to keep original case in Basic, not to use fancy case change according to one’s liking… :wink:)

a range object has no attribute (property) getSpreadSheet. getSpreadSheet would be method call getSpreadSheet() with parentheses – if it would exist.
Either you write the function oRange.getSpreadsheet() as documented or you make use of the pseudo-property “Spreadsheet”
oRange.Spreadsheet
A pseudo-property is derived from a pair of get/set methods.
If some interface has a pair of methods
value = obj.getSomething()
and
obj.setSomething(value)
then you can also use the equivalent pseudo-properties
value = obj.Something
and
obj.Something = value
Sometimes there is only method getSomething without setSomething. That would be a read-only pseudo-property – for instance range.getSpreadsheet() because a range belongs to one distinct sheet.
In rare cases there is only method setSomething without getSomething. That would be a write-only pseudo-property.

oCursor = oRange.getSpreadsheet().createCursorByRange(oRange)
# equivalent pseudo-property:
oCursor = oRange.Spreadsheet.createCursorByRange(oRange)
1 Like

Hello,
I followed the advice and here is the corrected code

def getCurrentRegion(oRange):
   oCursor = oRange.Spreadsheet.createCursorByRange(oRange)
   oCursor.collapseToCurrentRegion()
   return oCursor

And it works. Thank you for all of your assistance and explanation.

Zafar