Modify a gmail script

203 views
Skip to first unread message

Joseph

unread,
May 15, 2020, 9:12:14 PM5/15/20
to Google Apps Script Community
Hi everyone,

I have the following script that collects in a gsheet the emails from a specific label that I select from a gmail account. As you can see,I have to enter the label name manually in the ui.prompt window that appears. I would the label name "Need Review" which is the label in the gmail that the emails are being collected from, to be added directly to the code,so I can trigger this code automatically without the need entering the label name in the prompt window. Thank you in advance.

Code

var ui = SpreadsheetApp.getUi();
function onOpen(e){
  
  ui.createMenu("Gmail Manager").addItem("Get Emails by Label   ", "getGmailEmails").addToUi();
  
}
function getGmailEmails(){
  var input = ui.prompt('Label Name', 'Enter the label name that is assigned to your emails:', Browser.Buttons.OK_CANCEL);
  
  if (input.getSelectedButton() == ui.Button.CANCEL){
    return;
  }
  
  var label = GmailApp.getUserLabelByName(input.getResponseText());
  var threads = label.getThreads();
  
  for(var i = threads.length - 1; i >=0; i--){
    var messages = threads[i].getMessages();
    
    for (var j = 0; j <messages.length; j++){
      var message = messages[j];
      if (message.isUnread()){
        extractDetails(message);
        GmailApp.markMessageRead(message);
      }
    }
    threads[i].removeLabel(label);
    
  }
  
}

function extractDetails(message){
  var dateTime = message.getDate();
  var subjectText = message.getSubject();
  var senderDetails = message.getFrom();
  var bodyContents = message.getPlainBody();
  var CC = message.getCc();
  var Bcc = message.getBcc();
  var ReplyTo= message.getReplyTo();
  var Getto= message.getTo();
  
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  activeSheet.appendRow([dateTime, senderDetails, subjectText, bodyContents, CC,Bcc,ReplyTo,Getto]);
}

Alex

unread,
May 16, 2020, 12:08:59 AM5/16/20
to Google Apps Script Community
As I can understand the best way for you is to use a dialog instead the prompt.

Alan Wells

unread,
May 16, 2020, 8:23:40 AM5/16/20
to Google Apps Script Community
If you want to remove the prompt, then you need to remove (or comment out) the lines:

 
  if (input.getSelectedButton() == ui.Button.CANCEL){
    return;
  }

You can stop those lines of code from running by putting two slashes in front of EACH line:

  //if (input.getSelectedButton() == ui.Button.CANCEL){
    //
return;
 
//}

And you need to change the line:

var input = ui.prompt('Label Name', 'Enter the label name that is assigned to your emails:', Browser.Buttons.OK_CANCEL);


to:

var input = "PUT THE LABEL NAME HERE";

Where it states: "PUT THE LABEL NAME HERE"
You can "hard code" the label name.

Then the code will never display a prompt and always use the same label name.

If you want to run the code automatically, then you need to "install" a trigger.  In the code editor, choose "Edit" and "Current projects triggers" then click "+Add"

Joseph

unread,
May 19, 2020, 1:16:49 PM5/19/20
to Google Apps Script Community
Thanks for the replies ,is work great. I very much appreciate it!!
Reply all
Reply to author
Forward
0 new messages