javascript - What is the fastest way of replacing the text of many content controls via office-js? -


i wrote following function replace huge amount of ccs specified values:

/** replacemanyccs()  *  * replaces content of many content controls, based on replacementobject.  *  * @arg replacementobject   dictionary. keys titles of ccs should replaced. values replacement values.  */ function replacemanyccs (replacementobject) {     word.run(function (context) {         var time1 = date.now();          // load title of content controls         var ccc = context.document.contentcontrols.load('title');          return context.sync().then(function () { // synchronous             // extract cc titles             var documentcctitlelist = [];             for(var = 0; < ccc.items.length; i++) { documentcctitlelist.push(ccc.items[i].title); }              // check missing titles , replace             var replacecounter = 0;             for(var key in replacementobject) {                 var index = documentcctitlelist.indexof(key);                 if(index == -1) { // title missing                     throw 'could not find cc title "'+key+'"';                 }                 else { // replace                     ccc.items[index].inserttext(' '+replacementobject[key], 'replace');                     replacecounter++;                 }             }              $('#status').html('...replacing...');              return context.sync().then(function () {                 var time2 = date.now();                 var tdiff = time2-time1;                 $('#status').html('replaced '+replacecounter+' ccs in '+(tdiff/1000)+' seconds');             });         });     }).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));         }     }); } 

replacing 816 ccs requires 50-60 seconds code. there better/faster ways of accomplishing this?

good follow question. need know more details how big replacementobject array assuming @ least same size # of content controls in document, has direct impact on overall perf of method. can see from previous answer, updating 700 ccs fixed value takes no more 5 seconds, think satellite operations doing find new values affecting negatively performance.

specifically, see couple of things doing impact negatively perf, , potentially fix.

  1. you traversing @ least 2 times content control collection. 1 getting titles (and push them temp array), , 1 replace content (if replacement object matched ccs in doc). try in single pass.
  2. on top of above, in inner loop finding titles, practically traversing replacementobject each cc (this means @ least traversing array 700 times) , make things worse using array.indexof method find index key (just fyi in browser use add-ins, latest ie, this expensive way of traversing array, in fact 90% slower if loop trying find indesx given key, simple change theoretically 90% faster in latest ie, if want keep logic). check out page demonstrating this (use latest ie browser test). enter image description here

i think should tag (or title) each content control index directly maps relacementobject array position (title=index in replacement object , directly use index of replacementobject array change values in single pass. if there way of doing fastest outcome!


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -