Sunday, November 25, 2018

Gmail: Using Google Apps Script to Auto Delete Gmail Trash on a schedule! (or anything else for that matter)


1) Create Script

2) test

3) Schedule


Some really cool stuff we can do using Google Developers Dashboard, some references here:

https://gist.github.com/balupton/9800939

and

https://www.mikecr.it/ramblings/marking-gmail-read-with-apps-script/


for my case I need to purge the Trash every Sunday, to do this:

Summary as follows:

1) Create a script to clean Trash label (folder)

function deleteForever() {

  Logger.log('START ');
   var labelName = "Trash"

  var threads = GmailApp.search("in:trash label:" + labelName);
   for (var i = 0; i < threads.length; i++) {
    Logger.log('Delete id ' +  threads[i].getId());
      try {
         Gmail.Users.Messages.remove('me', threads[i].getId());
      }
      catch(e){}
      Logger.log('DONE Delete id ' +  threads[i].getId());
   }
}


2) To enable advanced services for this script, locate Resources on the menu, and select Advanced Google services...

3) Enable Gmail API on the list.

4) Before selecting OK, click on the Google Developers Dashboard link. Search for gmail, and enable

5) Schedule via a trigger:


FYI:

If new to G Scripting, use logging per this spec


Basic logging

A basic approach to logging in Apps Script is to use the built-in Logger. Logs created this way can be viewed by selecting View > Logs in the script editor. These logs are intended for simple checks during development and debugging, and do not persist very long.

For example, consider this function:

function emailDataRow(rowNumber, email) {
  Logger.log('Emailing data row ' + rowNumber + ' to ' + email);
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var rowData = data[rowNumber-1].join(" ");
  Logger.log('Row ' + rowNumber + ' data: ' + rowData);
  MailApp.sendEmail(email,
                    'Data in row ' + rowNumber,
                    rowData);
}

When this script is run with inputs "2" and "john@example.com" the following logs are written:

[16-09-12 13:50:42:193 PDT] Emailing data row 2 to john@example.com
[16-09-12 13:50:42:271 PDT] Row 2 data: Cost 103.24

The Logger.log() method expects a string or other JavaScript object. The logs can only hold a limited amount of data, so avoid logging large amounts of text.

No comments:

Post a Comment