Meet ONLYOFFICE Docs Cloud, complete office software as a service
Meet ONLYOFFICE Docs Cloud

Insert Paragraph in a desired space of opened document

DocumentBuilder version: 7.1.1
Installation method: docbuilder.exe
OS: Windows 7 x64
Hello. Please, suggest me a method how can I insert a paragraph in a desired space of a document, e.g. “between paragraph 1 and paragraph 2”? Para 1 and Para 2 already exists in an opened docx-document. Can’t find this method, CreateParagraph() doesn’t contain any parameters. Thanks.

Hello @poddex
I think you can achieve desired scenario with a macro.
Example:

(function()
{
    
    let oDocument = Api.GetDocument();
    let oParF = oDocument.GetElement(0);
    oParF.AddText('Par 1');
    let oParS = Api.CreateParagraph();
    oParS.AddText('Par 2');
    oDocument.Push(oParS);
    
    let oParT = Api.CreateParagraph();
    oParT.AddText('Par 3');
    let nElementsCount = oDocument.GetElementsCount();
    oDocument.AddElement(nElementsCount - 1, oParT);

})();
1 Like

In already existing document used as a template. Can I use GetElementCount() in already existing template? And please tell me where can I find a whole manual for onlyoffice script language? With a description of function() and other statements. Thanks.

In already existing document used as a template. Can I use GetElementCount() in already existing template?

I’m not sure that I understand it right. What do you mean by template?
Please check out this title which describes GetElementCount: ONLYOFFICE Api Documentation - GetElementsCount

And please tell me where can I find a whole manual for onlyoffice script language? With a description of function() and other statements.

We don’t have our own script language, we use javascript.
I believe these links will be useful for you, you can find examples of macros and API requests here:
https://api.onlyoffice.com/plugin/macros
https://api.onlyoffice.com/docbuilder/basic

1 Like

I attached a pic described a task. Already have a file with 2 paragraphs, need insert third paragraph between them. Can I do it via docbuilder language? I tried lines like this:

let nElementsCount = oDocument.GetElementsCount();
var oParagraph = Api.CreateParagraph();
oParagraph.AddText(nElementsCount);

By this I wanted 1) get a count of elements 2) insert element between them. In a step (1) I expected to see “2” (2 paragraphs), but there is no return by these lines.

I just have tried to run mentioned example of macro in docbuilder example and it works: Monosnap
Example:

builder.CreateFile("docx");
var oDocument = Api.GetDocument();
var oParagraph = oDocument.GetElement(0);
let oParF = oDocument.GetElement(0);
oParF.AddText('Par 1');
let oParS = Api.CreateParagraph();
oParS.AddText('Par 2');
oDocument.Push(oParS);
let oParT = Api.CreateParagraph();
oParT.AddText('Par 3');
let nElementsCount = oDocument.GetElementsCount();
oDocument.AddElement(nElementsCount - 1, oParT);
Api.Save();
builder.SaveFile("docx", "Save.docx");
builder.CloseFile();

If I misunderstood the situation, please clarify it.

Worked. Thanks.
Additional question. There is a SearchAndReplace function in api. Can I get a number of element containing some text? Useful, when I want to insert some element right after the paragraph contained some finded text.

Do you mean this method? ONLYOFFICE Api Documentation - GetElementsCount
Please describe the scenario in details.

For now there is no question on this issue. Thanks.