Javascript 如何通过word js替换大量内容控件中的文本?
我正在尝试编写一个函数,该函数以富文本内容控件列表和单个字符串作为参数,并用该字符串替换所有匹配内容控件的内容 虽然这种方法只适用于少量的内容控件,但对于包含大量内容控件的文档,则会失败。我必须处理具有700多个内容控件和单个标题的文档。在这种情况下,代码只替换前66X CCs,然后使用GeneralException中止。我认为这是由于大量的内容控制。当我尝试为所有这些CCs GeneralException注册绑定时,我也遇到了类似的问题。但这是一个不同的话题 我试图解决这个问题,通过限制每个.sync的更改量,并通过CCs循环,根据需要执行尽可能多的循环。然而,由于officejs的异步特性,这并不是那么容易。到目前为止,我还不太熟悉javascript异步promise编程。但我想到的是:Javascript 如何通过word js替换大量内容控件中的文本?,javascript,ms-word,ms-office,office-js,office-addins,Javascript,Ms Word,Ms Office,Office Js,Office Addins,我正在尝试编写一个函数,该函数以富文本内容控件列表和单个字符串作为参数,并用该字符串替换所有匹配内容控件的内容 虽然这种方法只适用于少量的内容控件,但对于包含大量内容控件的文档,则会失败。我必须处理具有700多个内容控件和单个标题的文档。在这种情况下,代码只替换前66X CCs,然后使用GeneralException中止。我认为这是由于大量的内容控制。当我尝试为所有这些CCs GeneralException注册绑定时,我也遇到了类似的问题。但这是一个不同的话题 我试图解决这个问题,通过限制每
function replaceCCtextWithSingleString (CCtitleList, string) {
var maxPerBatch = 100;
/*
* A first .then() block is executed to get proxy objects for all selected CCs
*
* Then we would replace all the text-contents in one single .then() block. BUT:
* Word throws a GeneralException if you try to replace the text in more then 6XX CCs in one .then() block.
* In consequence we only process maxPerBatch CCs per .then() block
*/
Word.run(function (context) {
var CCcList = [];
// load CCs
for(var i = 0; i < CCtitleList.length; i++) {
CCcList.push(context.document.contentControls.getByTitle(CCtitleList[i]).load('id'));
}
return context.sync().then(function () { // synchronous
var CClist = [];
// aggregate list of CCs
for(var i = 0; i < CCcList.length; i++) {
if(CCcList[i].items.length == 0) {
throw 'Could not find CC with title "'+CCtitleList[j]+'"';
}
else {
CClist = CClist.concat(CCcList[i].items);
}
}
$('#status').html('Found '+CClist.length+' CCs matching the criteria. Started replacing...');
console.log('Found '+CClist.length+' CCs matching the criteria. Started replacing...');
// start replacing
return context.sync().then((function loop (replaceCounter, CClist) {
// asynchronous recoursive loop
for(var i = 0; replaceCounter < CClist.length && i < maxPerBatch; i++) { // loop in loop (i does only appear in condition)
// do this maxPerBatch times and then .sync() as long as there are still unreplaced CCs
CClist[replaceCounter].insertText(string, 'Replace');
replaceCounter++;
}
if(replaceCounter < CClist.length) return context.sync() // continue loop
.then(function () {
$('#status').html('...replaced the content of '+replaceCounter+' CCs...');
return loop(replaceCounter, numCCs);
});
else return context.sync() // end loop
.then(function () {
$('#status').html('Replaced the content of all CCs');
});
})(0, CClist));
});
}).catch(function (error) {
$('#status').html('<pre>Error: ' + JSON.stringify(error, null, 4) + '</pre>');
console.log('Error: ' + JSON.stringify(error, null, 4));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo, null, 4));
}
throw error;
});
}
';
log'Error:'+JSON.stringifyerror,null,4;
如果OfficeExtension.error的实例出错{
log'Debug info:'+JSON.stringifyerror.debugInfo,null,4;
}
};
}
它仍然需要13995毫秒才能完成,但至少可以工作:-
有什么想法吗?是什么引起了普遍的感觉
我发布了一个关于速度问题的新问题:好问题。。很久以前我做了一些性能测试,我能够在一个文档中更改超过10k的内容控件。有了700,你应该没事了。 不确定为什么要预先填写一个列表,这是不必要的,实际上您在集合中导航了2次,这对性能不好。您可以在遍历集合时进行字符串比较 这里是一个例子,我刚刚用一个700内容控制文档做了一个快速测试,该文档带有一个假设的test标记 我能够 1.将他们的文本与你想比较的内容进行比较,它是一个字符串 2.如果条件为真,请更改该值 完成这个操作花费了5134毫秒,下面是代码。我认为这是可以接受的 希望这有帮助 函数perfContentControls{ var time1=Date.now;//让我们看看完成操作的时间: var CCs=0 Word.runfunction上下文{ var myCCs=context.document.body.contentControls.getByTagtest; context.loadmyCCs; 返回context.sync .Then函数{ CCs=myCCs.items.length 对于var i=0;i
function replaceCCtextWithSingleString_v1_1 (CCtitleList, string) {
Word.run(function (context) {
var time1 = Date.now();
// load the title of all content controls
var CCc = context.document.contentControls.load('title');
return context.sync().then(function () { // synchronous
// extract CC titles
var documentCCtitleList = [];
for(var i = 0; i < CCc.items.length; i++) { documentCCtitleList.push(CCc.items[i].title); }
// check for missing titles and replace
for(var i = 0; i < CCtitleList.length; i++) {
var index = documentCCtitleList.indexOf(CCtitleList[i]);
if(index == -1) { // title is missing
throw 'Could not find CC with title "'+CCtitleList[i]+'"';
}
else { // replace
CCc.items[index].insertText(string, 'Replace');
}
}
$('#status').html('...replacing...');
return context.sync().then(function () {
var time2 = Date.now();
var tdiff = time2-time1;
$('#status').html('Successfully replaced all selected CCs in '+tdiff+' ms');
});
});
}).catch(function (error) {
$('#status').html('<pre>Error: ' + JSON.stringify(error, null, 4) + '</pre>');
console.log('Error: ' + JSON.stringify(error, null, 4));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo, null, 4));
}
});
}