要删除HTML电子邮件中的嵌入式图像,请使用removeAttachmentAsync()
方法(在Mailbox API中)。
removeAttachmentAsync()
方法成功完成,但是其行为如下:
在Windows版Outlook中删除嵌入式图像的正确方法是什么?
第一个代码:
const test = async (event) => {
let timeout = null;
const item = Office.context.mailbox.item;
item.getAttachmentsAsync((result) => {
for (let i = 0; i < result.value.length; i++) {
item.removeAttachmentAsync(result.value[i].id, (result2) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
event.completed({ allowEvent: true });
}, 500);
});
}
});
}
更新:
我使用setAsync()
方法重写了代码。行为已更改:
修改后的代码:
const test = async (event) => {
let timeout = null;
const item = Office.context.mailbox.item;
item.getAttachmentsAsync((result) => {
for (let i = 0; i < result.value.length; i++) {
item.removeAttachmentAsync(result.value[i].id, (result2) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
item.body.getAsync(Office.CoercionType.Html, (result3) => {
item.body.setAsync(result3.value, { coercionType: Office.CoercionType.Html }, () => {
event.completed({ allowEvent: true });
});
});
}, 500);
});
}
});
}
答案 0 :(得分:0)
解决问题的代码:
const removeImagesFromBody = (event) => {
const item = Office.context.mailbox.item;
const type = Office.CoercionType.Html;
item.body.getAsync(type, (result) => {
let body = result.value;
let match;
const regex1 = new RegExp('v:shapes="([^"]+)"', 'gi');
while ((match = regex1.exec(result.value)) !== null) {
const regex2 = new RegExp(`<v:shape id="${match[1]}"[^]*?</v:shape>`, 'gi');
body = body.replace(regex2, '');
}
body = body.replace(/<img [^>]*>/gi, '');
item.body.setAsync(body, { coercionType: type }, () => {
event.completed({ allowEvent: true });
});
});
}