I am a JavaScript developer learning Python, and to this end I’d like to craft a budget analysis tool inside a Python macro for LibreOffice calc. In the end product, I’ll be receiving a CSV file from an online finances app, and then I’ll use my Python script to iterate over the data, pick out the pieces I want to look at, and make a fancy chart and/or graph to tell me what I need at a glance. (Amazingly, my app doesn’t seem to have that. Weird…) I’ve been doing small code challenges/tests to work on each part of my project, and I’ve currently run into what I hope is a minor roadblock.
My thinking is that I can import the CSV into a Calc spreadsheet, then iterate over the first row (so “Sheet1.A1:K1” or whatever the last cell would be), and when I find the column I want, based on text inside the current cell, then I can take that entire column and start doing my work on it. The problem that I’ve run into is that Calc ranges are apparently not iterable. For instance, I’m using the following code to test my understanding of Python iterables:
from scriptforge import CreateScriptService
svc_ui = CreateScriptService('UI')
this_window = svc_ui.ActiveWindow
cDoc = svc_ui.GetDocument(this_window)
def looptest():
testRange = cDoc.Range("$Sheet1.A1:K1")
rangeType = type(testRange)
cDoc.setValue('Sheet1.A5', str(rangeType))
cDoc.setValue('Sheet1.A7', str(logging.debug(testRange)))
for cell in testRange:
cDoc.setValue(cell, "stuff")
When I do this, I receive the following error:
com.sun.star.uno.RuntimeException: Error during invoking function secondtest in module file:///C:/Users/Arcturus/AppData/Roaming/LibreOffice/4/user/Scripts/python/Experiments/CreateAndInsertCalc.py (<class ‘TypeError’>: ‘SF_CalcReference’ object is not iterable
File “C:\Program Files\LibreOffice\program\pythonscript.py”, line 915, in invoke
ret = self.func( *args )
File “C:\Users\Arcturus\AppData\Roaming\LibreOffice\4\user\Scripts\python\Experiments\CreateAndInsertCalc.py”, line 27, in secondtest
for cell in testRange:
)
(FWIW, str(rangeType)
gives me <class 'scriptforge.SFDocuments.SF_CalcReference'>
and logging.debug(testRange)
gives me None
but that’s probably due to my lack of Python understanding rather than a bug or error.)
Seems to me the issue is that ‘SF_CalcReference’ object is not iterable. Okay, but then what? I’d assume you would have to loop over cells in a range to perform many operations or tasks with Calc. Is there a specific method to do this with ranges? I’ve looked in the ScriptForge.Calc service and the ScriptForge.Array service but haven’t seen what I’d think would do it. I’ve also looked at the LibreOffice Module Wizards page, but so far I am too dumb to get anything out of it. Am I looking in the wrong place? Do I need to be examining the other services for the right method? Is this indeed due to a fundamental misunderstanding of Python (in which case, I need to hit the books again)? Is this something where I need to use LibreOffice Basic to accomplish this task?
I am of course going to continue searching for answers, but wanted to drop this question here in case there was something I was obviously missing.
Of course, it may be that this is just a terrible idea from the get-go, and I should not even be approaching LibreOffice Python (or LibreOffice itself!) for this project. If that’s the case, any suggestions would be appreciated – just that, since I am studying Python, I would like Python based ideas.