Last active
August 29, 2015 14:10
-
-
Save kei-s/322aeb83f9284396734a to your computer and use it in GitHub Desktop.
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
function sendMail() { | |
var today = new Date(); | |
var MAIL_TO = "メールアドレス"; | |
var cycles = (function() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("サイクル"); | |
var rows = sheet.getDataRange(); | |
var numRows = rows.getNumRows(); | |
var values = rows.getValues(); | |
return values; | |
})(); | |
var logs = (function() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("購入ログ(商品データ)"); | |
var rows = sheet.getDataRange(); | |
var numRows = rows.getNumRows(); | |
var values = rows.getValues(); | |
return values; | |
})(); | |
var latestLog = function(asin) { | |
for (var i = logs.length - 1; i >= 1; i--) { | |
var log = logs[i]; | |
var log_asin = log[2]; | |
if (asin == log_asin) { | |
return log; | |
} | |
} | |
}; | |
var alerts = []; | |
for (var i = 1; i < cycles.length; i++) { | |
var cycle = cycles[i]; | |
var url = cycle[0]; | |
var asin = cycle[1]; | |
var name = cycle[2]; | |
var cycle_date = cycle[4]; | |
var latest = latestLog(asin); | |
if (!latest) { | |
continue; | |
} | |
var purchase_date = latest[0]; | |
var next_purchase_date = new Date((new Date(purchase_date)).getTime() + cycle_date * 24 * 60 * 60 * 1000); | |
if (new Date(next_purchase_date) < today) { | |
message = name + " は、前回の購入から" + cycle_date + "日以上経っています。そろそろ購入を検討してはいかがですか?\n" + url; | |
alerts.push(message); | |
} | |
} | |
var formattedToday = Utilities.formatDate(new Date(), "JST", "yyyy-MM-dd"); | |
MailApp.sendEmail(MAIL_TO, "そろそろアラート" + "(" + formattedToday + ")", | |
"お家を快適に保つには、定期的なお手入れ用具の交換が大事です。" + "\n\n" + alerts.join("\n")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment