首页 > 解决方案 > 将 Office.onReady() 函数添加到 javascript

问题描述

我是 Javascript 新手,正在尝试让 Outlook Web 插件Office.onReady()正确使用该功能。

https://docs.microsoft.com/en-us/office/dev/add-ins/develop/initialize-add-in

我尝试使用

Office.onReady()
    .then

使我的函数与 office js API 一起工作,但随后我收到一个错误,即找不到我的函数。

代码:

$("#brief-summary").click(briefsummary);
$("#email-setup").click(emailsetup);

Office.onReady()
    .then(function briefsummary() {
  var msgFrom = Office.context.mailbox.item.from;
  var msgfirstname = String(msgFrom.displayName).split(" ");
  Office.context.mailbox.item.displayReplyAllFormAsync(
    "Hello " +
      msgfirstname[0] +
      ", <br> \
  <br> Here is a brief summary on everything worked on: \
    <br> \
    <br>Thank you for your time, \
      <br>"
  );
});

结果:

Uncaught ReferenceError: briefsummary is not defined

所以我很确定我做错了。我还尝试Office.onReady();在脚本开头使用。它抑制了Uncaught Error: Office.js has not fully loaded. Your app must call "Office.onReady()"错误,但 office javascript 没有做任何事情。(假设会弹出一个新的回复窗口,里面有文字)

链接到托管应用程序的位置:https : //alloyautomateaddinbeta3.azurewebsites.net/index.html(虽然 office.js 仅在您在 Outlook 应用程序中时加载。但您可以sources从 chrome 中的检查器转到并查看js 脚本)任何想法都会很棒。

标签: javascriptoffice-js

解决方案


我似乎知道该怎么做,但我很笨拙。如果有人知道更好的清理方法,请告诉我。

我基本上是我的代码在这之间并且它起作用了:

Office.onReady((info) => {
  if (info.host === Office.HostType.Outlook) {

#Insert code here

}
});

这很笨拙,因为它给了我一个警告说Function variables should not be placed in blocks. Use a function expression or move the statement to the top of the outer function

我将定期处理代码,因此我会随着时间的推移将我的答案更新为更好的答案,除非其他人也有答案。

谢谢


推荐阅读