尝试获取parentContentControl时,使用object.track()重用上下文会中断

时间:2019-06-16 10:09:16

标签: office-js

上下文:当检测到单击时,我尝试检测是否有任何段落以某种方式没有contentControl。因此,它需要重用上下文。我已经能够使上下文重用正常工作,但是对于这部分特定的代码却没有。

因此,两次运行一段代码的复制最少:

        let randomObject: Word.Document = null;
        await Word.run(async (context) => {
            randomObject = context.document;
            randomObject.track();
        })
        await Word.run(randomObject, async (context) => {
            await test(context);
        });

        await Word.run(randomObject, async (context) => {
            await test(context);
        });

这是仅检索第一段中的第一个contentControl的代码。它执行以下操作:

  • 检索正文
  • 检索段落
  • 检索第一项
  • 检索parentContentControlOrNullObject
async function test(context: Word.RequestContext) {
    try {
        const body = context.document.body;
        await context.sync();
        const paragraphs = body.paragraphs;
        paragraphs.load('items');
        context.load(paragraphs);
        await context.sync();
        console.log('1111', paragraphs.items.length);
        const para = paragraphs.items[0];
        await context.sync();
        console.log('2222');
        const cc = para.parentContentControlOrNullObject;
        await context.sync();
        console.log('3333');
        await context.sync();
        console.log('done');
    } catch (e) {
        console.log('Error: test', e);
        if (e instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(e.debugInfo));
        }
    }
}

这是输出:

enter image description here

显然,它在第一次运行中就成功了,但是在第二次运行中却没有成功。

详细错误如下:

{
   [functions]: ,
   __proto__: { },
   code: "ItemNotFound",
   errorLocation: "ParagraphCollection.getItem",
   fullStatements: [ ],
   message: "ItemNotFound",
   statement: "var paragraph=paragraphs.getItem(...) /* originally getItem(...) */;",
   surroundingStatements: [ ],
   Symbol()_7.e7nkt729xmo: undefined,
   Symbol()_k.e7nkt729xt6: undefined,
   Symbol()_m.e7nkt729xt6: undefined,
   Symbol(Symbol._hidden)_l.e7nkt729xt6: undefined,
   Symbol(Symbol.observable)_n.e7nkt729xt6: undefined,
   Symbol(util.promisify.custom)_j.e7nkt729xt6: undefined
}

@ michael-zlatkovsky似乎与Using Paragraph outside the Word.run flow无关

            const body = context.document.body;
            await context.sync();
            const paragraphs = body.paragraphs;
            paragraphs.load('items');
            context.load(paragraphs);
            await context.sync();
            console.log('xxxxxxxxxxx');
            const t = paragraphs.items[0].getRange();
            await context.sync();
            console.log('yyyyyyyyyyy');

从不进入yyyyyyyyyyy

0 个答案:

没有答案