最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hide - Problems with Python macro embedded in worksheet - Stack Overflow

programmeradmin2浏览0评论

I have been trying to run a Python macro embedded in the worksheet. that hides / unhides a pushbutton based on a cell value. I am not a code person, so please forgive any errors I make here.

I got the code snippet from this thread: The macro is run by attaching the the sheet events....Content changed. I modified it as such (PushButton1 is the name from the properties panel):

1   def button_state_on_cell_Value(event):
2       “””
3       “””
4       if event.AbsoluteName.endswith('$O$1'):
5           sheet = event.Spreadsheet
6           # next button
7           PushButton1 = sheet.DrawPage[1].Control
8           PushButton1.EnableVisible = event.Value==3.0 

I found how to embed the macro with the spreadsheet from this thread: How to embed python scripts into LibreOffice Calc No modifications. This code is attached to a pushbutton .. Execute action event.

12  def test(args=None):
13      raise Exception("Hello from Python!")

When I tested the First macro, this error message appeared:

com.sun.star.uno.RuntimeException: Error during invoking function button_state_on_cell_Value in module vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py (<class 'AttributeError'>: Control File "C:\Program Files\LibreOffice\program\pythonscript.py", line 913, in invoke ret = self.func( *args ) File "vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py", line 7, in button_state_on_cell_Value)

When I tested the second macro, this error message appeared:

com.sun.star.uno.RuntimeException: Error during invoking function test in module vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py (<class 'Exception'>: Hello from Python! File "C:\Program Files\LibreOffice\program\pythonscript.py", line 913, in invoke ret = self.func( *args ) File "vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py", line 13, in test )

Anyone have an idea on how to correct these errors. If there is a way to hide and unhide the pushbutton with LO Basic, that would be great!

Thank you very much in advance!

Not sure I want to try to change anything else myself. Don't want to break thingss.

I have been trying to run a Python macro embedded in the worksheet. that hides / unhides a pushbutton based on a cell value. I am not a code person, so please forgive any errors I make here.

I got the code snippet from this thread: https://ask.libreoffice.org/t/macro-to-hide-show-and-enable-disable-a-push-button-based-on-a-cell-value/69794 The macro is run by attaching the the sheet events....Content changed. I modified it as such (PushButton1 is the name from the properties panel):

1   def button_state_on_cell_Value(event):
2       “””
3       “””
4       if event.AbsoluteName.endswith('$O$1'):
5           sheet = event.Spreadsheet
6           # next button
7           PushButton1 = sheet.DrawPage[1].Control
8           PushButton1.EnableVisible = event.Value==3.0 

I found how to embed the macro with the spreadsheet from this thread: How to embed python scripts into LibreOffice Calc No modifications. This code is attached to a pushbutton .. Execute action event.

12  def test(args=None):
13      raise Exception("Hello from Python!")

When I tested the First macro, this error message appeared:

com.sun.star.uno.RuntimeException: Error during invoking function button_state_on_cell_Value in module vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py (<class 'AttributeError'>: Control File "C:\Program Files\LibreOffice\program\pythonscript.py", line 913, in invoke ret = self.func( *args ) File "vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py", line 7, in button_state_on_cell_Value)

When I tested the second macro, this error message appeared:

com.sun.star.uno.RuntimeException: Error during invoking function test in module vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py (<class 'Exception'>: Hello from Python! File "C:\Program Files\LibreOffice\program\pythonscript.py", line 913, in invoke ret = self.func( *args ) File "vnd.sun.star.tdoc:/54/Scripts/python/PythonMacros.py", line 13, in test )

Anyone have an idea on how to correct these errors. If there is a way to hide and unhide the pushbutton with LO Basic, that would be great!

Thank you very much in advance!

Not sure I want to try to change anything else myself. Don't want to break thingss.

Share Improve this question asked Jan 19 at 23:34 Don Don 112 bronze badges 1
  • Welcome to SO! You mentioned that you want the first macro to hide/show a button based on the value of a cell. You never said what you'd like the second macro to do, though. Could you clarify? – Brendan Mitchell Commented Jan 20 at 22:32
Add a comment  | 

1 Answer 1

Reset to default 0

Macro 1: The only problem with your first macro is the index [1] in PushButton1 = sheet.DrawPage[1].Control. Python lists are zero-indexed, meaning if you want the first item in the list, you use [0] (and [1] for the second, and so on). I'm guessing your sheet only has one button, so when you try to do things with the second button, you get an error.

Macro 2: There are no problems here; the code does exactly what it's supposed to do. The line raise Exception("Hello from Python!") literally means "Generate an error and fail", which is exactly what it does.

I'm guessing you aren't very familiar with Python :) I recommend a finding a beginner course if you're interested in doing much more with it. You can easily learn enough for writing basic scripts in an hour. There are hundreds of courses on YouTube that will get you started quite nicely; I recommend Programming with Mosh.

发布评论

评论列表(0)

  1. 暂无评论