You need to enable JavaScript to run this app. Outlook插件中如何实现JavaScript获取的API文本按字数自动换行?
优惠活动
大模型
产品
解决方案
定价
更多

Outlook插件中如何实现JavaScript获取的API文本按字数自动换行?

问题根因

  1. <pre> 标签默认样式为 white-space: pre,仅保留原始换行符,不会自动根据容器宽度折行,长文本会直接溢出
  2. 代码存在两处明显错误:
  • JS 中给 <pre> 设置了 width:30px,宽度过小导致文本必然被截断
  • HTML 中的 CSS 类选择器漏掉了.,同时给标题设置了不合理的 width:10px,样式完全不生效
  1. 未配置长文本强制折行规则,无空格的长字符串(如邮箱、标题)会直接溢出容器

解决方案

步骤1:修改JS中pre标签的样式配置

删掉错误的width:30px,新增自动折行相关样式:

for (let i = 0; i < data.length; i++) {
    let text = "Subject: " + JSON.stringify(data[i].subject) + "<br>"+
    "CC Emails: " + JSON.stringify(data[i].cc_emails).replace("[]","No Emails are CC'd").replace("[","").replace("]","") + "<br>" +
    "Ticket Creation Date: " + JSON.stringify(data[i].created_at) + "<br>" +
    "Ticket Status: " + JSON.stringify(data[i].status).replace("2", "Open").replace("3", "Pending").replace("4", "Resolved").replace("5", "Closed").replace("6", "Waiting On Customer") ;
    let pre = document.createElement('pre');
    pre.innerHTML = text;
    // 修正后的样式
    pre.style.cssText += 'font-size:24px;font-weight:bold;width:100%;white-space:pre-wrap;word-wrap:break-word;overflow-x:hidden;'
    output.appendChild(pre);
}

步骤2:修正HTML中的CSS样式

修复类选择器错误,新增全局pre标签样式保障优先级:

<style>
    /* 修复类选择器,删除不合理的宽高配置 */
    .ms-Panel-headerText{
        margin: 0;
        padding: 0;
        background-color: white;
    }
    /* 全局配置pre标签折行规则,优先级更高 */
    pre {
        white-space: pre-wrap !important;
        word-wrap: break-word !important;
        width: 100% !important;
        overflow-x: hidden !important;
        margin: 12px 0;
    }
    /* 可选:如果父容器仍有溢出,补充以下配置 */
    .ms-Panel-content {
        width: 100%;
        overflow-x: hidden;
    }
</style>

内容的提问来源于stack exchange,提问作者user16451367

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 16:06:03