检查 Office.js 是否在 Office 客户端之外加载

问题描述 投票:0回答:3

如果我们在 Office 客户端之外加载引用

office.js
的网页,我们将收到警告:
Office.js is loaded outside of Office client

此信息很有用。

有谁知道是否有 API 可以检查我的代码中的内容?

编辑1:

我解释一下我的情况以及为什么我问这个问题。我正在使用 angularjs 制作一个应用程序,它可以作为网页加载到浏览器中,也可以作为加载项加载到 Office 中。我意识到我们不应该同时执行

<body ng-app="myApp">
angular.bootstrap(document, ['myApp'])
,否则控制器将执行两次。所以我决定不写
<body ng-app="myApp">
并在两种情况下(即网页和加载项)始终使用
angular.bootstrap

所以对于网页,我可以这样写:

$(document).ready(function () {
    angular.bootstrap(document, ['myApp'])  
})

app = angular.module('myApp', ['ui.router', 'ui.bootstrap'])
...

所以对于网页来说,我需要在

angular.bootstrap
里面写
Office.initialize
,并与add-in的情况共享其他代码:

Office.initialize = function (reason) {
    $(document).ready(function () {
        angular.bootstrap(document, ['myApp'])
    });
}

app = angular.module('myApp', ['ui.router', 'ui.bootstrap'])
// share the same code

但是,如果我将这两种情况写在一起,如下所示,它适用于网页,而我给出错误:ng:btstrpd 应用程序已使用此 Element 进行加载

$(document).ready(function () {
    angular.bootstrap(document, ['myApp'])
    console.log("bootstrapped outside Office.initialize")   
})

Office.initialize = function (reason) {
    $(document).ready(function () {
        angular.bootstrap(document, ['myApp'])
        console.log("bootstrapped inside Office.initialize")
    })
}

app = angular.module('myApp', ['ui.router', 'ui.bootstrap']).

如果我设置了一个标志,控制台将显示

bootstrapped outside Office.initialize
,然后显示
isBootstrapped
,然后运行代码将显示
Office.context
Office.context.document
未定义

var isBootstrapped = false;

$(document).ready(function () {
    angular.bootstrap(document, ['myApp'])
    isBootstrapped = true
    console.log("bootstrapped outside Office.initialize")
})

Office.initialize = function (reason) {
    $(document).ready(function () {
        if (isBootstrapped) console.log("isBootstrapped")
        else {
            angular.bootstrap(document, ['myApp'])
            console.log("bootstrapped inside Office.initialize")
        }
    })
}

app = angular.module('myApp', ['ui.router', 'ui.bootstrap'])

所以我真的需要一种有效的方法来检查 Office.js 是否在 Office 客户端之外加载(即,它是网页还是加载项),以决定应该执行哪一部分

angular.bootstrap

ms-office office-js
3个回答
6
投票

目前还没有这样的 API,尽管我们内部讨论过有一个

Office.ready()
(在精神上与
$(document).ready(...)
类似),只要 Office.js 完成初始化(无论是在加载项还是在不是)。

欢迎您在https://github.com/OfficeDev/office-js上提出建议,并在此发布链接。我对 API 的想法是它会接受一个回调(就像

$(document).ready(...)
一样,它会在准备好时触发,并且也可以以 Promise 形式提供(所以你可以这样做
await Office.ready()
)。你认为这适用于你的场景?

FWIW:作为解决方法,对于 Script Lab,我们将 Promise 包装在 Office.initialized 周围(并确保在应用程序加载的早期执行此操作,否则如果晚得多它就不会触发),等待为此,如果我们在前 3 秒内没有收到任何信息,我们会显示一组按钮,让用户帮助我们消除歧义。请参阅 https://script-lab.azureedge.net/ 了解其外观示例。并不完美,但对于我们的场景来说还不错。不过,我确实鼓励您在 office-js 存储库上提交建议错误,并添加您的具体场景来支持它。


2
投票

一种方法是使用 https://github.com/OfficeDev/office-js-helpers

OfficeHelpers.Utilities.host
OfficeHelpers.Utilities.platform
都提供有用的信息。


0
投票

我是这样做的:

里面

commands.js
我有以下内容:

Office.onReady(async function (info) {
  await Office_Loaded_Check(info)
});

我加载了一个单独的文件

globals.js
,其中包含以下内容:

function getGlobal() {
  return typeof self !== "undefined"
    ? self
    : typeof window !== "undefined"
    ? window
    : typeof global !== "undefined"
    ? global
    : undefined;
}

var g = getGlobal();

//Office Loaded
async function Office_Loaded_Check(info) {
  if (info["host"] == null) {
    //All the below allow code to run without Error when using Edge to debug outside Office
    console.log('Setting Fake Office/Excel Objs Vars for Running Outside Office')

    //Office
    Office.addin = {};
    Office.addin.setStartupBehavior = function () {
      return true;
    };
    Office.addin.showAsTaskpane = function () {
      return true;
    };

    Office.StartupBehavior = {};
    Office.StartupBehavior.none = null;

    //Excel
    var Excel = {};
    Excel.run = async function (Excel_Run) {
      var context = {};
      context.sync = function () {
        return true;
      };
      Excel_Run(context);
      return true;
    };

    //Excel Obj for No Error in Edge/NodeJS Mode
    g.Excel = Excel;

 }
}

奖励,我有时也会在 Node 中运行我的代码服务器端,我使用这个:

注意:对于这一点,我确实在代码中使用了布尔值,例如 If NodeJS then --

const xmlserializer = require('xmlserializer'); //XMLSerializer only available client side, want code to be usable both client and server side, so use xmlserializer 

var Is_NodeJS_Bool = false;
g.Is_NodeJS_Bool = Is_NodeJS_Bool;
if (typeof window === "undefined") {
  Is_NodeJS_Bool = true;
  console.log("Is_NodeJS_Bool = true");
} else {
  var module = {};
  //NodeJS Var for No Error in Edge/Office Mode
  g.module = module;
}

这样,我的代码中可以包含以下内容,而不会在 Edge 中出现错误:

//Server_Side NodeJS
module.exports = {
    ...,
};
© www.soinside.com 2019 - 2024. All rights reserved.