我正在使用 HTML、CSS、JS 和 Blazor 应用程序为 outlook 开发网络插件。
使用此插件,新撰写的邮件在发送时必须更改内容。 简而言之,我想在发送邮件的同时更改邮件正文的内容 这是我在函数文件中的代码。
//INITIALIZE OFFICE
Office.initialize = function () { }
//THIS FUNCTION IS USED TO EXECUTE ON SEND EVENT
async function ExecuteOnSend(event) {
try {
const mail_item = Office.context.mailbox.item;
mail_item.body.getAsync('html', function (body_get_response) {
if (body_get_response.status === 'succeeded') {
mail_item.body.setSelectedDataAsync("Hello world", { coercionType: Office.CoercionType.Html }, function (body_set_response) {
if (body_set_response.status === "failed") {
console.log("FAILED");
} else {
console.log("SUCCESS");
}
});
} else {
console.error(body_get_response.error);
}
});
} catch (error_message) {
console.log("Error occurred : " + error_message);
}
event.completed();
}
//REGISTRATION OF FUNCTIONS
Office.actions.associate("ExecuteOnSend", ExecuteOnSend);
setAsync
和setSelectedDataAsync
这两种方法我都用过。但是我没有做以上的事情。
我还注意到,这两种方法的执行,既没有去到if
案例,也没有去到else
案例。
所以“失败”或“成功”没有打印在控制台上。
if (body_set_response.status === "failed") {
console.log("FAILED");
} else {
console.log("SUCCESS");
}
请注意,此邮件的项目类型为“消息”&发送邮件时调用“ExecuteOnSend”函数&我也可以获取邮件正文.
如何解决这个问题?提前致谢。
在代码中,您异步调用
body.getAsync
方法,这意味着它将与函数中的其余代码并行运行。看起来下面的代码行在调用回调并完成其工作之前执行:
event.completed();
我建议仅当从
async函数检索到结果时才调用
completed
方法。所以代码可能看起来像下面这样:
//THIS FUNCTION IS USED TO EXECUTE ON SEND EVENT
async function ExecuteOnSend(event) {
try {
const mail_item = Office.context.mailbox.item;
mail_item.body.getAsync('html', function (body_get_response) {
if (body_get_response.status === 'succeeded') {
mail_item.body.setSelectedDataAsync("Hello world", { coercionType: Office.CoercionType.Html }, function (body_set_response) {
if (body_set_response.status === "failed") {
console.log("FAILED");
event.completed();
} else {
console.log("SUCCESS");
event.completed();
}
});
} else {
console.error(body_get_response.error);
event.completed();
}
});
} catch (error_message) {
console.log("Error occurred : " + error_message);
}
}