如何使用 Office JS 在 Word 文档正文中添加书签

How do I add a bookmark within the body of a Word doc using Office JS

Office JS在preview中提供了以下功能,但我找不到任何示例。

这是我尝试过的方法,但它似乎不起作用,不知道我在这里遗漏了什么,因为这段代码插入了文本,但没有创建书签。

Word.run(function (context)
{
    let range = context.document.getSelection();
    return context.sync().then(function ()
    {
        range.insertText(`Test Bookmark`, Word.InsertLocation.replace);

        let uniqueStr = new Date().getTime();
        let bookmarkName = `Test_BookmarkCode_${uniqueStr}`;
        range.insertBookmark(bookmarkName);
    });
});

交叉发布 here.

所以,这是工作代码。显然,当我们插入文本时,返回了一个新的范围,我们需要使用该范围来插入书签。

Word.run(function (context)
{
    let range = context.document.getSelection();
    return context.sync().then(function ()
    {
        let insertedTextRange = range.insertText(`Test Bookmark`, Word.InsertLocation.replace);

        let uniqueStr = new Date().getTime();
        let bookmarkName = `Test_BookmarkCode_${uniqueStr}`;
        insertedTextRange.insertBookmark(bookmarkName);
    });
});