Editing a Textfield with Python-Macros in LibreOffice Calc

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
many
Posts: 3
Joined: Thu Jan 17, 2019 1:16 pm

Editing a Textfield with Python-Macros in LibreOffice Calc

Post by many »

Hey there.
I wrote a script in python, wich is difficult to handle in a python-shell. As I found out that it's possible to imply Py-Macros in *.ods I thought it might be helpful to have some sort of GUI for this script.

Especially for Debugging, i need some sort of "print"-output. I don't want all that info in one single Cell so i thought it would be easy to write into a textfield. Somehow I'm not able to write a code for this. I simply don't know anything about the structures which I need to handle. But the problem should be solved easily.

The textfield already exists. How am I able to access this object?
LibreOffice 5.3.7.2 (x64) on Windows 10
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Editing a Textfield with Python-Macros in LibreOffice Ca

Post by Lupp »

I don't know the term 'textfield' in a sense applicable to what you describe. As far as I know in Calc there only is a TextPortionType TextField which is used for URL containd in the text content of a cell (Type = 2).

If you want to add your output to the string (may be empty) already contained in a TextBox inserted as a shape, you need to find the textbox as an element of the respective sheet's DrawPage, and to use its .Text property. If formatting isn't a topic, you can concatenate with the .String property. Otherwise you need a TextCursor and have to use the insertTextContent method of the .Text.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Editing a Textfield with Python-Macros in LibreOffice Ca

Post by Villeroy »

There are database fields, text boxes on database forms which may be linked to a spreadsheet cell. There are text boxes from the drawing toolbar, text boxes on a dialog. I have no idea what a text field in a spreadsheet is, may be a cell annotation?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
many
Posts: 3
Joined: Thu Jan 17, 2019 1:16 pm

Re: Editing a Textfield with Python-Macros in LibreOffice Ca

Post by many »

Hey there. I guess the problem is: I don't really know what a "textfield" is either. Unfortunately my OO is installed in german so i have to check everything...

asfaik I'm using a dialogbox / form. I thought it would be accessible similar to cell-objects.

Sorry. I'm really completely new to OpenOffice as was only using a Python-IDE for everything.
LibreOffice 5.3.7.2 (x64) on Windows 10
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Editing a Textfield with Python-Macros in LibreOffice Ca

Post by Lupp »

In Calc a TextField actually is what I already described.
If you want to output a couple of text pieces without using cells for the purpose (Why?) you need to use shapes. These are (as I already told) managed as elements of the DrawPage every sheet has associated. If you already inserted a TextBox (A drawing object!) or a rectangle (e.g.) you can access the respective .Text property and process the .String. (I am not completely sure if every shape type has the .Text.String property).
If there is more than one shape in the sheet, you need to find the correct one first.
Using Basic as a kind of reference language for accessing the AOO (or LibO) API, I supply the following code:

Code: Select all

Function getOutShape(Optional pZ As Long, Optional pLabel As String)
If IsMissing(pZ) Then
  sheetIndex = 0
Else
  sheetIndex = pZ - 1
End If 
If IsMissing(pLabel) Then
  theLabel = "MyOutput"
Else
  theLabel = pLabel
End If 
theDoc = ThisComponent
theSheet = theDoc.Sheets(sheetIndex)
theShapes = theSheet.DrawPage
For k = 0 To theShapes.Count - 1
  outShape = theShapes(k)
  If Instr(outShape.Text.String, theLabel)=1 Then Exit For
Next k
outShape.Text.String = outShape.Text.String & Chr(10) & "New output starting here:" & Chr(10)
getOutShape = outShape
End Function
If you expereince an error due to shape types not having the accessed property, you need to exclude these types inside the For loop.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
many
Posts: 3
Joined: Thu Jan 17, 2019 1:16 pm

Re: Editing a Textfield with Python-Macros in LibreOffice Ca

Post by many »

THANKS. But you're completely right, why not using a Cell for this kind of output if I don't know that much about the used structures.
LibreOffice 5.3.7.2 (x64) on Windows 10
Post Reply