Microsoft 365 Outlook for Business: メールのカテゴリ解除

Microsoft 365 Outlook for Business: メールのカテゴリ解除

Microsoft 365 Outlook for Business: Remove Category from Email

この工程は、Outlook のメールからカテゴリを外します。一度に複数のカテゴリを解除可能です。

Basic Configs
工程名
メモ
Auto Step icon
Configs for this Auto Step
conf_OAuth2
C1: OAuth2 設定 *
conf_MessageId
C2: メール ID *
conf_Category
C3: 解除するカテゴリ(1 行に 1 つ) *

Notes

Capture

See Also

Script (click to open)
  • 次のスクリプトが記述されている XML ファイルをダウンロードできます
    • outlook-message-category-remove.xml (C) Questetra, Inc. (MIT License)
    • Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます

// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://login.microsoftonline.com/{your-tenant-id}/oauth2/v2.0/authorize
// - Token Endpoint URL: https://login.microsoftonline.com/{your-tenant-id}/oauth2/v2.0/token
// - Scope: https://graph.microsoft.com/Mail.ReadWrite offline_access
// - Consumer Key: (Get by Microsoft Azure Active Directory)
// - Consumer Secret: (Get by Microsoft Azure Active Directory)

function main(){
    //// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
    const auth = configs.getObject('conf_OAuth2');
    const messageId = retrieveMessageId();
    const categoriesToRemove = retrieveCategories();

    //// == 演算 / Calculating ==
    const currentCategories = getMessageCategories(auth, messageId);
    const newCategories = currentCategories.filter(category => !categoriesToRemove.includes(category));
    if (newCategories.length < currentCategories.length) { // 差分がある場合のみ、更新
        updateMessageCategories(auth, messageId, newCategories);
    }
}

/**
 * config からメール ID を読み出す
 * 空の場合はエラー
 * @returns {String} メール ID
 */
function retrieveMessageId() {
    const messageId = engine.findData(configs.getObject('conf_MessageId'));
    if (messageId === null) {
        throw 'Message ID is blank.';
    }
    return messageId;
}

/**
 * ワークフローデータからカテゴリの一覧を読み出す
 * 末尾の改行のみ無視し、他の空行はエラー
 * @returns {Array<String>} categories カテゴリ一覧
 */
function retrieveCategories() {
    let categoriesStr = configs.get('conf_Category');
    if (categoriesStr.endsWith('\n')) { // 末尾に空行がある場合、削除
        categoriesStr = categoriesStr.slice(0, -1);
    }
    const categories = categoriesStr.split('\n');
    if (categories.some(line => line === '')) {
        throw 'Empty lines are not allowed for category.';
    }
    return categories;
}

/**
 * Microsoft Graph API にメール取得の GET リクエストを送信し、カテゴリ一覧を取得する
 * @param {AuthSettingWrapper} auth 認証設定
 * @param {String} messageId メール ID
 * @returns {Array<String>} categories メールに設定されたカテゴリ一覧
 */
function getMessageCategories(auth, messageId) {
    const response = httpClient.begin()
        .authSetting(auth)
        .queryParam('$select', 'categories')
        .get(`https://graph.microsoft.com/v1.0/me/messages/${encodeURI(messageId)}`);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw `Failed to get message. status: ${status}`;
    }
    return JSON.parse(responseStr).categories;
}

/**
 * Microsoft Graph API にメール更新の PATCH リクエストを送信し、カテゴリ一覧を置き換える
 * @param {AuthSettingWrapper} auth 認証設定
 * @param {String} messageId メール ID
 * @param {Array<String>} categories カテゴリ一覧
 */
function updateMessageCategories(auth, messageId, categories) {
    const body = {categories};
    const response = httpClient.begin()
        .authSetting(auth)
        .body(JSON.stringify(body), 'application/json; charset=UTF-8')
        .patch(`https://graph.microsoft.com/v1.0/me/messages/${encodeURI(messageId)}`);
    const status = response.getStatusCode();
    if (status !== 200) {
        engine.log(response.getResponseAsString());
        throw `Failed to update message. status: ${status}`;
    }
}

    
上部へスクロール

Questetra Supportをもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む