Last active
August 29, 2015 13:56
-
-
Save wingyplus/8828179 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
var sheet = SpreadsheetApp.getActiveSheet(); | |
function Account(_id, _name, _email, _isSend) { | |
this.id = _id; | |
this.name = _name; | |
this.email = _email; | |
var isSend = _isSend; // private scope | |
this.isSend = function() { return isSend === "SENDED" ? true : false; }; | |
this.isNotSend = function() { return isSend !== "SENDED" ? true : false; }; | |
} | |
var AccountFactory = { | |
createAccount: function(id, account) { | |
return new Account(id, account[0], account[1], account[2]); | |
} | |
}; | |
function getEmails() { | |
var startRow = 2, | |
numRows = 2, | |
dataRange = sheet.getRange(startRow, 1, numRows, 3), | |
data = dataRange.getValues(); | |
var i = startRow; | |
return data.map(function(account) { return AccountFactory.createAccount(i++, account); }); | |
} | |
function sendEmails(accounts) { | |
accounts.forEach(function(account) { | |
if (account.isNotSend()) { | |
MailApp.sendEmail(account.email, "ทดลองส่งผ่าน AppScript", "สวัสดีครัช " + account.name + " มีความพยายาม แต่ก็ยังอ่อนหัด!!!"); | |
sheet.getRange(account.id, 3).setValue("SENDED"); | |
SpreadsheetApp.flush(); | |
} | |
}); | |
} | |
function main() { | |
var accounts = getEmails(); | |
sendEmails(accounts); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment