TiagoGouvea / gmail-to-google-sheets-script

Search for emails on your gmail account (using a query) and store results on a google sheets
64 stars 20 forks source link

"from:" and "is:sent" not being respected #3

Open crysman opened 2 years ago

crysman commented 2 years ago

This is a wonderful code, thanks!

But... it does not respect the "from:" filter :( - native Gmail does neither - it counts the whole conversation tree.

Any ideas how to actually get sent e-mails only?

Sample query: is:sent from:mymail@mydoma.in -notwantthisma.il -filename:.ics after:2022/6/15

Result: Threads' incoming e-mails are included, too.

Expected behavior: Only sent mails included.

TiagoGouvea commented 2 years ago

Hi @crysman .

I tested here and got the same results, it gets more than expected.

But it's not under my control, it's related to the gmail filtering way..

We can, maybe, think on a way to loop the results and filter by code it, resulting in only the emails you want, but, I don't know if it is doable.

crysman commented 2 years ago

I've got a workaround - adding a filter like this:

//filter
function filterSheet() {
  console.info("adding filter...");
    var spreadsheet = SpreadsheetApp.getActive()
    spreadsheet.getRange('A:D').createFilter();
        //var QUERY_SENDER = Session.getActiveUser().getEmail();
    var criteria1 = SpreadsheetApp.newFilterCriteria()
    .whenTextContains(`${QUERY_SENDER}`)
  if ( ONLY_MYDOMAIN ) {
    console.info("filtering... (📥 INTERNAL mails only)...")
      var criteria2 = SpreadsheetApp.newFilterCriteria()
      .whenTextContains("@mydomain.com")
  } else {
    console.info("filtering... (📤 EXTERNAL mails only)...")
    var criteria2 = SpreadsheetApp.newFilterCriteria()
      .whenTextDoesNotContain("@mydomain.com")
  }
  //columns are:
  //Date | Subject | Sender (from) | Recipients (to) | Recipients (cc,bcc)    
  spreadsheet.getActiveSheet().getFilter().setColumnFilterCriteria(3, criteria1);
  spreadsheet.getActiveSheet().getFilter().setColumnFilterCriteria(4, criteria2);
  console.info(getFilteredRows()+" rows filtered-out");
}

function clearFilter() {
//to clear the filter before next run...
  var spreadsheet = SpreadsheetApp.getActive()
  console.log("Clearing sheet's filters...");
  spreadsheet.getActiveSheet().getFilter() && spreadsheet.getActiveSheet().getFilter().remove();
}