
Gmail: メールのラベルを解除
Gmail: Remove Label from Email Message
Gmail のメールからラベルを外します。一度に複数のラベルの解除が可能です。複数解除する場合、データ項目では1行につき1つずつラベルを書くようにしてください。
Basic Configs
- 工程名
- メモ
Configs for this Auto Step
- conf_auth
- C1: OAuth2 設定 *
- conf_messageId
- C2: メール ID *
- conf_label
- C3: 解除するラベル *
Notes
- 本モデリング要素は Google Workspace アカウント用です。一般ユーザー向けアカウント(gmail.com)での利用はできません。
- 事前準備として、Google Workspace の管理者が対象の OAuth アプリを信頼できるアプリとして設定済みである必要があります。この設定がなされていない状態での利用はできません。
- 本モデリング要素が使用する OAuth アプリのクライアント ID は 13039123046-t87nmrj499ffoa58asehks3asajvgqnh.apps.googleusercontent.com です。
- メール ID は Gmail 上では確認できません。「開始: Gmail: メール受信時」で取得したメール ID を使用してください。
- ユーザーが作成したラベルだけでなく、INBOX、UNREAD、STARRED などのシステムラベルも指定できます。システムラベルの一覧は Gmail API のガイドに掲載されています。
Capture

See also
Script (click to open)
- 次のスクリプトが記述されている XML ファイルをダウンロードできます
- gmail-message-label-remove.xml (C) Questetra, Inc. (MIT License)
- Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます
// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force
// - Token Endpoint URL: https://oauth2.googleapis.com/token
// - Scope: https://www.googleapis.com/auth/gmail.modify
// - Consumer Key: (Get by Google Developer Console)
// - Consumer Secret: (Get by Google Developer Console)
function main(){
//// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
const auth = configs.getObject("conf_auth");
const messageId = engine.findData( configs.getObject("conf_messageId") );
const labels = retrieveLabels();
//// == 演算 / Calculating ==
const apiUri = determineApiUri( messageId );
const labelIds = getLabelIds( auth, labels );
removeLabels( apiUri, auth, labelIds );
}
/**
* ワークフローデータからラベルを読み出す
* ラベルの設定がない場合はエラー
* @return {Array<String>} labels ラベル一覧
*/
function retrieveLabels() {
const labels = configs.get('conf_label').split('\n')
.map(label => label.trim())
.filter(label => label !== '');
// データ項目に改行のみ等、ラベルの設定がない場合はエラー
if (labels.length === 0) {
throw "No labels to remove are set.";
}
return labels;
}
/**
* Gmail のメール編集の URI を決定する
* メール ID が空であればエラーとする
* @param {String} messageId メール ID
* @return {String} apiUri API の URI
*/
function determineApiUri( messageId ) {
if ( messageId === "" || messageId === null ) {
throw "Message ID is empty.";
}
const apiUri = `https://gmail.googleapis.com/gmail/v1/users/me/messages/${encodeURI(messageId)}/modify`;
return apiUri;
}
/**
* ラベルからラベル ID を取得する
* @param {String} auth 認証設定
* @param {Array<String>} labels ラベル一覧
* @return {Array<String>} ラベル ID の一覧
*/
function getLabelIds( auth, labels ) {
const response = httpClient.begin()
.authSetting(auth)
.get('https://gmail.googleapis.com/gmail/v1/users/me/labels');
const responseJson = response.getResponseAsString();
const status = response.getStatusCode();
if (status >= 300) {
const accessLog = `--- users.labels.list --- ${status}\n${responseJson}\n`;
engine.log(accessLog);
throw `Failed to get labels. status: ${status}`;
}
const json = JSON.parse(responseJson);
let found = json.labels
.filter(label => labels.includes(label.name));
const foundLabels = found.map(label => label.name);
const notFoundLabels = labels.filter(label => !foundLabels.includes(label));
if (notFoundLabels.length > 0) {
const accessLog = `--- users.labels.list --- ${status}\n${responseJson}\n`;
engine.log(accessLog);
// id が見つからない label がある場合、エラーをスロー
throw `label ids of ${notFoundLabels.join(', ')} not found`;
}
return found.map(label => label.id);
}
/**
* Gmail REST API にメール編集の POST リクエストを送信し、ラベルを除去する
* @param {String} apiUri API の URI
* @param {String} auth 認証設定名
* @param {Array<String>} labelIds ラベル ID の一覧
*/
function removeLabels( apiUri, auth, labelIds ) {
const body = {
"removeLabelIds": labelIds
};
const response = httpClient.begin()
.authSetting( auth )
.body(JSON.stringify(body), "application/json; charset=UTF-8")
.post( apiUri );
// when error thrown
const responseJson = response.getResponseAsString();
const status = response.getStatusCode();
if (status >= 300) {
const accessLog = `---POST request--- ${status}\n${responseJson}\n`;
engine.log( accessLog );
throw `Failed to remove labels. status: ${status}`;
}
}
