[Solved] List of Modules and Macros

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

[Solved] List of Modules and Macros

Post by saleem145 »

Hello,

From python can I determine the list of basic modules that are part of the spreadsheet. Within each module can I can determine what macros are available??

Given a module and macro name (implemented in basic) can I call the macro from python.

Thanks,

Saleem
Last edited by saleem145 on Mon Aug 06, 2012 1:20 am, edited 1 time in total.
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: List of Modules and Macros

Post by saleem145 »

spreadsheet.BasicLibraries will give me the list of modules. Not sure how to get a list of macros and how to call them from python.
OpenOffice 3.4.0
Mac OS X 10.5.8
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: List of Modules and Macros

Post by hanya »

Basically both listing macros and executing one of them can be done through script provider which can be accessed on document model. But listing basic macro functions can not be done through PyUNO due to the problem on the component implementation of the node for basic method and pyuno in Python on AOO 3.4.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: List of Modules and Macros

Post by hanya »

hanya wrote:But listing basic macro functions can not be done through PyUNO due to the problem on the component implementation of the node for basic method and pyuno in Python on AOO 3.4.
Issue: https://issues.apache.org/ooo/show_bug.cgi?id=120458
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: List of Modules and Macros

Post by hanya »

We need to use the workaround due to the problem above.

Code: Select all

def _get_document_uri(ctx, doc):
    """ Get document transient URI. """
    tddc = ctx.getServiceManager().createInstanceWithContext(
    "com.sun.star.frame.TransientDocumentsDocumentContentFactory", ctx)
    try:
        content = tddc.createDocumentContent(doc)
        id = content.getIdentifier()
        return id.getContentIdentifier()
    except:
        pass


def get_document_basic_macros_provider(ctx, doc):
    uri = _get_document_uri(ctx, doc)
    return create(ctx, "com.sun.star.script.provider.ScriptProviderForBasic", (uri,))


def create(ctx, name, args=None):
    smgr = ctx.getServiceManager()
    if args is None:
        return smgr.createInstanceWithContext(name, ctx)
    else:
        return smgr.createInstanceWithArgumentsAndContext(name, args, ctx)

def get_basic_macros_provider(ctx, context):
    return create(ctx, "com.sun.star.script.provider.ScriptProviderForBasic", (context,))


def get_list_of_macros(ctx, sp):
    introspection = create(ctx, "com.sun.star.beans.Introspection")
    macros = []
    for lib_node in sp.getChildNodes():
        #print(lib_node.getName())
        for module_node in lib_node.getChildNodes():
            #print(module_node.getName())
            for method_node in module_node.getChildNodes():
                d = None
                inspected = introspection.inspect(method_node)
                idl_getName = inspected.getMethod("getName", -1)
                idl_getPropertyValue = inspected.getMethod("getPropertyValue", -1)
                name, d = idl_getName.invoke(method_node, ())
                uri, d = idl_getPropertyValue.invoke(method_node, ("URI",))
                macros.append((name, uri))
    return macros

def test(*args):
    ctx = XSCRIPTCONTEXT.getComponentContext()
    doc = XSCRIPTCONTEXT.getDocument()
    
    sp = get_document_basic_macros_provider(ctx, doc)
    #sp = get_basic_macros_provider(ctx, "user")
    macros = get_list_of_macros(ctx, sp)
    for name, uri in macros:
        print(name)
        print(uri)
Once you have URI to a macro, you can call it like described in http://user.services.openoffice.org/en/ ... pt#p242949
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: List of Modules and Macros

Post by saleem145 »

Hello,

I have been able to obtain the list of macros. But am having difficulty invoking them from Python...

Code: Select all

        sp = ctx.ServiceManager.createInstanceWithArgumentsAndContext("com.sun.star.script.provider.ScriptProviderForBasic", (spreadsheet,), self.ctx)

        try:
            macro_exists = True
            script = sp.getScript("vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=document")
        except:
            macro_exists = False

        if macro_exists:
            script.Invoke([],[],[])
It finds the macro but fails on the call to script.Invoke. Any help is appreciated as always. Thanks,

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: List of Modules and Macros

Post by hanya »

Code: Select all

script.invoke((), (), ())
See: http://www.openoffice.org/api/docs/comm ... cript.html
And PyUNO bridge does not allow to take instance of list as method argument, use tuple.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Post Reply