首页 > 解决方案 > Office Addin:我无法调用元素中的函数在元素中

问题描述

我目前正在开发 Word 加载项。在 manifest.xml 中,我创建了一个元素,其中包含一个元素类型为 button 的元素。我有一个 xsi 元素:type = ExecuteFunction,其中一个“testEventObject”函数作为一个元素。在 FunctionFile 中,我使用“testEventObject”方法调用包含脚本的 commands.html 页面。我已经在函数中放置了一个警报和一个 console.log,但是当我点击按钮时,什么也没有发生。预先感谢您的帮助 !

清单.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<OfficeApp xmlns="http://schemas.microsoft.com/office/appforoffice/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bt="http://schemas.microsoft.com/office/officeappbasictypes/1.0" xmlns:ov="http://schemas.microsoft.com/office/taskpaneappversionoverrides" xsi:type="TaskPaneApp">

  <Id>a963b4a9-15fa-4d1e-8bad-8c03c6027ad7</Id>

  <Version>1.0.0.0</Version>

  <ProviderName>Aproove</ProviderName>

  <DefaultLocale>en-US</DefaultLocale>

  <DisplayName DefaultValue="Aproove"/>

  <Description DefaultValue="Aproove description."/>

  <IconUrl DefaultValue="https://localhost:3000/assets/icon-32.png"/>

  <HighResolutionIconUrl DefaultValue="https://localhost:3000/assets/icon-80.png"/>

  <SupportUrl DefaultValue="https://www.aproove.com/fr-fr/aproove-work-management-contact-us"/>

  <AppDomains>
    <AppDomain>https://www.aproove.com</AppDomain>
  </AppDomains>

  <Hosts>
    <Host Name="Document"/>
  </Hosts>

  <DefaultSettings>
    <SourceLocation DefaultValue="https://localhost:3000/taskpane.html"/>
  </DefaultSettings>

  <Permissions>ReadWriteDocument</Permissions>
  <VersionOverrides xmlns="http://schemas.microsoft.com/office/taskpaneappversionoverrides" xsi:type="VersionOverridesV1_0">
    <Hosts>
      <Host xsi:type="Document">
        <DesktopFormFactor>

          <GetStarted>
            <Title resid="GetStarted.Title"/>
            <Description resid="GetStarted.Description"/>
            <LearnMoreUrl resid="GetStarted.LearnMoreUrl"/>
          </GetStarted>

          <FunctionFile resid="Commands.Url"/>

          <ExtensionPoint xsi:type="PrimaryCommandSurface">

            <CustomTab id="Aproove.Tab">

              <Group id="Aproove.Tab1.Group1">

                <Label resid="Aproove.Tab1.GroupLabel" />

                <Icon>
                  <bt:Image size="16" resid="Icon.16x16" />
                  <bt:Image size="32" resid="Icon.32x32" />
                  <bt:Image size="80" resid="Icon.80x80" />
                </Icon>

                <Control xsi:type="Button" id="Aproove.TaskpaneButton">

                  <Label resid="Aproove.TaskpaneButton.Label" />

                  <Supertip>

                    <Title resid="Aproove.TaskpaneButton.Label" />

                    <Description resid="Aproove.TaskpaneButton.Tooltip" />

                  </Supertip>

                  <Icon>
                    <bt:Image size="16" resid="Icon.16x16" />
                    <bt:Image size="32" resid="Icon.32x32" />
                    <bt:Image size="80" resid="Icon.80x80" />
                  </Icon>

                  <Action xsi:type="ShowTaskpane">

                    <TaskpaneId>ButtonId1</TaskpaneId>
                
                    <SourceLocation resid="Taskpane.Url" />

                  </Action>
                </Control>
              </Group>

              <Group id="Aproove.Tab1.GroupSettings">

                <Label resid="Aproove.Tab1.GroupLabelSettings" />

                <Icon>
                  <bt:Image size="16" resid="Icon.16x16" />
                  <bt:Image size="32" resid="Icon.32x32" />
                  <bt:Image size="80" resid="Icon.80x80" />
                </Icon>

                <Control xsi:type="Button" id="Aproove.SettingsButton">

                  <Label resid="Aproove.SettingsButton.Label" />

                  <Supertip>

                    <Title resid="Aproove.SettingsButton.Label" />

                    <Description resid="Aproove.SettingsButton.Tooltip" />

                  </Supertip>

                  <Icon>
                    <bt:Image size="16" resid="Icon.16x16" />
                    <bt:Image size="32" resid="Icon.32x32" />
                    <bt:Image size="80" resid="Icon.80x80" />
                  </Icon>
            
                  <Action xsi:type="ExecuteFunction">
                    <FunctionName>testEventObject</FunctionName>
                  </Action>
                </Control>
              </Group>

              <Label resid="Aproove.Tab.TabLabel" />

            </CustomTab>

          </ExtensionPoint>
        </DesktopFormFactor>
      </Host>
    </Hosts>

    <Resources>

      <bt:Images>
        <bt:Image id="Icon.16x16" DefaultValue="https://localhost:3000/assets/icon-16.png"/>
        <bt:Image id="Icon.32x32" DefaultValue="https://localhost:3000/assets/icon-32.png"/>
        <bt:Image id="Icon.80x80" DefaultValue="https://localhost:3000/assets/icon-80.png"/>
      </bt:Images>

      <bt:Urls>
        <bt:Url id="GetStarted.LearnMoreUrl" DefaultValue="https://go.microsoft.com/fwlink/?LinkId=276812"/>
        <bt:Url id="Commands.Url" DefaultValue="https://localhost:3000/commands.html"/>
        <bt:Url id="Taskpane.Url" DefaultValue="https://localhost:3000/taskpane.html"/>
      </bt:Urls>

      <bt:ShortStrings>
        <bt:String id="GetStarted.Title" DefaultValue="Get started with your sample add-in!"/>
        <bt:String id="CommandsGroup.Label" DefaultValue="Commands Group"/>
        <bt:String id="TaskpaneButton.Label" DefaultValue="Show Taskpane"/>

        <bt:String id="Aproove.Tab.TabLabel" DefaultValue="Aproove"/>
        <bt:String id="Aproove.Tab1.GroupLabel" DefaultValue="Commands Group Aproove"/>
        <bt:String id="Aproove.TaskpaneButton.Label" DefaultValue="Show notes"/>

        <bt:String id="Aproove.Tab1.GroupLabelSettings" DefaultValue="User"/>
        <bt:String id="Aproove.SettingsButton.Label" DefaultValue="Settings"/>
      </bt:ShortStrings>

      <bt:LongStrings>
        <bt:String id="GetStarted.Description" DefaultValue="Your sample add-in loaded succesfully. Click here et click in the 'Show notes' button to view notes."/>
        <bt:String id="TaskpaneButton.Tooltip" DefaultValue="Click to Show a Taskpane"/>
        <bt:String id="Aproove.TaskpaneButton.Tooltip" DefaultValue="Click to Show a Taskpane"/>

        <bt:String id="Aproove.SettingsButton.Tooltip" DefaultValue="Click to show settings"/>
      </bt:LongStrings>

    </Resources>
  </VersionOverrides>
</OfficeApp>

还有commands.html:

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />

    <!-- Office JavaScript API -->
    <script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js"></script>

    <script>

        Office.initialize = function () {
           
        }
        function testEventObject(event) {
          
            window.alert("hey");
            console.log("ici");

            event.completed();
        }
    </script>
</head>

<body>
  
</body>

</html>

标签: javascriptxmlms-wordoffice-js

解决方案


Office.initialize部分必须在匿名函数内。

这是正确的脚本:

(function () {
    Office.initialize = function () {
         
    };
})();

function testEventObject(event) {
          
     window.alert("hey");
     console.log("ici");

     event.completed();
}

推荐阅读