You are on page 1of 2

TIKTOK VIDEO - OUTCOME - Watch this first

Follow these steps to create a Google Apps Script that lists all unique email addresses, along
with their usernames, from your Gmail (including archived mails) into a Google Spreadsheet.

1. Create a New Google Spreadsheet:


a. Open Google Sheets (https://sheets.google.com).
b. Click on + or Blank to create a new spreadsheet.
c. Rename the spreadsheet if you wish, for better recognition.
2. Prepare the Spreadsheet:
a. Click on cell B1 and type Username. This column will hold the usernames of the
email addresses.
b. Click on cell C1 and type Email Address. This column will hold the email
addresses.
3. Open Google Apps Script Editor:
a. Click on Extensions from the menu bar.
b. Select Apps Script.
c. If this is your first time using Google Apps Script, you might need to grant it
permissions.
4. Create the Script:
a. Copy and paste the following code into the script editor:

function listMails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var start = PropertiesService.getScriptProperties().getProperty('start');
start = start ? parseInt(start) : 0;
var threads = GmailApp.search('in:all', start, 500);
var row = sheet.getLastRow() + 1;
var emailList = {};

if (start === 0) {
sheet.getRange(1, 2).setValue('Username');
sheet.getRange(1, 3).setValue('Email Address');
}

for (var i = 0; i < threads.length; i++) {


var messages = threads[i].getMessages();

for (var j = 0; j < messages.length; j++) {


var email = messages[j].getFrom();

if (!emailList[email]) {
var username = email.split('@')[0];
sheet.getRange(row, 2).setValue(username);
sheet.getRange(row, 3).setValue(email);
emailList[email] = true;
row++;
}
}
}

PropertiesService.getScriptProperties().setProperty('start', start + 500);


}

5. Save and Deploy the Script:


a. Click on File > Save to save the script.
b. Name the project for easy recognition.
6. Set Up a Trigger:
a. Click on Triggers icon (clock-like icon) on the left side of the editor.
b. Click on + Add Trigger.
c. In the function field, select listMails.
d. Choose the deployment to be Head.
e. Set event source to Time-driven.
f. Set type of time-based trigger to Minutes timer.
g. Set the timer to every N minutes (choose a value for N that makes sense for
you).
h. Click Save.

You might also like