Office.Actions interface

管理操作和键盘快捷方式。

方法

areShortcutsInUse(shortcuts)

检查用户当前是否正在使用另一加载项或 Office 应用程序定义的一组快捷方式组合。 有关详细信息,请参阅 将自定义键盘快捷方式添加到 Office 加载项

associate(actionId, actionFunction)

将操作的 ID 或名称与函数相关联。

getShortcuts()

获取加载项的现有快捷方式。 该组合始终包括 (1) 加载项清单中定义的键盘快捷方式和 (2) 当前用户的自定义快捷方式(如果存在)。 如果快捷方式与其他加载项的快捷方式或 Office 应用程序冲突,则它可以是 null 。 具体来说,如果在提示选择要使用的快捷方式时,用户没有选择当前加载项的操作,就会发生这种情况 null 。 有关快捷方式冲突的详细信息,请参阅 避免其他加载项使用的组合键。

replaceShortcuts(shortcuts)

为用户使用自定义快捷方式替换现有加载项快捷方式。

方法详细信息

areShortcutsInUse(shortcuts)

检查用户当前是否正在使用另一加载项或 Office 应用程序定义的一组快捷方式组合。 有关详细信息,请参阅 将自定义键盘快捷方式添加到 Office 加载项

areShortcutsInUse(shortcuts: string[]): Promise<Array<{shortcut: string, inUse: boolean}>>;

参数

shortcuts

string[]

快捷方式组合数组。 例如, ["Ctrl+1", "Ctrl+2"].

返回

Promise<Array<{shortcut: string, inUse: boolean}>>

解析为对象数组的承诺。 每个对象都由快捷方式组合和布尔值组成。 如果快捷方式组合与其他加载项的快捷方式或 Office 应用程序的快捷方式冲突,则产生该值 true ;否则为 false。 例如, [{shortcut:"Ctrl+1", inUse:true},{shortcut:"Ctrl+2", inUse:false}].

注解

要求集

示例

// Checks if a specific keyboard shortcut is in use.
const shortcuts = ["Ctrl+Shift+1", "Ctrl+Shift+2"];
Office.actions.areShortcutsInUse(shortcuts)
    .then((shortcutsInUse) => {
        const availableShortcuts = shortcutsInUse.filter((shortcut) => { return !shortcut.inUse; });
        console.log(`Available keyboard shortcuts: ${availableShortcuts}`);
        const usedShortcuts = shortcutsInUse.filter((shortcut) => { return shortcut.inUse; });
        console.log(`Shortcuts in use: ${usedShortcuts}`);
});

associate(actionId, actionFunction)

将操作的 ID 或名称与函数相关联。

associate(actionId: string, actionFunction: (arg?: any) => void): void;

参数

actionId

string

清单中定义的操作的 ID。

actionFunction

(arg?: any) => void

调用操作时运行的函数。

返回

void

示例

// Maps the action ID to the showTaskPane function.
Office.actions.associate("ShowTaskpane", showTaskPane);

// Displays the add-in's task pane.
function showTaskPane() {
    return Office.addin.showAsTaskpane()
        .then(() => { console.log("Task pane is visible."); })
        .catch((error) => {
            console.log(error.code);
        });
}

getShortcuts()

获取加载项的现有快捷方式。 该组合始终包括 (1) 加载项清单中定义的键盘快捷方式和 (2) 当前用户的自定义快捷方式(如果存在)。 如果快捷方式与其他加载项的快捷方式或 Office 应用程序冲突,则它可以是 null 。 具体来说,如果在提示选择要使用的快捷方式时,用户没有选择当前加载项的操作,就会发生这种情况 null 。 有关快捷方式冲突的详细信息,请参阅 避免其他加载项使用的组合键。

getShortcuts(): Promise<{[actionId: string]: string|null}>;

返回

Promise<{[actionId: string]: string|null}>

解析为快捷方式对象的承诺,键是清单) 中定义的操作 (的 ID,值是快捷方式组合。 例如, {"SetItalic": "Ctrl+1", "SetBold": "Ctrl+2", "SetUnderline": null}.

注解

要求集

示例

// Gets the list of keyboard shortcuts for an add-in.
Office.actions.getShortcuts()
    .then((shortcuts) => {
        for (const action in shortcuts) {
            let shortcut = shortcuts[action];
            console.log(`${action}: ${shortcut}`);
        }
});

replaceShortcuts(shortcuts)

为用户使用自定义快捷方式替换现有加载项快捷方式。

replaceShortcuts(shortcuts: {[actionId: string]: string}): Promise<void>;

参数

shortcuts

{[actionId: string]: string}

自定义快捷方式的对象,其中键是操作的 ID,值是快捷方式组合。 例如, {"SetItalic": "Ctrl+1", "SetBold": "Ctrl+2"}. 若要了解如何指定有效的操作 ID 和组合键,请参阅向 Office 加载项添加自定义键盘快捷方式。 (请注意,键组合可以为 null,在这种情况下,操作将保留 JSON 文件中指定的键组合。)

返回

Promise<void>

一个承诺,在注册每个自定义快捷方式分配 shortcuts 后解析。 即使与现有快捷方式发生冲突,也会注册自定义快捷方式。 否则,承诺将被拒绝,并显示错误代码和错误消息。 OfficeExtension.ErrorCodes.invalidArgument如果其中shortcuts的任何操作 ID 不存在,或快捷方式组合无效,则返回错误代码。

注解

要求集

示例

// Replaces the keyboard shortcuts of an add-in.
const customShortcuts = {
    ShowTaskpane:"Ctrl+Shift+1",
    HideTaskpane:"Ctrl+Shift+2"
};
Office.actions.replaceShortcuts(customShortcuts)
    .then(() => { console.log("Keyboard shortcuts successfully registered."); })
    .catch((error) => {
        if (error.code === "InvalidOperation") {
            console.log("ActionId does not exist or shortcut combination is invalid.");
        }
});