2017-02-22 4 views
1

私はこのドキュメントにカスタムプロパティを追加する方法について助けが必要だったカップル日前にthisを作成しました。(Office用JavaScript API 1.3)カスタムプロパティGetItemOrNull

まず、Word 1701(7766.2047)を実行しています。


私は、上記のカスタムプロパティを返すメソッドがあるとしましょう。最初に、カスタムプロパティが既に作成されているかどうかを確認します。リターンはその後、単純にそれを作成して返すnullの場合、私は...簡単なgetItemOrNullObject(キー)とでこれを行うだろう

  • それエルス
  • を返し、それ

それは私が必要私の理解であります戻り値context.sync()を実行します。次に、オブジェクトに対して実際にデータがロードされますか?あまりにも多くの返すcontext.sync()呼び出し何もないですか? Wordの開始時に '未定義である 'しかし、私はそれをデバッグする場合、それは

var customDocProps = context.document.properties.customProperties; context.load(customDocProps); return context.sync().{....}


を壊す前に、それが実行されます。

Word.run(function(context) { 
 
    var customDocProps = context.document.properties.customProperties; 
 
    context.load(customDocProps); 
 
    return context.sync() 
 
    .then(function() { 
 
     var temp = customDocProps.getItemOrNullObject("X"); 
 
     return context.sync() 
 
     .then(function() { 
 
      if (!temp) { 
 
      context.document.properties.customProperties.add("X", 1234); 
 
      temp = customDocProps.getItemOrNullObject("X"); 
 
      return context.sync() 
 
       .then(function() { 
 
       return temp; 
 
       }); 
 
      } else { 
 
      return temp; 
 
      } 
 
     }); 
 
    }); 
 
});

次のコードは、私に' にReferenceError' をスローします

もう1つ質問があります。 、私は私のカスタムプロパティを更新したいと言う:

Word.run(function (context) { 
 
     context.document.properties.customProperties.add("X", 56789); 
 
     return context.sync(); 
 
    });

オーバーライド新しいものと古い値はありますか?

これを読んでいただければありがとうございます!どんな助けもありがとうございます。 乾杯!

答えて

3

ありがとうございます。

すべての* getItemOrNullObjectメソッドはすべてではありません。 JavaScriptを返すと、if(!temp)ステートメントは期待通りに機能しません。存在を検証する場合は、代わりにif(temp.isNullObject)を呼び出す必要があります。また

提案のカップル:

  1. customProperties.add()のセマンティクスがプロパティが存在しない場合、それを交換するということです。したがって、プロパティを作成または変更する場合は、存在するかどうかを確認する必要はありません。現在の値を読みたい場合は、これはあなたの2番目の質問に答えます。
  2. 単一のプロパティの読み込みに興味がある場合は、コードの簡略化された効率的な提案があります。

Word.run(function (context) { 
 
    var myProperty = context.document.properties.customProperties.getItemOrNullObject("X"); 
 
    context.load(myProperty); 
 
    return context.sync() 
 
     .then(function() { 
 
     if (myProperty.isNullObject) { 
 
      //this means the Custom Property does not exist.... 
 
      context.document.properties.customProperties.add("X", 1234); 
 
      console.log("Property Created"); 
 
      return context.sync(); 
 
     } 
 
     else 
 
      console.log("The property already exists, value:" + myProperty.value); 
 
     }) 
 
    }) 
 
    .catch(function (e) { 
 
     console.log(e.message); 
 
    })

これが混乱するようだと私たちは、ドキュメントを更新します。

ありがとうございます!

+0

Word.run()が非同期で実行されるため、戻り値を格納したい場合はコールバックを使用する必要があります。これはWord.run()で可能ですか? –

+0

プロパティ値を返す場合は、サンプルからmyProperty.valueの値を返します。サンプルを使用して –

+0

タグがnullの場合、私はいつもcontext.sync()の後で一般例外を取得します あなたが書いたようにそれはそのままですので、何が間違っているのか分かりません。新しい質問をするべきですか? http://imgur.com/a/TKujx –

関連する問題