Page 1 of 1

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

Posted: Mon Aug 03, 2020 6:19 pm
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!!!

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

Posted: Mon Aug 03, 2020 6:31 pm
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, ) )

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

Posted: Mon Aug 03, 2020 7:00 pm
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.

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

Posted: Mon Aug 03, 2020 7:37 pm
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.

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

Posted: Mon Aug 03, 2020 9:04 pm
by StillLearning23#1
Thank you.