WPS weboffice插件开发,流式插入有序列表,会出现自动编号,导致编号重复的问题

问题详情:

在开发WebOffice插件,使用AI对话流式输出将内容添加到文档中时,会返回例如

  1. ...
  2. ...
  3. ...

这样的有序列表,但是在使用【ActiveDocument.ActiveWindow.Selection.InsertAfter】API插入文本时,会出现下面这种情况

  1. ...
  2. 2. ...
  3. 3. ...

插入时触发了文档【自动编号】功能,自动加上了索引

因为【数字+.+空格】的格式会自动唤醒【自动编号】功能

但翻遍wps的开发文档,没有找到一个可以取消该功能的API,于是尝试用不可见字符来破坏【数字+.+空格】格式,使【自动编号】功能不被唤醒,后面尝试亲测有效,可以给大家借鉴一下

解决方案:

<script type="text/javascript">
const jssdk = WebOfficeSDK.config({
			mount: document.querySelector('.custom-mount'),
			url: "xxxxxxxxx" 
		});
 

let _buffer = {
  contentParts: [],     
  isProcessing: false,  
  timeoutId: null  
};
 
async function insertContent(content) {

  _buffer.contentParts.push(content);
  
  // 若已有处理任务存在,清空旧计时器,防止重复
  if (_buffer.timeoutId) clearTimeout(_buffer.timeoutId);
  

  _buffer.timeoutId = setTimeout(async () => {
      
    if (_buffer.isProcessing) return;
    _buffer.isProcessing = true;
    
    try {

      const mergedContent = _buffer.contentParts.slice(0,4).join('');
      _buffer.contentParts = _buffer.contentParts.slice(4);
      
      const processedContent = mergedContent.replace(
        /(\d+)(\.)(\s)/g, 
        (_, num, dot, space) => `${num}${dot}\u200B${space}`
      );
      
      console.log('[插入内容] 原始:', mergedContent, '处理结果:', processedContent);
      
      await jssdk.ready();
      const app = jssdk.Application;
      await app.ActiveDocument.ActiveWindow.Selection.InsertAfter(processedContent);
    } catch (error) {
      console.error('插入异常:', error);
    } finally {
      _buffer.isProcessing = false;
        
      // 剩余数据
      if (_buffer.contentParts.length > 0) {
        setTimeout(() => insertContent(''), 10); 
      }
    }
  }, 50); 
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值