如何获取Outlook插件运行平台(Windows/Mac/OWA)?
获取Outlook插件运行平台的Office JS API方案
当然有啦!Office JavaScript API 专门提供了一套工具来获取宿主环境的详细信息,完全能搞定你要的平台检测需求。
首先,最直接的就是使用Office.context.platform属性,它会返回一个枚举值,直接告诉你当前插件运行在哪个平台上:
Office.PlatformType.PC:对应Windows桌面版Outlook(包括2016及后续所有桌面版本)Office.PlatformType.Mac:Mac版OutlookOffice.PlatformType.Web:Outlook网页版(OWA)Office.PlatformType.iOS/Office.PlatformType.Android:移动版Outlook
下面是一个实用的代码示例,帮你把枚举值转换成易读的平台描述:
// 封装获取平台信息的函数 function getCurrentPlatform() { const platform = Office.context.platform; let platformName; switch (platform) { case Office.PlatformType.PC: platformName = "Windows 桌面版 Outlook(含2016及以上版本)"; break; case Office.PlatformType.Mac: platformName = "Mac 版 Outlook"; break; case Office.PlatformType.Web: platformName = "Outlook 网页版(OWA)"; break; case Office.PlatformType.iOS: platformName = "iOS 版 Outlook"; break; case Office.PlatformType.Android: platformName = "Android 版 Outlook"; break; default: platformName = "未知运行平台"; } console.log(`当前插件运行平台:${platformName}`); return platformName; } // 务必在Office初始化完成后调用 Office.initialize = (reason) => { getCurrentPlatform(); };
如果你还需要更详细的宿主信息(比如具体的Outlook版本号),可以使用Office.context.diagnostics对象,它包含了宿主应用类型、版本号、平台类型等更全面的诊断信息:
function getHostDiagnostics() { const diag = Office.context.diagnostics; console.log(`宿主应用:${diag.host}`); // 输出类似 "Outlook" console.log(`应用版本:${diag.version}`); // 输出类似 "16.0.15601.20000" console.log(`平台类型:${diag.platform}`); // 和Office.context.platform返回值一致 }
注意事项
- 所有这些API必须在
Office.initialize回调完成后调用,否则会因为上下文未初始化而报错; - 这些API在Outlook 2016及以上版本、OWA、Mac版Outlook中都得到了支持,兼容性不用担心。
内容的提问来源于stack exchange,提问作者Prateek Chaudhary
相关产品推荐
相关产品推荐

