问题详情:
在开发WebOffice插件,使用AI对话流式输出将内容添加到文档中时,会返回例如
- ...
- ...
- ...
这样的有序列表,但是在使用【ActiveDocument.ActiveWindow.Selection.InsertAfter】API插入文本时,会出现下面这种情况
- ...
- 2. ...
- 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>
1361

被折叠的 条评论
为什么被折叠?



