如何解决Excel 加载项 |根据条件启用/禁用功能区图标
我正在使用 Office.js、javascript 和 react 来创建 excel 加载项。希望在用户成功登录后启用功能区图标。使用 Office 对话框 API 进行身份验证。
-
下面是 Manifest.xml 文件中用于最初禁用“HomeButton 菜单”图标的标记。
<Action xsi:type="ShowTaskpane"> <TaskpaneId>DataDirectId</TaskpaneId> <SourceLocation resid="HomeButton.Url" /> </Action> <Enabled>false</Enabled>
-
单击功能区任务盘中的登录菜单图标时,将打开包含“登录和注销”按钮的登录页面。
-
点击办公室对话框 API 代码下方的登录按钮时,将被调用。
Office.context.ui.displayDialogAsync( dialogLoginUrl,{ height: 45,width: 40 },result => { if (result.status === Office.AsyncResultStatus.Failed) { displayError(`${result.error.code} ${result.error.message}`); } else { loginDialog = result.value; loginDialog.addEventHandler( Office.EventType.DialogMessageReceived,processLoginMessage,); loginDialog.addEventHandler( Office.EventType.DialogEventReceived,processLoginDialogEvent,); } },
);
// 处理程序:
const processLoginMessage = arg => {
const messageFromDialog = JSON.parse(arg.message);
if (messageFromDialog.status === 'success') {
// below method using to enable ribbon Home button icon
enableButton();
loginDialog.close();
// console.log('login detail==',messageFromDialog.result);
localStorage.setItem('loggedIn','yes');
} else {
// Something went wrong with authentication or the authorization of the web application.
loginDialog.close();
displayError(messageFromDialog.result);
}
};
const processLoginDialogEvent = arg => {
processDialogEvent(arg,setState,displayError);
};
- 下面是从 Enable / Disable 中引用的 enableButton 方法
// 来自 enableButton 方法的代码
const enableButton = () => {
// Tried with this option 1
OfficeRuntime.ui.getRibbon().then(ribbon => {
ribbon.requestUpdate({
tabs: [
{
id: 'ShareTime',controls: [
{
id: 'BtnNTHome',enabled: true,},],});
});
// Tried with this option 2
Office.ribbon.requestUpdate({
tabs: [
{
id: 'ShareTime',controls: [
{
id: 'BtnNTHome',});
// Tried with this option 3
const button = { id: 'BtnNTHome',enabled: true };
const parentTab = { id: 'ShareTime',controls: [button] };
const ribbonUpdater = { tabs: [parentTab] };
Office.ribbon.requestUpdate(ribbonUpdater);
};
尝试了以上 3 个选项,但出现以下错误: excel-web-16.00.js:26 Uncaught (in promise) RichApi.Error:您尝试使用的 API 不可用。它可能在不同的场景中可用。 在新的 n (https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:26:300800) 在 r.i.processRequestExecutorResponseMessage (https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:26:362097) 在https://appsforoffice.microsoft.com/lib/1/hosted/excel-web-16.00.js:26:360202
请帮助解决这个问题。感谢您的帮助。 提前致谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。