2017-08-29 5 views
1

Word文書の各段落のテキストをJSON形式のデータに対して処理し、その結果をindex.htmlファイル内のaに出力するOfficeアドインを開発しています。タスクペインに表示されます。これは正常に動作します。私は今、JSONデータのキーのヒットに対応するWord文書内の文字列をフォーマットしようとしています。OfficeアドインでWord.SearchOptionsを使用する

私は "Office.initialize"と呼ばれ、JSONデータに基づいて変数を定義し、上記の機能に関連するユーティリティ関数を持つindex.htmlファイルの先頭にJSブロックを持っています。その後、Wordのコンテキストを取得し、JSONデータに対してWordのファイル段落を処理し、ヒットをフォーマットするためにWord段落自体を検索しようとする機能があります。この最後の作業では、Michael Mainer 1からスニペットを再現しようとしています。しかし、私はこの機能を有効にするとフォーマットが行われません。残念ながら私はMacを使用しているのでコンソールにアクセスすることはできません。これはデバッグが難しくなります。

私が間違っている場所を私に見せてくれる人に感謝します。あなただけのフォントプロパティにアクセスする必要があるように見えます

`ファンクションテスター(){ Word.run(機能(コンテキスト){

// Create a proxy object for the document's paragraphs 
    var paragraphs = context.document.body.paragraphs; 

    // Load the paragraphs' text, which I run regexes on 
    context.load(paragraphs, 'text'); 

    return context.sync().then(function() { 
     for (var i = 0; i < paragraphs.items.length; i++) { 
      var text = paragraphs.items[i].text; 
      // jquery to iterate over the "notes" objects from the JSON 
      $.each(notes, function(key, value) { 
       var regex = new RegExp("\\b" + key + "\\b", "g"); 
       var res = regex.test(text); 
       // if the regex hits... 
       if (res == true) { 
        // This part works fine, using the JSON data to append to the <DIV> with ID = "notes" 
        document.getElementById('notes').innerHTML += "<button onclick=hide('" + value.seqNo + "')><b>" + key + "</b></button><p class='" + value.seqNo + "' id='" + i + "'>" + value.notes[0].note + "</p>"; 

        // I now go on to searching for these hits within the current paragraph in the Word file 
        var thisPara = paragraphs.items[i]; 

        // Set up the search options. 
        var options = Word.SearchOptions.newObject(context); 
        options.matchCase = false 

        // Queue the commmand to search the current paragraph for occurrences of the string "key" (coming from the JSON data) 
        var searchResults = paragraphs.items[i].search(key, options); 

        // Load 'text' and 'font' for searchResults. 
        context.load(searchResults, 'text, font'); 

        // Synchronize the document state by executing the queued-up commands, and return a promise to indicate task completion. 
        return context.sync().then(function() { 

         // Queue a command to change the font for each found item. 
         for (var j = 0; j < searchResults.items.length; j++) { 
          searchResults.items[j].font.color = '#FF0000' 
          searchResults.items[j].font.highlightColor = '#FFFF00'; 
          searchResults.items[j].font.bold = true; 
         } 

         // Synchronize the document state by executing the queued-up commands, 
         // and return a promise to indicate task completion. 
         return context.sync(); 
        }); 
       } 
      }); 
     } 

    }); 
}) 
.catch(function (error) { 
    console.log('Error: ' + JSON.stringify(error)); 
    if (error instanceof OfficeExtension.Error) { 
     console.log('Debug info: ' + JSON.stringify(error.debugInfo)); 
    } 
}); 

} `

答えて

0

、あなただけ試してみました:

私のために働いていた
context.load(searchResults, 'font'); 

+0

私はそれをやっていました? (コード中の "context.load(searchResults、 'text、font');"参照)。問題は、Michael Mainer'sのような例のように、単一のものではなく、用語の配列を検索したいということから来ていると思います。別のJS関数を作成してこの配列を反復処理し、Wordで検索する関数に1つずつ送信すると、動作が終了しました。 – tioken

関連する問題