OfficeApi's Paragraph throwing an exception when calling Search function

Do you want to: Report a bug
Document Server version:8.1.1.26
Additional information:

I have a sample .pptx file where I want to write a macro that’ll search a paragraph for a specific word and then highlight the hits. However, when I retrieve the paragraphs from a Slide and call the Search method, I get an exception that’s coming from sdk-all.js itself.

This seems like a bug, since it’s coming from the sdk itself. Moreover, if I create a copy by calling the Copy method on the paragraph and try to search the copy instead, I don’t get the exception. However, the copy still doesn’t return search results, even if I pass a string that’s present in said paragraph.

Steps to reproduce:

  1. Open the following file samplepptx.pptx in onlyoffice’s presentation editor. Link to file: samplepptx

  2. Go to the plugins menu, and then click on macro

  3. Open dev tools from your browser (on chrome and firefox, hit F12) and go to console

  4. In the macro window, paste the following script and click on run. You should get some exceptions as mentioned.

(function()
{
    var oPresentation = Api.GetPresentation();

    for(let i = 0; i < oPresentation.GetSlidesCount(); i++){
       let slide = oPresentation.GetSlideByIndex(i);
       let shapes = slide.GetAllShapes();
       shapes.forEach(shape =>{
            var shapeContent = shape.GetContent();
            for(let j = 0; j < shapeContent.GetElementsCount(); j++){
                var element = shapeContent.GetElement(j);
                if(element.GetClassType() === 'paragraph'){
                    var text = element.GetText();
                    console.log(text);
                    var query = "Cloud";
                    try{
                        var hits = element.Search(query);
                        console.log('hits: ')
                        console.log(hits);
                    }
                    catch(error){
                        console.log('error in this paragraph:')
                        console.log(error)
                        console.log('searching in copy instead...')
                        //Search a copy instead
                        var copy = element.Copy();
                        var hitsInCopy = copy.Search(query)
                        console.log(hitsInCopy)
                    }
                    console.log('')
                }
            }
       });
      
    } 
})();
1 Like

Hello @Tarragod

This is expected behavior that Search does not return any results, as API for Presentation editor does not have such method.

I belive that is not true. Using GetAllShapes will return an array of ApiShape elements.

Then, on line 9, I’m using the GetContent method from ApiShape, and according to official documentation, that’ll return an object of type ApiDocumentContent.

I then iterate over the elements in ApiDocumentContent, and GetElement() should return a DocumentElement (GetElement - ONLYOFFICE Api Documentation ), and that in turn can be an ApiParagraph: Simple types - ONLYOFFICE Api Documentation.

I believe line 12 proves that is a paragraph with the GetClassType call, so Search should be available.

Indeed, it can be a paragraph but the method Search() for presentation editor is not implemented yet, hence when trying to call it nothing happens. Each editor has its own set of API methods, you cannot execute method of, for instance, spreadsheet editor in presentation editor.

Suggestion to implement Search() for presentation editor was registered earlier so I’m adding your query to it. Once this method becomes available we will notify you.

1 Like

Thanks Constantine.

As a side note, would it be possible to update the documentation on ApiParagraph for Presentation API? ApiParagraph - ONLYOFFICE Api Documentation

There’re lots of methods that are available for it, but are not documented.

Using the same sample file from my original post, you’ll notice that all of these methods in this code snippet work, but none of them appear in the official docs for presentation api.

(function()
{
    var oPresentation = Api.GetPresentation();
    var oSlide = oPresentation.GetSlideByIndex(0)
    var oElement = oSlide.GetAllShapes()[1].GetContent().GetElement(0);
    
    // None of these appear on ApiParagraph's documentation for Presentation API
    oElement.SetColor(255, 111, 61);
    oElement.SetBold(true);
    oElement.SetDoubleStrikeout(true);
    oElement.SetFontFamily("Impact");
    
})();

Can you provide a link to that issue here, if it is also within the forums, so I can upvote it? I’m having this problem as well.

Mentioned methods (SetColor(), SetBold(), SetDoubleStrickeout(), SetFontFamily()) are referenced in ApiRun section, which represents a small text block.


Hello @monkeycoder

This topic is generally the issue. Once any news come up, you will be notified.
Thanks for your interest.

1 Like