javascript - 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>
解决方案
该Office.initialize
部分必须在匿名函数内。
这是正确的脚本:
(function () {
Office.initialize = function () {
};
})();
function testEventObject(event) {
window.alert("hey");
console.log("ici");
event.completed();
}
推荐阅读
- neo4j - 如何识别 Neo4j 图中的跳跃关系
- c# - 如何在 Yeoman 生成器中创建一个新的 .net 核心项目
- c++ - 从基类到派生类的动态转换以访问方法
- python - 在 Python 中预填充输入
- c# - 如何处理在 Assert 中返回 null 的变量
- google-play - 是否允许从 Google Play 游戏链接到 HTML5 游戏网站?
- python - 如何设置默认的 conda 环境,以便每当我打开终端时它应该被激活而不是 base?
- r - 如何将 r 中的数据帧分成相等数量的记录组,并在两个数据帧中随机平均分割数据
- python - ORtools 为每个学生分配一门常规课程或三门特殊课程
- javascript - 从毫秒转换日期而不本地化它们