Add Only Book Mark Without Text and Witout ContentControls

Hi @Constantine I want to insert a bookmark in the current position of the mouse, and then set the inserted bookmark as a bookmark,Here’s how I did it:

function Referenceclick(ctrl) {
clickCounter++;

var LocationID = document.getElementById("ddlContentType").options[document.getElementById("ddlContentType").selectedIndex].text;

var bookmarkName = LocationID + "_Enforce" + clickCounter;

GetLocationPoints(window.parent.parent.parent.document.getElementById('hdnDevid').value);

var arrDocuments = [{
    "Props": {
        "Id": 7,
        "Lock": 0
    },
    "Script": `var oParagraph = Api.CreateParagraph();
               oParagraph.AddText(' ');
               Api.GetDocument().InsertContent([oParagraph]);
               var oRange = oParagraph.GetRange(0, 1);
               oRange.AddBookmark('${bookmarkName}');`
}];

window.Asc.plugin.executeMethod("InsertAndReplaceContentControls", [arrDocuments]);

window.Asc.plugin.executeMethod("RemoveContentControl", [7]); 

}

but i want Without Text and Without ContentControls.I want Only Bookmark When I Click ( current position of the mouse)

Hello @Ram21

I don’t quite understand this. How is that? Do you want to insert some text as an anchor for a bookmark? Can you share an example how this can be done manually via editor interface? I’m not talking about macros or plugin, I’d like to understand scenario how it is.

Hi @Constantine
For Manually Click Reference Tab → BookMark → Add Bookmark

My Goal is Insert Bookmark without Text or ContentControl Like Manually

var bookmarkName = bookmarkName.toString().trim();

Asc.scope.bookmarkName = bookmarkName;
window.Asc.plugin.callCommand(function (bookmarkName) {
    var oParagraph = Api.CreateParagraph();
    oParagraph.AddText(' ');
    Api.GetDocument().InsertContent([oParagraph]);
    var oRange = oParagraph.GetRange(0, 1);
    oRange.AddBookmark(Asc.scope.bookmarkName);
}, false, bookmarkName);

undefined value kindly correct

This is an incorrect example by its concept. You are building a paragraph from scratch, which means that it is not in the document yet, and after that trying to get a range from undefined paragraph. Also, there is no text string that would define bookmark name.

In general, this approach with insertion of new paragraph won’t work.

I came up with following approach:

    {
      var oName = "Bookmark";
      var bookmarkName = oName.toString().trim();

      Asc.scope.bookmarkName = bookmarkName;
      window.Asc.plugin.callCommand(function (bookmarkName) {
        var oDocument = Api.GetDocument();
        var oSent = oDocument.GetCurrentSentence("before");
        console.log(oSent);
        var aSearch = oDocument.Search(oSent);
        var p = aSearch[0].GetEndPos();
        console.log(p);
        var newRage = aSearch[0].GetRange(p - 1, p);
        newRage.AddBookmark(Asc.scope.bookmarkName);
      }, false, bookmarkName);
    }

Please note that you need to somehow pass a sting for the bookmark name. In this example I’ve added a variable for it for the reference.

This simply gets the range with Search that looks up for the result of GetCurrentSentence with before parameter, parses returned range with GetEndPost method to get ending index of the range and start new range for the insertion of a bookmark. I’ve calculated minus one character for new range with p - 1, because range for bookmark insertion cannot be zero characters length.

I hope it helps.

1 Like

window.Asc.plugin.callCommand(filling,true );

function filling() {
function generate() {

    let key = '';
    const data = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
        'abcdefghijklmnopqrstuvwxyz0123456789';
    for (let i = 1; i <= 12; i++) {
        let index = Math.floor(Math.random()
            * data.length + 1);
        key += data.charAt(index);
    }
    return 'ROW_ENFORCE_' + key;
}
const id = generate();
var oDocument = Api.GetDocument();
var oParagraph = Api.CreateParagraph();
oParagraph.AddText(id);
oDocument.InsertContent([oParagraph]);
var startPosition = oParagraph.GetText().indexOf(id);

if(startPosition !== -1) {
    var endPosition = startPosition + id.length;

    var oRange = oParagraph.GetRange(startPosition, endPosition);
}
oRange.AddBookmark(oRange.Text);

oRange = oDocument.GetBookmarkRange(oRange.Text);
oDocument.SearchAndReplace({ "searchString": oRange.Text, "replaceString": '' });

};

This one Working Fine as Expected .Unique Bookmark name Added.But not displayed in Document(Replace with Empty string)
Thanks for Support.

1 Like