Share via


Outlook で予定またはメッセージを作成するときに件名を取得または設定する

Office JavaScript API には、ユーザーが作成している予定またはメッセージの件名を取得および設定するための非同期メソッド (subject.getAsyncsubject.setAsync) が用意されています。 これらの非同期メソッドは、アドインを作成する場合にのみ使用できます。これらのメソッドを使用するには、Outlook がアドイン作成 フォームをアクティブ化するためにアドイン XML マニフェストを適切に設定していることを確認します。

プロパティは subject 、予定とメッセージの新規作成フォームと読み取りフォームの両方で読み取りアクセスに使用できます。 読み取りフォームで、次のように親オブジェクトから直接 プロパティにアクセスします。

Office.context.mailbox.item.subject;

ただし、新規作成フォームでは、ユーザーとアドインの両方が同時にサブジェクトを挿入または変更できるため、メソッドを getAsync 使用してサブジェクトを非同期的に取得する必要があります。

Office.context.mailbox.item.subject.getAsync(callback);

プロパティは subject 、作成フォームでのみ書き込みアクセスでき、読み取りフォームでは使用できません。

ヒント

メッセージの件名に表示されるコンテンツを読み取りモードで一時的に設定するには、 Office.context.mailbox.mailbox.item.display.subject (プレビュー) を使用します。

Office JavaScript API のほとんどの非同期メソッドとsetAsync同様に、getAsyncオプションの入力パラメーターを受け取ります。 これらの省略可能な入力パラメーターを指定する方法の詳細については、「 Office アドインでの非同期プログラミング」の「省略可能なパラメーターを非同期メソッドに渡す」を参照してください。

件名を取得する

このセクションでは、ユーザーが作成している予定またはメッセージの件名を取得して、その件名を表示するサンプル コードについて説明します。

を使用 item.subject.getAsyncするには、非同期呼び出しの状態と結果をチェックするコールバック関数を指定します。 省略可能 asyncContext なパラメーターを使用して、コールバック関数に必要な引数を指定できます。 コールバック関数から状態、結果、エラーを取得するには、コールバックの asyncResult 出力パラメーターを使用します。 非同期呼び出しが成功した場合は、 AsyncResult.value プロパティを使用して、件名をプレーン テキスト文字列として取得します。

let item;

// Confirms that the Office.js library is loaded.
Office.onReady((info) => {
    if (info.host === Office.HostType.Outlook) {
        item = Office.context.mailbox.item;
        getSubject();
    }
});

// Gets the subject of the item that the user is composing.
function getSubject() {
    item.subject.getAsync((asyncResult) => {
        if (asyncResult.status === Office.AsyncResultStatus.Failed) {
            write(asyncResult.error.message);
            return;
        }

        // Display the subject on the page.
        write(`The subject is: ${asyncResult.value}`);
    });
}


// Writes to a div with id="message" on the page.
function write(message) {
    document.getElementById("message").innerText += message; 
}

件名を設定する

このセクションでは、ユーザーが作成している予定またはメッセージの件名を設定するサンプル コードについて説明します。

を使用 item.subject.setAsyncするには、 パラメーターに最大 255 文字の文字列を data 指定します。 必要に応じて、 パラメーターにコールバック関数とコールバック関数の任意の引数を asyncContext 指定できます。 コールバックの出力パラメーターで asyncResult 、コールバックの状態、結果、およびエラー メッセージを確認します。 非同期呼び出しが成功した場合は、 setAsync 指定したサブジェクト文字列をプレーン テキストとして挿入し、その項目の既存の件名を上書きします。

let item;

// Confirms that the Office.js library is loaded.
Office.onReady((info) => {
    if (info.host === Office.HostType.Outlook) {
        item = Office.context.mailbox.item;
        setSubject();
    }
});

// Sets the subject of the item that the user is composing.
function setSubject() {
    // Customize the subject with today's date.
    const today = new Date();
    const subject = `Summary for ${today.toLocaleDateString()}`;

    item.subject.setAsync(
        subject,
        { asyncContext: { optionalVariable1: 1, optionalVariable2: 2 } },
        (asyncResult) => {
            if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                write(asyncResult.error.message);
                return;
            }

            /*
              The subject was successfully set.
              Run additional operations appropriate to your scenario and
              use the optionalVariable1 and optionalVariable2 values as needed.
            */
        });
}

// Writes to a div with id="message" on the page.
function write(message) {
    document.getElementById("message").innerText += message; 
}

関連項目