Home > Software engineering >  Delete gmails with script using google sheet with gmail search terms and multiple rows in sheet for
Delete gmails with script using google sheet with gmail search terms and multiple rows in sheet for

Time:08-16

This function will delete ONE (1) email matching search term defined in A2 (And it works):

function deleteEmail() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheets()[0];
  const searchTerm = sheet.getRange("A2").getValue();
  GmailApp.search(searchTerm).map(thread => thread.moveToTrash()); 
}

However, creating individual script functions for individual search terms with individual triggers would take forever to do...

I tried simply to use A2:A, but unfortunately that was not working.

function deleteEmail() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheets()[0];
  const searchTerm = sheet.getRange("A2:A").getValue();
  GmailApp.search(searchTerm).map(thread => thread.moveToTrash()); 
}

How can one delete gmails with a script using google sheet scripts with gmail search terms and multiple rows in a sheet for bulk processing?

3 parameters are needed: sender, subject and older_than, like this:

from:(@google.com) subject:Notification older_than:3d

CodePudding user response:

Perhaps this will work

function deleteEmail() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheets()[0];
  const terms = sheet.getRange("A2:A"   sheet.getLastRow()).getValues();
  terms.forEach(s => GmailApp.search(s).forEach(thread => thread.moveToTrash()))
}
  • Related