Excel 加载项 |根据条件启用/禁用功能区图标

问题描述

我正在使用 Office.js、javascript 和 react 来创建 excel 加载项。希望在用户成功登录后启用功能区图标。使用 Office 对话框 API 进行身份验证。

  1. 下面是 Manifest.xml 文件中用于最初禁用“HomeButton 菜单”图标的标记

     <Action xsi:type="ShowTaskpane">
        <TaskpaneId>DataDirectId</TaskpaneId>
        <SourceLocation resid="HomeButton.Url" />
     </Action>
     <Enabled>false</Enabled>
    
  2. 单击功能区任务盘中的登录菜单图标时,将打开包含“登录和注销”按钮的登录页面

  3. 点击办公室对话框 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);
};
  1. 下面是从 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

enter image description here

请帮助解决这个问题。感谢您的帮助。 提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)