Outlook Mac版Addin API body.getAsync丢失部分样式问题求助
I've run into this exact inconsistency with Outlook for Mac's Office JS API before, especially in older versions like 16.13. Here's a breakdown of what's happening and how you can work around it:
What's Causing the Issue
This is a client-specific bug in Outlook for Mac 16.13. When you insert bullet points via the Outlook UI, the client uses MSO-specific CSS classes (like MsoListParagraph) to render them correctly. However, when you call getAsync with CoercionType.Html, the API strips out these critical classes from the returned HTML. When you pass that sanitized HTML back to setAsync, Outlook can no longer recognize it as a bullet list, so the formatting disappears. This doesn't happen in OWA because the web version handles HTML coercion more consistently.
Workarounds to Try
- Upgrade Outlook for Mac: This is the most reliable fix. The 16.13 version is several years old, and Microsoft has resolved many API consistency issues in later releases (e.g., 16.50+). Upgrading your client will almost certainly eliminate the bullet point preservation problem.
- Manually Reconstruct the List Structure: If upgrading isn't an option, you can modify the HTML returned by
getAsyncto re-add the MSO list classes before callingsetAsync. Here's a simple example using regex to wrap bullet items in the required styling:
Note: This is a basic regex example—you may need to tweak it to match your specific content structure.// Get the HTML content via getAsync first Office.context.mailbox.item.body.getAsync(Office.CoercionType.Html, (result) => { if (result.status === Office.AsyncResultStatus.Succeeded) { let modifiedHtml = result.value; // Replace bullet characters with MSO-styled paragraphs modifiedHtml = modifiedHtml.replace(/•(.*?)(<\/p>)/g, "<p class='MsoListParagraph' style='text-indent:0in'>$1</p>"); // Call setAsync with the adjusted HTML Office.context.mailbox.item.body.setAsync(modifiedHtml, { coercionType: Office.CoercionType.Html }, (setResult) => { // Handle success/failure here }); } }); - Fallback to Text Coercion: If preserving full HTML styling isn't critical, using
CoercionType.Textwill retain the bullet characters themselves, even if you lose other formatting.
Additional Notes
While the official docs mention that getAsync and setAsync aren't strictly idempotent, this issue is a specific bug in the older Outlook for Mac client rather than a general API limitation. You can report this behavior to Microsoft through their developer feedback channels to help prioritize fixes for legacy versions, though upgrading remains the most practical solution.
内容的提问来源于stack exchange,提问作者BChen

