{"id":221,"date":"2019-02-22T14:20:28","date_gmt":"2019-02-22T08:50:28","guid":{"rendered":"http:\/\/www.jawed.in\/blog\/?p=221"},"modified":"2021-08-20T19:09:24","modified_gmt":"2021-08-20T13:39:24","slug":"google-sheets-mail-merge-advanced","status":"publish","type":"post","link":"https:\/\/www.jawed.in\/blog\/google-sheets-mail-merge-advanced\/","title":{"rendered":"Google Sheets Mail Merge : Advanced"},"content":{"rendered":"\n<div class=\"wp-block-cover has-background-dim\" style=\"background-image:url(https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Mail-Merge-header-Image.png)\"><div class=\"wp-block-cover__inner-container is-layout-flow wp-block-cover-is-layout-flow\">\n<p class=\"has-text-align-center has-large-font-size\">Google Script for Mail Merge<\/p>\n<\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Introduction<\/h4>\n\n\n\n<p>Last year, during\nChristmas, I was looking out for an easy to use script to send out\nPersonalised e-Cards using the power of Google.  But unfortunately I\ncould not find a readily available solution. A few solutions were\navailable but they were not free or not ready-to-use.<\/p>\n\n\n\n<p>So I just looked at\nthe Google Help section where I found an example which was meeting\naround 90% of the requirement  and then a little bit of browsing\nStackoverflow and I was good to go.<\/p>\n\n\n\n<p>I am sharing this Ready to Use Google Sheet with the script on web with the hope that it helps someone save time while doing Mail Merge.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What this script does?<\/h4>\n\n\n\n<p>This script will\nread the email ids and the form of Address (Mr.\/Ms Firstname etc) and\nthen create a personalised email with the predefined subject and body\nand then insert an image(a Christmas card in my case) and then send\nout the email.  The script picks the image to be embedded from the\nuser\u2019s Google Drive. After the mail is sent successfully, it will\nupdate the status column in the sheet with the words \u2018EMAIL_SENT\u2019.\nThis is important as Google limits the number of email which free\naccounts can send(using scripts) to 100 per day. \n<\/p>\n\n\n\n<p>So if you  have 300 email ids in your list, then you have to break it into 3 parts. The code is written in such a way that based on the Status it will skip all those email ids which have already been sent the Greeting Email. So effectively we can schedule this script to run at a fixed time everyday from 22<sup>nd<\/sup> Dec to 24<sup>th<\/sup> Dec. Everyday it will fire 100 emails and then stop. It will continue this way till the 3<sup>rd<\/sup> day(i.e., 24<sup>th<\/sup> Dec) by when it would have sent out emails to your complete list.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Requirements for this mail merge to function<\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li>An existing Gmail id<\/li><li>Google sheet with your contact\u2019s email id and Form of address(Mr. Kent or Mr. Clarke Kent)<\/li><li>Greeting Image(upload it to your Google Drive)<\/li><li>Content of the email(subject and body)<\/li><li><\/li><\/ol>\n\n\n\n<p>You can download the ready to use Google Sheet with the code in it by clicking on the link below. Please login to your personal Gmail ID before clicking on the link below.<\/p>\n\n\n\n<p><a href=\"https:\/\/docs.google.com\/spreadsheets\/d\/14zdcYdM7iBF_YTV7n6NGj7CTYuCMrcTBsa1yqYL3amU\/edit?usp=sharing\">https:\/\/docs.google.com\/spreadsheets\/d\/14zdcYdM7iBF_YTV7n6NGj7CTYuCMrcTBsa1yqYL3amU\/edit?usp=sharing<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Steps to copy the template sheet (along with code ) to your Google Drive<\/h4>\n\n\n\n<p>First you have to Login to your Gmail ID and then click on the link given above.<\/p>\n\n\n\n<p>Once the sheet has opened, please click on <strong>File&#8211;&gt; Make a copy.. <\/strong>You won&#8217;t be able to make a copy if you are not logged in to your personal Gmail ID.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"543\" height=\"385\" src=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Copying-the-sheet-to-your-own-drive.png\" alt=\"Copy the Google Sheet to your own Google Drive\" class=\"wp-image-217\" srcset=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Copying-the-sheet-to-your-own-drive.png 543w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Copying-the-sheet-to-your-own-drive-300x213.png 300w\" sizes=\"auto, (max-width: 543px) 100vw, 543px\" \/><figcaption>Copy the Google Sheet to your own Google Drive<\/figcaption><\/figure><\/div>\n\n\n\n<p>Then  you need to open the Script Editor by clicking on <strong>Tools &#8211;&gt; Script Editor<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"437\" height=\"435\" src=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/How-to-open-the-Script-Editor-1.png\" alt=\"How to open the script editor in Google Sheets\" class=\"wp-image-223\" srcset=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/How-to-open-the-Script-Editor-1.png 437w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/How-to-open-the-Script-Editor-1-300x300.png 300w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/How-to-open-the-Script-Editor-1-150x150.png 150w\" sizes=\"auto, (max-width: 437px) 100vw, 437px\" \/><figcaption>Opening the Script Editor in Google Sheets<\/figcaption><\/figure><\/div>\n\n\n\n<p>Here you need to find the ID of this particular  Google Sheet. For this we have to give a few authorizations first. <\/p>\n\n\n\n<p>Run the function called &#8216;GetID&#8217;.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"445\" height=\"298\" src=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/how-to-know-the-sheet-ID1.png\" alt=\"\" class=\"wp-image-219\" srcset=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/how-to-know-the-sheet-ID1.png 445w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/how-to-know-the-sheet-ID1-300x201.png 300w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><figcaption>Click on the Function getId<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"395\" height=\"208\" src=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/how-to-know-the-sheet-ID2.png\" alt=\"\" class=\"wp-image-218\" srcset=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/how-to-know-the-sheet-ID2.png 395w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/how-to-know-the-sheet-ID2-300x158.png 300w\" sizes=\"auto, (max-width: 395px) 100vw, 395px\" \/><figcaption>Now click on Run to run the function getId<\/figcaption><\/figure><\/div>\n\n\n\n<p>At this stage you will be prompted to Authorize the script.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"515\" height=\"226\" src=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation1-1.png\" alt=\"\" class=\"wp-image-224\" srcset=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation1-1.png 515w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation1-1-300x132.png 300w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><figcaption>Click on the button named Review Permissions<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"478\" src=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation2-1.png\" alt=\"\" class=\"wp-image-225\" srcset=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation2-1.png 570w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation2-1-300x252.png 300w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><figcaption>Your own email id will appear here. Please click on the email id.<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"582\" height=\"541\" src=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation3-1.png\" alt=\"\" class=\"wp-image-226\" srcset=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation3-1.png 582w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation3-1-300x279.png 300w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><figcaption>You might also get a browser prompt informing you that the script has not been verified by Google. Which is true, but since you have created this code you can safely go ahead and Click on Go to &lt;scriptname&gt;<br><br><\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"502\" height=\"573\" src=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation4-1.png\" alt=\"\" class=\"wp-image-227\" srcset=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation4-1.png 502w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Script-Authorisation4-1-263x300.png 263w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><figcaption>Click on Allow<\/figcaption><\/figure><\/div>\n\n\n\n<p>After you have given the authorisation, please run the GetID script again and then &lt;IMPORTANT&gt;go to the Browser TAB where you have the Google Sheet Open. &lt;\/IMPORTANT&gt;In this sheet, a popup will be generated with the Sheet ID.<br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"521\" src=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Sheet-ID.png\" alt=\"\" class=\"wp-image-216\" srcset=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Sheet-ID.png 736w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/Google-Sheet-ID-300x212.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><figcaption>This long string is the Unique ID for this Google Sheet. <\/figcaption><\/figure><\/div>\n\n\n\n<p>Please copy this string and paste it into the Script at the top where its mentioned.<\/p>\n\n\n\n<p>To insert the image into the email using the script, you will also need to find the Image ID of that image. It can be done by opening the image in Google Drive and clicking on the Share link. In the next screen which opens, please copy the string between the slashes after \u2018d\u2019 and before \u2018view\u2019.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"473\" height=\"394\" src=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/how-to-get-the-image-ID.png\" alt=\"\" class=\"wp-image-220\" srcset=\"https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/how-to-get-the-image-ID.png 473w, https:\/\/www.jawed.in\/blog\/wp-content\/uploads\/2019\/02\/how-to-get-the-image-ID-300x250.png 300w\" sizes=\"auto, (max-width: 473px) 100vw, 473px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Pre-flight Checklist<\/h4>\n\n\n\n<p>Check that you have changed the following things in the script.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Google Sheet ID<\/li><li>Google Drive Image ID<\/li><li>Your name in the from email id<\/li><li>Your email id<\/li><li>Your name in the body of the greeting email<\/li><li>  <\/li><\/ul>\n\n\n\n<p>Once you are done you can check by entering any of  your own alternative email id to the Google Sheet and running the SendGreetings script in the same way that you ran the GetID script. If it works properly then you can paste the rest of the email ids to the Sheet and run the script. If you have more than 100 contact names then you can run the script once every day till you are done or you can even schedule it using Googe Script Triggers.<\/p>\n\n\n\n<p>I am also sharing the Google Script(see below). But you don&#8217;t need to download it separately if you  have already made a copy of the Google Sheet to your Google Drive. As the code is included within the Google Sheet which you  can access via Script Editor.<br><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**   Sends e-greetings along with embedded image to the contact list  **\/\n\n\/**   By Jawed Ahmed  **\/\n\n\/\/ key: 1mEypO1c3xxxxxxxxxxxxxxxxXl1H4\n\n\n\nfunction SendGreetings() {  \n  \/\/upload the greeting card image to your google drive and then enter the image id in the line below\nvar GreetingBlob =   DriveApp.getFileById('ENTER THE IMAGE ID HERE');\n\n\/\/use the function getID to find out the google sheet ID of your google Sheet. It *NEEDS* to be pasted below\n  var spreadsheet = SpreadsheetApp.openById('Enter Google Sheet ID here');\n  var sheet = spreadsheet.getSheets()&#91;0];\n  var startRow = 2;                            \/\/ First row of data contains header so start from the second row\n  var numRows = sheet.getLastRow() - 1;  \/\/ Number of rows to process\n  var dailylimit = 0; \/\/counter to check how many mails have been sent in this process till now\n  var lastColumn = sheet.getLastColumn();      \/\/ Last column\n  var dataRange = sheet.getRange(startRow, 1, numRows, lastColumn) \/\/ Fetch the data range of the active sheet\nvar data = dataRange.getValues(); \/\/ Fetch values for each row in the range\nvar EMAIL_SENT = \"EMAIL SENT\"; \n  for (var i = 0; i &lt; data.length; ++i) {\n    var row = data&#91;i];  \n    \/\/ Assign each row a variable\n    var contactName = row&#91;3];                \/\/ Col D: Contact name or address\n    var contactEmail = row&#91;4];               \/\/ Col E: Contact email\n    var emailStatus = row&#91;lastColumn - 1]; \/\/ Col E: Email Status\n  \n   if (emailStatus !== EMAIL_SENT &amp;&amp; contactEmail &amp;&amp; dailylimit&lt;100 ) {\n    \n     \n          var emailBody =  '&lt;p&gt;Dear ' + contactName + ',&lt;p&gt;';\n          emailBody += '&lt;p&gt;Wishing you and your family a great new year 2019.May all your wishes be fulfilled and may you achieve all that you are wishing for !&lt;p&gt;';\n          emailBody += '&lt;br&gt;Warm regards&lt;br&gt;&lt;YOUR OWN NAME&gt;&lt;br&gt;';  \/\/Please mention your name here\n     emailBody += '&lt;img src=cid:GreetingImage&gt;';\n          emailBody += '&lt;p&gt;&lt;email signature, name, contact number, place name etc&gt;&lt;\/p&gt;';\n     MailApp.sendEmail({ to: contactEmail, \/\/ recipient\n                              subject: \"Seasons Greetings!\", \/\/ This is the email subject\n                              htmlBody: emailBody, \/\/ body\n                        name: 'YOUR NAME',    \/\/ Enter your name\n                        replyTo: 'YOUR EMAIL ID',  \/\/ Enter your email\n                        inlineImages: {GreetingImage: GreetingBlob}}\n                              );\n               sheet.getRange(startRow + i, lastColumn).setValue(EMAIL_SENT); \/\/ Update the last column with \"EMAIL_SENT\"\n               SpreadsheetApp.flush(); \/\/ Make sure the last cell is updated right away\n     Utilities.sleep(200);\n     \n     dailylimit = dailylimit + 1;\n     \n        }\n    \n  }\n}\n\n\nfunction getId() {\n  Browser.msgBox('Spreadsheet key: ' + SpreadsheetApp.getActiveSpreadsheet().getId());\n}<\/code><\/pre>\nTags: <a href=\"\/blog\/tag\/google-sheet\/\">Google Sheet<\/a>, <a href=\"\/blog\/tag\/greeting-emails\/\">Greeting emails<\/a>, <a href=\"\/blog\/tag\/mail-merge\/\">Mail Merge<\/a><br>","protected":false},"excerpt":{"rendered":"<p>Introduction Last year, during Christmas, I was looking out for an easy to use script to send out Personalised e-Cards using the power of Google. But unfortunately I could not find a readily available solution. A few solutions were available but they were not free or not ready-to-use. So I just looked at the Google [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":219,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[16,17,15],"class_list":["post-221","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","tag-google-sheet","tag-greeting-emails","tag-mail-merge"],"_links":{"self":[{"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/posts\/221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":6,"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"predecessor-version":[{"id":291,"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions\/291"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/media\/219"}],"wp:attachment":[{"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jawed.in\/blog\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}