Skip to content

Instantly share code, notes, and snippets.

@Kazuki-tam
Last active February 27, 2022 13:27
Show Gist options
  • Save Kazuki-tam/b7d6fd8be32af0a581bb7a989248728e to your computer and use it in GitHub Desktop.
Save Kazuki-tam/b7d6fd8be32af0a581bb7a989248728e to your computer and use it in GitHub Desktop.
gas-spreadsheet-mail-01
function main(){}(()=>{"use strict";var e,t,a,r={598:(e,t)=>{t.__esModule=!0,t.sendEmailToAll=void 0,t.sendEmailToAll=function(e,t,a,r){var n=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email Content"),i=SpreadsheetApp.getUi();if(n){for(var l="",o="",s="",c="",d=n.getLastRow()-1,u=0;u<d;u++){var p=n.getRange(2+u,1).getValue(),g=n.getRange(2+u,2).getValue();"CC"===p&&(s=g),"BCC"===p&&(c=g),"Subject"===p&&(l=g),"Body"===p&&(o=g.replace("{COMPANY}",t).replace("{DEPARTMENT}",a).replace("{PIC}",r))}var m={cc:s,bcc:c};GmailApp.sendEmail(e,l,o,m),i.alert("📤 Sent email","It's done!",i.ButtonSet.OK)}else i.alert("🚨 The sheet name may be incorrect. It should be Email Content.")}},690:(e,t)=>{t.__esModule=!0,t.checkDuplicate=void 0,t.checkDuplicate=function(e){return e.filter((function(e,t,a){return a.indexOf(e)===t&&t!==a.lastIndexOf(e)}))}},506:(e,t)=>{t.__esModule=!0,t.validateEmail=void 0,t.validateEmail=function(e){return/^[A-Za-z0-9]{1}[A-Za-z0-9_.-]*@{1}[A-Za-z0-9_.-]+.[A-Za-z0-9]+$/.test(e)}}},n={};function i(e){var t=n[e];if(void 0!==t)return t.exports;var a=n[e]={exports:{}};return r[e](a,a.exports,i),a.exports}i.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),e=i(598),t=i(506),a=i(690),i.g.main=function(){var r=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email List"),n=SpreadsheetApp.getUi(),i=Browser.msgBox("Confirmation","Are you sure to send email?👀",Browser.Buttons.YES_NO);if(r&&"yes"==i){for(var l=2,o=3,s=4,c=5,d=r.getLastColumn(),u=1;u<=d;u++){var p=r.getRange(2,u).getValue();"Company"===p&&(l=u),"Department"===p&&(o=u),"Person in charge"===p&&(s=u),"Email address"===p&&(c=u)}var g=r.getLastRow()-2,m=[],h=[],f=[];for(u=0;u<g;u++){var v=r.getRange(3+u,l).getValue(),A=r.getRange(3+u,o).getValue(),E=r.getRange(3+u,s).getValue(),y=r.getRange(3+u,c).getValue(),S={company:v,department:A,pic:E,address:y};(0,t.validateEmail)(y)?(m.push(S),h.push(y)):""===y||(0,t.validateEmail)(y)||f.push(y)}if(f.length>0)return n.alert("🚨 Invalid email address: "+f.join(", "));var C=(0,a.checkDuplicate)(h);if(C.length>0)return n.alert("🚨 Duplicate email address: "+C.join(", "));m.forEach((function(t){""!=t.address&&(0,e.sendEmailToAll)(t.address,t.company,t.department,t.pic)}))}else"no"==i?n.alert("Send canceled!"):n.alert("🚨 The sheet name may be incorrect. It should be Email List.")}})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment