Last active
February 25, 2019 13:50
-
-
Save noblejasper/175f38e65b2c9a3669c4e97313e10645 to your computer and use it in GitHub Desktop.
Gmailの添付CSVファイルをスプレッドシートに自動で入れたい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var GMAIL_SEARCH_QUERY = "{Gmailの検索キーワード}"; | |
var ATTACHMENT_NAME = "{CSVのファイル名}"; | |
function getNewestCSVFromGmailAttachment() { | |
// 最新5件まで検索する | |
var thread = GmailApp.search(GMAIL_SEARCH_QUERY, 0, 5); | |
var messagesArray = GmailApp.getMessagesForThreads(thread); | |
// 中でも最新の1件の該当ファイルがついているメールから添付ファイルを変数にいれる | |
var newestAttachment, newestAttachmentDate; | |
for (var i in messagesArray) { | |
var messages = messagesArray[i]; | |
for (var mi in messages) { | |
var m = messages[mi]; | |
var attachments = m.getAttachments(); | |
for (var ai in attachments) { | |
var a = attachments[ai]; | |
var dt = m.getDate(); | |
if (a.getName() != ATTACHMENT_NAME) { | |
continue; | |
} | |
// 最新なら入れ直すのだよ | |
if (!newestAttachmentDate || newestAttachmentDate < dt) { | |
newestAttachment = a; | |
newestAttachmentDate = dt; | |
} | |
} | |
} | |
} | |
if (!newestAttachment) { | |
return null; | |
} | |
// CSVを配列にして返す | |
return Utilities.parseCsv(newestAttachment.copyBlob().getDataAsString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment