Office Word JS breaks on office on the web but not on desktop version of word

62 Views Asked by At

the following function works just fine on the following desktop-version of Word (Microsoft® Word for Microsoft 365 MSO (Version 2309 Build 16.0.16827.20130) 64-bit). However upon testing it with office on the web (tried Google Chrome + Edge - both updated to latest version) it breaks down.

The function creates sentences of a document body by splitting on "." with functionality for protecting some places where it shouldn't create a sentence.

It breaks down just after console.log('break15.5') and before console.log('break16')

The problem is therefore with the await context.sync() between the two console.log statements

The error code i receive is the usual rich.api error that doesn't help alot in terms of debugging. This is extended the error code (indicating something wrong with the expandto function):

OfficeExtension.Error.debugInfo for additional information. ","errorLocation":"Range.expandTo","statement":"var expandTo42 = v43.expandTo(...);","surroundingStatements":["...","var v42 = context.root._getObjectByReferenceId("{d347b1c8-cf0d-4ed2-a5ad-013d60035bba}{167}");","var expandTo41 = v42.expandTo(...);","// Instantiate {expandTo41}","expandTo41.load(["text"]);","var v43 = context.root._getObjectByReferenceId("{d347b1c8-cf0d-4ed2-a5ad-013d60035bba}{168}");","// >>>>>","var expandTo42 = v43.expandTo(...);","// <<<<<","// Instantiate {expandTo42}","expandTo42.load(["text"]);","var v44 = context.root._getObjectByReferenceId("{d347b1c8-cf0d-4ed2-a5ad-013d60035bba}{169}");","var expandTo43 = v44.expandTo(...);","// Instantiate {expandTo43}","..."],"fullStatements":["var v = context.root._getObjectByReferenceId("{d347b1c8-cf0d-4ed2-a5ad-013d60035bba}{125}");","var v1 = context.root._getObjectByReferenceId("{d347b1c8-cf0d-4ed2-a5ad-013d60035bba}{126}");","var expandTo = v.expandTo(v1);","

As stated the code works just fine on desktop but not on office on the web.

Please help.

The function is the following: (forgive the console.log statements for debugging)

var global_sentences3_text = []
var global_sentences3_ranges = []
var global_sentences_real = null
var global_contract_body = null
async function realsentenizer() {
  await Word.run(async (context) => {
    const body = context.document.body.getRange();
    body.load("text");
    await context.sync();

    console.log('break1')
    //for sending entire body of text to api for analysis
    global_contract_body = body.text

    console.log('break2')

    ///PROTECTING DOTS///
    //making sure we are not splitting on period in these cases: "clause 5.6" or "registration no. 555", they need to be done sequentially so they do not catch the same words and thus at the end insert them again twice.
    var saved_text = []
    var ranges_to_protect = body.search("[0-9].[0-9]", {matchWildcards: true, matchCase: false})
    console.log('break3')
    ranges_to_protect.load('text')
    console.log('break4')
    await context.sync()
    console.log('break5')
    for (let i = 0; i < ranges_to_protect.items.length ; i++) {
      saved_text.push(ranges_to_protect.items[i].text)
      ranges_to_protect.items[i].insertText('[PROTECTEDDOT]','Replace')
    }
    console.log('break6')
    await context.sync()
    
    var saved_text2 = []
    var ranges_to_protect2 = body.search("[Nn][Oo].", {matchWildcards: true, matchCase: false})
    ranges_to_protect2.load('text')
    await context.sync()
    for (let i = 0; i < ranges_to_protect2.items.length ; i++) {
      saved_text2.push(ranges_to_protect2.items[i].text)
      ranges_to_protect2.items[i].insertText('[PROTECTEDDOT]','Replace')
    }
    await context.sync()
    console.log('break7')
    
    var saved_text3 = []
    var ranges_to_protect3 = body.search("[0-9].", {matchWildcards: true, matchCase: false})
    ranges_to_protect3.load('text')
    await context.sync()
    for (let i = 0; i < ranges_to_protect3.items.length ; i++) {
      saved_text3.push(ranges_to_protect3.items[i].text)
      ranges_to_protect3.items[i].insertText('[PROTECTEDDOT]','Replace')
    }
    await context.sync()
    console.log('break8')

    var saved_text4 = []
    var ranges_to_protect4 = body.search("[Cc][Ff].", {matchWildcards: true, matchCase: false})
    ranges_to_protect4.load('text')
    await context.sync()
    for (let i = 0; i < ranges_to_protect4.items.length ; i++) {
      saved_text4.push(ranges_to_protect4.items[i].text)
      ranges_to_protect4.items[i].insertText('[PROTECTEDDOT]','Replace')
    }
    await context.sync()
    console.log('break9')

    var saved_text6 = []
    var ranges_to_protect6 = body.search("[A-Za-z].[A-Za-z]", {matchWildcards: true, matchCase: false})
    ranges_to_protect6.load('text')
    await context.sync()
    for (let i = 0; i < ranges_to_protect6.items.length ; i++) {
      saved_text6.push(ranges_to_protect6.items[i].text)
      ranges_to_protect6.items[i].insertText('[PROTECTEDDOT]','Replace')
    }
    await context.sync()
    console.log('break10')

    ///PROTECTING DOTS///

    //replacing sentences with "whereas" + ";" or ":" with . so it doesnt create big introduction sentence that merges with the first clause. Using pseudo_sentences because it needs its own split on "." on body.getTextRanges()
    const pseudo_sentences = body.getTextRanges(["."], false)
    pseudo_sentences.load('text')
    await context.sync()

    console.log('break11')
    
    var ranges_to_protect5 = []
    var searched_ranges = null
    for (let i = 0; i < pseudo_sentences.items.length ; i++) {
      if (pseudo_sentences.items[i].text.toLowerCase().includes('whereas') && pseudo_sentences.items[i].text.includes(';')){
        searched_ranges = pseudo_sentences.items[i].search(";", {matchWildcards: true, matchCase: false})
        searched_ranges.load('text')
        await context.sync()
        for (let i = 0; i < searched_ranges.items.length ; i++) {
          ranges_to_protect5.push(searched_ranges.items[i])
          searched_ranges.items[i].insertText('.','Replace')
        }
      }
    }
    await context.sync();
    console.log('break12')

    //splitting bodytext on "."
    const sentences = body.getTextRanges(["."], false);
    sentences.load('text')
    global_sentences_real = sentences
    await context.sync()

    console.log('break13')

    //reinserting protected texts - important that it is here before looping through the pairs.
    for (let i = 0; i < ranges_to_protect.items.length ; i++) {
      ranges_to_protect.items[i].insertText(saved_text[i],'Replace')
    }
    for (let i = 0; i < ranges_to_protect2.items.length ; i++) {
      ranges_to_protect2.items[i].insertText(saved_text2[i],'Replace')
    }
    for (let i = 0; i < ranges_to_protect3.items.length ; i++) {
      ranges_to_protect3.items[i].insertText(saved_text3[i],'Replace')
    }
    for (let i = 0; i < ranges_to_protect4.items.length ; i++) {
      ranges_to_protect4.items[i].insertText(saved_text4[i],'Replace')
    }
    for (let i = 0; i < ranges_to_protect5.length ; i++) {
      ranges_to_protect5[i].insertText(";",'Replace')
    }
    for (let i = 0; i < ranges_to_protect6.items.length ; i++) {
      ranges_to_protect6.items[i].insertText(saved_text6[i],'Replace')
    }
    await context.sync() 

    console.log('break14')

    const new_ranges =[]
    let saved_range = sentences.items[0]
    let extended_range = {};

    console.log('break15')

    console.log(sentences.items.length)
    // Loop through the ranges, checking adjacent pairs
    for (let i = 1; i < sentences.items.length ; i++) {
      console.log(`ITERATION: ${i}`)
      console.log('for_start')
      if (sentences.items[i].text.includes(".") == false){
        console.log('if_1')
        extended_range = saved_range.expandTo(sentences.items[i])
        console.log('if_2')
        extended_range.load('text')
        console.log('if_3')
        saved_range = extended_range
        console.log('if_4')
      } else {
        extended_range = saved_range.expandTo(sentences.items[i])
        console.log('else_1')
        extended_range.load('text')
        console.log('else_2')
        new_ranges.push(extended_range)
        console.log('else_3')
        saved_range = sentences.items[i+1]
        console.log('else_4')
      }
      console.log('for_end')      
    }   
    console.log('LOOP END FULL')  
    new_ranges.push(saved_range)
    
    console.log('break15.5')
    await context.sync();
    
    console.log('break16')

    ///testing new catch headers logic ///

    //catching headers that are all uppercase
    var new_ranges2 = []
    for (let i = 0; i < new_ranges.length ; i++) {
      if (new_ranges[i].text.toUpperCase() == new_ranges[i].text){
        const new_range = new_ranges[i].expandTo(new_ranges[i+1])
        new_range.load('text')
        new_ranges2.push(new_range)
        i += 1
      } else {
        new_ranges[i].load('text')
        new_ranges2.push(new_ranges[i])
      }
    }
    await context.sync()

    console.log('break17')

    ///testing new catch headers logic ///

    for (let sentence of new_ranges2) {
      sentence.load('text')
      global_sentences3_text.push(sentence.text)
      sentence.track()
      global_sentences3_ranges.push(sentence)
    }
    await context.sync()
    
    for (let sentence of global_sentences3_text) {
      console.log(sentence)
    }

    console.log('break18')

    global_sentences_real.track()
    
  await context.sync()
  console.log('Succesfully sentenized')
  });
}
1

There are 1 best solutions below

0
Eugene Astafiev On

Try to not to call the context.sync method again before the previous one is not finished yet. See Proper way to wait for one function to finish before continuing? for more information.