在Word.run流之外使用段落 [英] Using Paragraph outside the Word.run flow

查看:88
本文介绍了在Word.run流之外使用段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图首先找到一个段落,然后在用户单击按钮时,对该段落进行一些操作.

I am trying to first find a paragraph, and later on when a user clicks a button, do some manipulation on the paragraph.

我尝试使用似乎是权威书籍的示例:迈克尔·兹拉特科夫斯基(Michael Zlatkovsky)的"Building Office Addins".

I tried using a sample from what seems to be the defenitive book: "Building Office Addins" by Michael Zlatkovsky.

var global_paragraph = undefined;
async function analyzeDocument() {
Word.run(async function(context) {
    const paragraphs = context.document.body.paragraphs;
    context.load(paragraphs, 'text');
    return context.sync().then(() => {
        for (let i = 0; i < paragraphs.items.length; i++) {
        if (/*some condition that works only once*/) {
          global_paragraph = paragraphs.items[i];
          global_paragraph.track();
        }
    };});
}).catch(handleError);
};
async function handleButtonClick() {
    OfficeExtension.config.extendedErrorLogging = true;
    Word.run(global_paragraph, async function(context) {
        global_paragraph.load("text");
        return context.sync().then(() => {
            /* do something */
        });
    }).catch(handleError);
};

这导致了常规异常.

{"code":"GeneralException","message":"GeneralException","errorLocation":"Document._GetObjectByReferenceId","statement":"var v=context.root._getObjectByReferenceId(\"p!00000DB2\");","surroundingStatements":["// >>>>>","var v=context.root._getObjectByReferenceId(\"p!00000DB2\");","// <<<<<","v.load([\"text\"]);"],"fullStatements":["var v=context.root._getObjectByReferenceId(\"p!00000DB2\");","v.load([\"text\"]);"]}

推荐答案

我能够在脚本实验室中重现您的问题.

I am able to reproduce your issue in Script Lab.

我认为问题不在于您的代码,而在于Word API.好消息是,有一个简单的解决方法,尽管我鼓励您在 https上提交错误.仍然要://://github.com/officedev/office-js/issues ,以确保产品团队可以对此进行调查.

I believe the issue isn't with your code, but rather with the Word APIs. The good news is that there is a simple workaround, though I would encourage you to file a bug on https://github.com/officedev/office-js/issues anyway, to make sure that the product team can look into it.

解决方法代替

global_paragraph = paragraphs.items[i];

改为:

global_paragraph = paragraphs.items[i].getRange();

通过调用getRange(),它将创建一个具有正确标识的新对象,从而可以在以后进行跟踪.

By calling getRange(), it creates a new object with a a proper identity, and thus is able to track it later.

我在Script Lab中使用的代码段(实际上与您使用的代码段相同)如下:

The snippet that I used within Script Lab (effectively the same as what you had) is as follows:

$("#button1").click(() => tryCatch(button1));
$("#button2").click(() => tryCatch(button2));

var global_paragraph: Word.Range;

async function button1() {
  await Word.run(async function(context) {
    const paragraphs = context.document.body.paragraphs;
    context.load(paragraphs, "text");
    return context.sync().then(() => {
      for (let i = 0; i < paragraphs.items.length; i++) {
        if (paragraphs.items[i].text.startsWith("Dear")) {
          global_paragraph = paragraphs.items[i].getRange();
          global_paragraph.track();
        }
      }
    });
  });
}

async function button2() {
  OfficeExtension.config.extendedErrorLogging = true;
  Word.run(global_paragraph, async function(context) {
    global_paragraph.load("text");
    return context.sync().then(() => {
      console.log(global_paragraph.text);
    });
  });
}

/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
  try {
    await callback();
  } catch (error) {
    // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
    console.error(error);
  }
}

(当然,要对HTML进行相应的更改以添加两个按钮):

(And, of course, a corresponding change to the HTML to add the two buttons):

<button id="button1" class="ms-Button">
    <span class="ms-Button-label">Button1</span>
</button>

<button id="button2" class="ms-Button">
    <span class="ms-Button-label">Button2</span>
</button>

希望这会有所帮助,

〜迈克尔

这篇关于在Word.run流之外使用段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆