I am trying to extra email replies from gmail. I already found some code that is supposed to work.

My question is: how can one expand it to support a filter as a variable? (in this case so a filter is passed and working named z_lbl)

I am calling the function like so: =listemails('test')

But my error is sometimes: 'Formula parse error'; or I have also seen 'not allowed to use GmailApp.search'

IN the end I would like to make a function that I can call in my sheets. It takes 1 variable that I program either as a label finder (becomes label:searchme). Or even better: as a complete filter that is added as a search filter (becomes 'label:searchme has:attachment' etc).

Help is appreciated

function listEmails(z_lbl) { 
  // http://stackoverflow.com/a/12029701/1536038  
  // get all messages      
  if (z_lbl == null) {
    throw 'Please add a label variable to the function to search for';
  var eMails = GmailApp.getMessagesForThreads(
      .reduce(function(a, b) {return a.concat(b);})
      .map(function(eMails) {
    return eMails.getFrom() 

  // sort and filter for unique entries  
  var aEmails = eMails.sort().filter(function(el,j,a)
    {if(j==a.indexOf(el))return 1;return 0});  

  // create 2D-array
  var aUnique = new Array();  
  for(var k in aEmails) {

  // add data to sheet
  SpreadsheetApp.getActiveSheet().getRange(1, 1, aUnique.length, 1)
  • The key line to achieve your goal is GmailApp.search('label:'+z_lbl). A quick and dirty way that could work is to remove 'label:', then when you call the function pass as argument "a filter". – Rubén Jun 10 '16 at 11:22
  • "make a function that I can call in my sheets" is not going to happen: custom functions called from a spreadsheet run without authorization, hence cannot access email. – user79865 Jun 10 '16 at 12:41

I created a sheet that uses filters that allow all kinds of exports.

Works great - hope it helps others

Shared here: https://docs.google.com/spreadsheets/d/14nOGCyu2nOLDQ_cJNQfVsDiBxy-Te3W8eh9du3C4sVQ/copy

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.