[Solved] Python: Accessing "Linked Cell" property of a form

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
StillLearning23#1
Posts: 3
Joined: Mon Aug 03, 2020 5:21 pm

[Solved] Python: Accessing "Linked Cell" property of a form

Post by StillLearning23#1 »

Python: Accessing "Linked Cell" property of a form control

Hello. I am trying to write a macro that will change the linked cell of a checkbox in Calc. This will solve the issue I'm having where I duplicate a sheet and the checkboxes continue to link to the original sheet.

This has been accomplished successfully in StarBasic as documented in this thread:

viewtopic.php?f=20&t=84268#p392162


I have successfully recreated most of this in python, however I can not write the property back to the control. Here is the relevant python code, with the error being genereated as a result of oCVB.initialize( oNamedValue ) :

Code: Select all


oLinkedCell = uno.createUnoStruct("com.sun.star.table.CellAddress")
oLinkedCell.Sheet = 2
oLinkedCell.Column = oBoundCell.Column
oLinkedCell.Row = oBoundCell.Row


oNamedValue = uno.createUnoStruct("com.sun.star.beans.NamedValue")
oNamedValue.Name  = "BoundCell"
oNamedValue.Value = oLinkedCell
oCVB = model.createInstance("com.sun.star.table.CellValueBinding")
oCVB.initialize( oNamedValue )
The error:

Code: Select all

>>> runpy.run_module(mod_name="hello_calc")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/runpy.py", line 210, in run_module
    return _run_code(code, {}, init_globals, run_name, mod_spec)
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/john/Documents/Computer/LibreCalcProgramming/hello_calc.py", line 61, in <module>
    oCVB.initialize( oNamedValue )
hello_calc.CannotConvertException: conversion not possible!
The basic equivalent of that line of code is found here (from the BASIC solution linked above):

Code: Select all

oLinkedCell.Sheet = i-9
oLinkedCell.Column = 14
oLinkedCell.Row = j
oNamedValue.Name = "BoundCell"
oNamedValue.Value = oLinkedCell
vCVB = oDoc.createInstance("com.sun.star.table.CellValueBinding")
vCVB.Initialize(Array(oNamedValue))

I noticed the use of "Array" in the BASIC call to Initialize and wondered if there was some sort of python equivalent to a C++ type cast that I'm supposed to include?

The "CellValueBinding Service Reference" states:

Code: Select all

The arguments passed to the com::sun::star::lang::XInitialization::initialize() method must be instances of com::sun::star::beans::NamedValue... 
Looking at my code, it seems this condition has been met? I don't know how to fix this error and despite searching, can find no example to help me.

I'm new to UNO, the LibreOffice / OOo API and Python, but also very close to achieving what I want. Any help will be greatly appreciated!!!
Last edited by robleyd on Tue Aug 04, 2020 1:56 am, edited 4 times in total.
Reason: Add green tick
LibreOffice 6.4.5.1 on Debian Bullseye
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Python: Accessing "Linked Cell" property of a form contr

Post by FJCC »

I haven't used Python in a long time, so this suggestion is likely off target. I would try passing oNamedValue as a tuple.

Code: Select all

oCVB.initialize( (oNamedValue, ) )
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
StillLearning23#1
Posts: 3
Joined: Mon Aug 03, 2020 5:21 pm

Re: Python: Accessing "Linked Cell" property of a form contr

Post by StillLearning23#1 »

Your "off target" reply hit the bullseye!!!! Everything works now. I can't thank you enough. Where can I learn about that syntax? The argument with a comma. I had seen this in the hours and hours of searching but didn't understand that it was a sort of type cast. Anyway, thank you again, I REALLY appreciate your answer. Have a good day.
LibreOffice 6.4.5.1 on Debian Bullseye
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved] Python: Accessing "Linked Cell" property of a f

Post by FJCC »

The "stray" comma in (oNamedValue, ) is a Python convention to distinguish a single entity in parentheses from a tuple with only one element. So, (x) is just the same as x but (x, ) is a tuple with x as its only element.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
StillLearning23#1
Posts: 3
Joined: Mon Aug 03, 2020 5:21 pm

Re: [Solved] Python: Accessing "Linked Cell" property of a f

Post by StillLearning23#1 »

Thank you.
LibreOffice 6.4.5.1 on Debian Bullseye
Post Reply