2016-11-23 40 views
3

特定のパラメータセットに対してGmailにネイティブなフィルタを作成します。Google Appsスクリプトを使用してGmailにネイティブフィルタを作成します。

基本的には、Googleの別名機能を多く使用しています(電子メールの後に+記号が付きます)。私は、 "To"行を読み込んで "+"を探すフィルタを作成するプロセスを自動化したいと思います。 "+"が見つかった場合は、 "+"の後ろのラベルを作成します。次に、専用の/ネイティブフィルタを作成します。受信トレイをスキップし、 "+"の後ろのラベルを適用します。

Gmailのスクリプトを見て、ネイティブフィルタを作成する方法が見つかりませんでした。私はこの機能が実装されたばかりであることを理解しています。

function getTo() { 
    // Log the To line of up to the first 50 emails in your Inbox 
    var email = Session.getActiveUser().getEmail(); 
    Logger.log(email); 
    var threads = GmailApp.getInboxThreads(0, 50); 
    for (var i = 0; i < threads.length; i++) { 
    var messages = threads[i].getMessages(); 
    for (var j = 0; j < messages.length; j++) { 
     Logger.log(messages[j].getTo()||email); 
    } 
    } 
} 

助けがあれば助かります。

ソリューション:

// Creates a filter to put all email from ${toAddress} into 
// Gmail label ${labelName} 
function createToFilter(toAddress, labelName) { 

// Lists all the filters for the user running the script, 'me' 
var labels = Gmail.Users.Settings.Filters.list('me') 

// Search through the existing filters for ${toAddress} 
var label = true 
labels.filter.forEach(function(l) { 
    if (l.criteria.to === toAddress) { 
     label = null 
    } 
}) 

// If the filter does exist, return 
if (label === null) return 
else { 
// Create the new label 
GmailApp.createLabel(labelName) 

// Lists all the labels for the user running the script, 'me' 
var labelids = Gmail.Users.Labels.list('me') 

// Search through the existing labels for ${labelName} 
// this operation is still needed to get the label ID 
var labelid = false 
labelids.labels.forEach(function(a) { 
    if (a.name === labelName) { 
     labelid = a 
    } 
}) 
Logger.log(labelid); 
Logger.log(labelid.id); 
// Create a new filter object (really just POD) 
var filter = Gmail.newFilter() 

// Make the filter activate when the to address is ${toAddress} 
filter.criteria = Gmail.newFilterCriteria() 
filter.criteria.to = toAddress 

// Make the filter remove the label id of ${"INBOX"} 
filter.action = Gmail.newFilterAction() 
filter.action.removeLabelIds = ["INBOX"]; 
// Make the filter apply the label id of ${labelName} 
filter.action.addLabelIds = [labelid.id]; 

// Add the filter to the user's ('me') settings 
Gmail.Users.Settings.Filters.create(filter, 'me') 
} 

}

おかげで、エンダー

+0

今すぐあなたの受信トレイを実行しますか?メールが届くのは?これまでにどのようなコードを試しましたか? – Brian

+0

だから私はそれがまだ頻繁にフィルタリングされていない新しい電子メールを探すために実行したい。ネイティブフィルタは常に実行されている必要があります。高度な機能でいくつかのフィルタ設定が見つかりました:https://developers.google.com/gmail/api/v1/reference/users/settings/filters –

+0

私は.getToをテストしています。私は私の電子メールとチェックする電子メールの違いを排除しようとしています。違いはラベルになります。それから私はラベルを持っていれば、私はフィルタを作るだろう。私はまだフィルタを作ろうとしていません。 –

答えて

5

機能が存在するようだが、完全なGmailのAPIを有効にする必要があります。

Appsスクリプトの場合は、https://developers.google.com/apps-script/guides/services/advancedの指示に従って高度なGoogleサービスを有効にします。 Gmail.Users.Settings.Filters.list('me')の呼び出しを初めて試してみたところ、認証エラーが発生しました。デベロッパーコンソールでGmail APIを有効にするためのリンクが表示されていましたが、これはちょうどスイッチを反転してしまいました。あなたはこれが有効になったら

、あなたが

// 
// Creates a filter to put all email from ${toAddress} into 
// Gmail label ${labelName} 
// 
function createToFilter (toAddress, labelName) { 

    // Lists all the labels for the user running the script, 'me' 
    var labels = Gmail.Users.Labels.list('me') 

    // Search through the existing labels for ${labelName} 
    var label = null 
    labels.labels.forEach(function (l) { 
    if (l.name === labelName) { 
     label = l 
    } 
    }) 

    // If the label doesn't exist, return 
    if (label === null) return 

    // Create a new filter object (really just POD) 
    var filter = Gmail.newFilter() 

    // Make the filter activate when the to address is ${toAddress} 
    filter.criteria = Gmail.newFilterCriteria() 
    filter.criteria.to = toAddress 

    // Make the filter apply the label id of ${labelName} 
    filter.action = Gmail.newFilterAction() 
    filter.action.addLabelIds = [label.id] 

    // Add the filter to the user's ('me') settings 
    Gmail.Users.Settings.Filters.create(filter, 'me') 

} 


function main() { 
    createToFilter('[email protected]', 'Aliases/foo') 
} 

のように、フィルタを作成するためのスクリプトを書くことができ、私はGoogleがないので、さかのぼって自動的にメッセージにラベルを付けるフィルタを作成する方法を見つけ出すことができませんでした彼らのAPIにそれを公開しているようです。

+0

これは非常に便利です!!!!どうもありがとうございました!私はこの種の質の高い助けを得たことはありません。私は投票を行うだろうが、私は担当者を持っていない。 –

+0

私は自分の受信箱にある.toをすべて電子メールと比較して、ラベル部分だけを得ることを計画していました。次にラベルがすでに作成されているかどうか、またはフィルタがすでに存在するかどうかを確認します。その後、 "+"の後ろにあるラベルでフィルタを生成します。フィルタを編集すると、それが電子メール全体に適用されるオプションがあります。これは、ラベルの作成時にすべてを適切なラベルに移動する方法が見つからない場合に行う必要があります。 –

+0

私はそれを実装しようとしているコードでいくつかの時間を費やしましたが、私はあなたがIFステートメントで何をしているのか本当に理解していません。私はあなたがフィルタが既に作成されているかどうかを確認する確認が表示されますが、私はどのようにわからないのですか?また、私が実行したすべてのテストはラベルのためnullを返したので、私は何か間違っているかどうかはわかりません。コメントは、これが私の初めてのJavaスクリプトとGoogleスクリプトを使用するのに役立ちます。 –

関連する問題