Editing a Textfield with Python-Macros in LibreOffice Calc

Keyboard macros or custom scripts

Editing a Textfield with Python-Macros in LibreOffice Calc

Postby many » Thu Jan 17, 2019 1:21 pm

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
many
 
Posts: 3
Joined: Thu Jan 17, 2019 1:16 pm

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

Postby Lupp » Thu Jan 17, 2019 1:59 pm

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 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2523
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

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

Postby Villeroy » Thu Jan 17, 2019 2:12 pm

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, no OpenOffice, LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26975
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Postby many » Thu Jan 17, 2019 2:16 pm

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
many
 
Posts: 3
Joined: Thu Jan 17, 2019 1:16 pm

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

Postby Lupp » Thu Jan 17, 2019 2:44 pm

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   Expand viewCollapse view
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 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2523
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

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

Postby many » Thu Jan 17, 2019 3:28 pm

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
many
 
Posts: 3
Joined: Thu Jan 17, 2019 1:16 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 0 guests