hello, I created some functions with respective library in basic that work well, I tried to do the same in python and there is no way, I can not in any way, I created all the necessary files that I put in the folder scripts/python,
init.py
library.py
macro.py
But nothing to do, someone managed it?
Thank you for your replies
What specifically fails? What you do (like “I press this” or “I put that there”); what is your expectation; what do you see actually? What is the test document that you forgot to attach to your question (and the Python code in the text of the question), so that others could see?
Hi Mike, I asked if anyone has created custom functions in python with their own library that have worked, indipemately by the code, I want to understand how it is done.
Example, in basic I created the function:
CopyPaste(“a1:a10”, “sheet1”, “a1:a10”, “sheet2”) with the respective code placed in the library in the “standard” folder.
In macros it is enough that I write that function and performs everything correctly.
In python I tried in all ways and gave me any kind of error, especially that does not find the file library.py
At the beginning of each macro I enter:
from Library import CopyPaste and after the code. Does not recognize it.
It is always a good thing to name your versions of LibreOffice, if you use APSO etc. But as I’m traveling I can’t check. But I remember from older times apso not implementing python-path, wich would limit you to the current module.
.
There is apso with PIP available now, so there may be ablot of changes recently…
hello, I have the latest version of libreoffice and python, I have many macros in python that work well, but when I try to do a function there is no way.
Regardless of the version, has anyone ever created a custom function with library?
I have a suspicion that you keep omitting a crucial piece of information:
“I try to create a custom spreadsheet function using Python”. And all your “if anyone has created custom functions in python”, “I created the function: CopyPaste(“a1:a10”, “sheet1”, “a1:a10”, “sheet2”)”, “In macros it is enough that I write that function and performs everything correctly”, etc. imply that you put these calls in a spreadsheet cell. You know, a “function” is a usual thing in Python; unless you explicitly specify, that you are talking about some different thing, people need mind-reading abilities to understand you.
You know what: writing macros is programming. And programming requires a specific mindset. You need to learn to be as specific as the programs you create. Otherwise, your programs will be similarly imprecise, and will fail.
And no, there is no way to create “user-defined functions” (spreadsheet functions created as macros) in Python. You may create Basic functions, and call Python code from there; or you may create spreadsheet functions as add-ons written in Python.
Hi, I created this function which I put in the “standard” folder:
.
Sub CopyPaste(sourceRange As String, sourceSheet As String, destRange As String, destSheet As String)
Dim oDoc As Object
Dim oSourceSheet As Object
Dim oDestSheet As Object
Dim oSourceRange As Object
Dim oDestRange As Object
oDoc = ThisComponent
oSourceSheet = oDoc.Sheets.getByName(sourceSheet)
oDestSheet = oDoc.Sheets.getByName(destSheet)
oSourceRange = oSourceSheet.getCellRangeByName(sourceRange)
oDestRange = oDestSheet.getCellRangeByName(destRange)
oDestRange.setDataArray(oSourceRange.getDataArray())
End Sub
.
that I use in this way:
.
Sub macro
CopyPaste(“a1:a10”, “sheet1”, “a1:a10”, “sheet2”)
End Sub
.
the function is fine, it does its job.
it is possible to do the same with python?