【问题标题】:Office Add-in hangs on context.sync() when using wildcards in search在搜索中使用通配符时,Office 加载项在 context.sync() 上挂起
【发布时间】:2022-06-24 11:25:20
【问题描述】:

我正在尝试编写一个办公插件来查找所有电话号码并突出显示它们。我使用来自the official documentation 的代码模板编写了以下带有通配符的搜索:

async function checkForText() {
        // Run a batch operation against the Word object model.
        await Word.run(async (context) => {

            // Queue a command to search the document with a wildcard
            // for any string of characters that starts with 'to' and ends with 'n'.
            const searchResults = context.document.body.search('[0-9]@-', { matchWildcards: true });

            // Queue a command to load the font property values.
            searchResults.load('font');

            // Synchronize the document state.
            await context.sync();
            console.log('Found count: ' + searchResults.items.length);

            // Queue a set of commands to change the font for each found item.
            for (let i = 0; i < searchResults.items.length; i++) {
                searchResults.items[i].font.color = 'purple';
                searchResults.items[i].font.highlightColor = 'pink';
                searchResults.items[i].font.bold = true;
            }

            // Synchronize the document state.
            await context.sync();
        })
            .catch(function (error) {
                console.log('Error: ' + JSON.stringify(error));
                if (error instanceof OfficeExtension.Error) {
                    console.log('Debug info: ' + JSON.stringify(error.debugInfo));
                }
            });
    }

我唯一修改的行是搜索表达式。

如果我在一行文本上运行此代码,它似乎可以正常工作,但如果我再运行它,插件会(永远)挂在await context.sync(); 行。奇怪的是,仅使用通配符表达式 '[0-9]@' 可以毫无问题地找到大型文档中的所有数字序列。

【问题讨论】:

  • 这个问题是在word online 还是word desktop 上出现的?你能帮忙确认一下吗?
  • @MSFT-Jipyua 它发生在 Word 桌面、Word 2021、Visual Studio 2022 64 位 17.1.1 上。

标签: ms-word office-js wildcard office-addins word-addins


【解决方案1】:

请改用通配符表达式“[0-9]@\-”。 '-' 在通配符搜索中使用时是一个特殊字符,因此我们需要在它前面加上 '\' 以将其用作文字字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2019-03-12
    相关资源
    最近更新 更多