Write a Google Apps Script to populate a Google Spreadsheet with a list of unique email senders to my Gmail account
Assistant
You can use the Gmail service in Google Apps Script to retrieve the unique email senders in your Gmail account and populate them in a Google Spreadsheet. Here's a sample script to do that:
javascript
function getEmailSenders() {
// Get the Gmail threads in the inbox
var threads = GmailApp.getInboxThreads();
// Create an empty array to store the unique senders
var senders = [];
// Loop through the threads and add the sender to the array if it's unique
for (var i = 0; i < threads.length; i++) {
var sender = threads[i].getMessages()[0].getFrom();
if (senders.indexOf(sender) === -1) {
senders.push(sender);
}
}
// Get the active spreadsheet and insert the senders in a new sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.insertSheet();
sheet.getRange(1, 1, senders.length, 1).setValues(senders.map(function(sender) { return [sender]; }));
}
To use this script, follow these steps:
Open a new Google Spreadsheet
Go to Tools > Script editor
Paste the script into the editor and save it
Run the script by clicking the "Run" button or by pressing Ctrl + Enter
Grant the necessary permissions when prompted
Check the spreadsheet to see the list of unique email senders in your inbox.
User
This script will run too long and will be stopped by the server. Please make the script able to run for a fixed time and resume where it left off when run again. Feel free to use a page of the spreadsheet to store state from the script.
Assistant
To make the script able to run for a fixed time and resume where it left off when run again, you can use the PropertiesService to store the state of the script and update it on each run. Here's an updated version of the script that does that:
javascript
User
The script should check the time after processing each thread, not just after processing all threads.
Assistant
To check the elapsed time after processing each thread, you can move the time check inside the loop. Here's an updated version of the script that does that:
javascript
User
Now create another function to schedule getEmailSenders to be run at a configurable rate expressed in hours.
Assistant
To schedule the getEmailSenders function to run at a configurable rate expressed in hours, you can use the Triggers service in Google Apps Script. Here's an updated version of the script that includes a function:
function getEmailSenders() {
var startTime = new Date().getTime(); // get the start time in milliseconds
var maxTime = 30000; // set the maximum time in milliseconds (30 seconds)
// Get the script properties to determine where to resume
var scriptProperties = PropertiesService.getScriptProperties();
var lastThreadId = scriptProperties.getProperty('lastThreadId') || '0';
// Get the Gmail threads in the inbox, starting from the last processed thread
var threads = GmailApp.search('in:inbox newer_than:7d after:' + lastThreadId);
// Create an empty array to store the unique senders
var senders = [];
// Loop through the threads and add the sender to the array if it's unique
for (var i = 0; i < threads.length; i++) {
var sender = threads[i].getMessages()[0].getFrom();
if (senders.indexOf(sender) === -1) {
senders.push(sender);
}
// Check the elapsed time and exit the loop if the maximum time has been reached
var currentTime = new Date().getTime();
if (currentTime - startTime >= maxTime) {
scriptProperties.setProperty('lastThreadId', threads[i].getId()); // store the last processed thread ID
return;
}
}
// Get the active spreadsheet and insert the senders in a new sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.insertSheet();
sheet.getRange(1, 1, senders.length, 1).setValues(senders.map(function(sender) { return [sender]; }));
// Reset the last processed thread ID if all threads have been processed
scriptProperties.deleteProperty('lastThreadId');
}
This script sets a maximum time of 30 seconds and uses the PropertiesService to store the ID of the last processed thread. On each run, the script searches for threads newer than the last processed thread and resumes from there. If the maximum time is reached, the script exits the loop and stores the ID of the last processed thread, so it can resume from there on the next run. If all threads have been processed, the script resets the last processed thread ID.
Note that the script searches for threads newer than 7 days. You can adjust this by changing the "newer_than" parameter in the search query.
function getEmailSenders() {
var startTime = new Date().getTime(); // get the start time in milliseconds
var maxTime = 30000; // set the maximum time in milliseconds (30 seconds)
// Get the script properties to determine where to resume
var scriptProperties = PropertiesService.getScriptProperties();
var lastThreadId = scriptProperties.getProperty('lastThreadId') || '0';
// Get the Gmail threads in the inbox, starting from the last processed thread
var threads = GmailApp.search('in:inbox newer_than:7d after:' + lastThreadId);
// Create an empty array to store the unique senders
var senders = [];
// Loop through the threads and add the sender to the array if it's unique
for (var i = 0; i < threads.length; i++) {
var sender = threads[i].getMessages()[0].getFrom();
if (senders.indexOf(sender) === -1) {
senders.push(sender);
}
// Check the elapsed time and exit the loop if the maximum time has been reached
var currentTime = new Date().getTime();
if (currentTime - startTime >= maxTime) {
scriptProperties.setProperty('lastThreadId', threads[i].getId()); // store the last processed thread ID
return;
}
}
// Get the active spreadsheet and insert the senders in a new sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.insertSheet();
sheet.getRange(1, 1, senders.length, 1).setValues(senders.map(function(sender) { return [sender]; }));
// Reset the last processed thread ID if all threads have been processed
scriptProperties.deleteProperty('lastThreadId');
}
In this updated script, the time check is done after processing each thread. If the maximum time has been reached, the script exits the loop and stores the ID of the last processed thread, so it can resume from there on the next run.
scheduleEmailSenders
javascript
function getEmailSenders() {
var startTime = new Date().getTime(); // get the start time in milliseconds
var maxTime = 30000; // set the maximum time in milliseconds (30 seconds)
// Get the script properties to determine where to resume
var scriptProperties = PropertiesService.getScriptProperties();
var lastThreadId = scriptProperties.getProperty('lastThreadId') || '0';
// Get the Gmail threads in the inbox, starting from the last processed thread
var threads = GmailApp.search('in:inbox newer_than:7d after:' + lastThreadId);
// Create an empty array to store the unique senders
var senders = [];
// Loop through the threads and add the sender to the array if it's unique
for (var i = 0; i < threads.length; i++) {
var sender = threads[i].getMessages()[0].getFrom();
if (senders.indexOf(sender) === -1) {
senders.push(sender);
}
// Check the elapsed time and exit the loop if the maximum time has been reached
var currentTime = new Date().getTime();
if (currentTime - startTime >= maxTime) {
scriptProperties.setProperty('lastThreadId', threads[i].getId()); // store the last processed thread ID
return;
}
}
// Get the active spreadsheet and insert the senders in a new sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.insertSheet();
sheet.getRange(1, 1, senders.length, 1).setValues(senders.map(function(sender) { return [sender]; }));
// Reset the last processed thread ID if all threads have been processed
scriptProperties.deleteProperty('lastThreadId');
}
function scheduleEmailSenders(hours) {
var trigger = ScriptApp.newTrigger('getEmailSenders')
.timeBased()
.everyHours(hours)
.create();
}
The scheduleEmailSenders function takes one argument, hours, which is the number of hours between each run of the getEmailSenders function. This function creates a time-based trigger using the ScriptApp.newTrigger method and schedules the getEmailSenders function to run every hours hours. You can call this function with the desired number of hours to schedule the trigger.
For example, to schedule the trigger to run every 6 hours, you can call the function like this:
scss
scheduleEmailSenders(6);
Note that you need to run the scheduleEmailSenders function only once to set up the trigger. The trigger will run the getEmailSenders function at the specified interval until it is deleted.