问题描述
我正在为Ms-Word创建加载项,在一种情况下,我需要检查文件名并设置自定义加载项按钮以启用/禁用此功能?
我尝试了许多方法来解决它,但是它们对我不起作用。
下面给出了代码。
Office.initialize = function () {
// Office is ready
await Word.run(function (context) {
loadFileName();
return context.sync();
})
.catch(function (error) {
console.log("Error: " + error);
$('#errorDiv').text(error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
};
Office.onReady(async () => {
});
function loadFileName() {
var a = "1";
var name = "";
Office.context.document.getFilePropertiesAsync(null,(res) => {
if (res && res.value && res.value.url) {
a += "2";
name = res.value.url.substr(res.value.url.lastIndexOf('\\') + 1);
a += "2.1";
a += name;
$('#errorDiv').text(a);
test(name,a);
}
});
}
function test(name,a) {
a += "test";
$('#errorDiv').text(a);
if (name != null && name != "" && name.toLowerCase().startsWith("cleancopy")) {
a += "3";
Office.ribbon.requestUpdate({
tabs: [
{
id: "Contoso.Tab1",controls: [
{
id: "Contoso.FunctionButton",enabled: false
},{
id: "Contoso.TaskpaneButton",]
}
]
});
a += "4";
}
$('#errorDiv').text(a);
}
当前不使用代码,当我单击“任务”窗格按钮时,此方法正在调用。
如果有人有一个主意,那就对我有很大帮助。
解决方法
要测试是否支持,您的代码应调用Office.context.requirements.isSetSupported('RibbonApi','1.1')
。当且仅当该调用返回true
时,您的代码才能调用启用/禁用API。如果isSetSupported
的调用返回false
,则所有自定义加载项命令始终处于启用状态。
我将验证并添加代码。
,不幸的是,如文档here所述,目前Excel仅支持Ribbon API 1.1。
当前在Word或PPT上无法实现。
如果您希望在Word和PPT上看到此功能-请发布在Office Dev Suggestion Box上。