Skip to content

Instantly share code, notes, and snippets.

@wingyplus
Last active August 29, 2015 13:56
Show Gist options
  • Save wingyplus/8828179 to your computer and use it in GitHub Desktop.
Save wingyplus/8828179 to your computer and use it in GitHub Desktop.
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