0

I have the following script to read information from my email and populate the information in a google sheet. Most of the time, there will be one email in the Pending label (script should run every minute), but regardless, the script is not pulling the information in the Pending label. For some reason, the sheet is populating information that's from the "Done" label. Also, it's populating duplicate values. Any idea what could be wrong here?

For example, let's say the Pending label has the email johndoe@gmail.com and the Done label has an email joesmith@gmail.com, the script is pulling information from the joesmith@gmail.com email even though it's already processed. The johndoe@gmail.com will still transfer over from Pending to Done, but the right information won't transfer over.

// Modified from http://pipetree.com/qmacro/blog/2011/10/automated- 
email-to-task-mechanism-with-google-apps-script/

// Globals, constants
var LABEL_PENDING = "Example Label/PENDING";
var LABEL_DONE = "Example Label/DONE";

// processPending(sheet)
// Process any pending emails and then move them to done
function processPending_(sheet) {

// Date format
var d = new Date();
var date = d.toLocaleDateString();

// Get out labels by name
var label_pending = GmailApp.getUserLabelByName(LABEL_PENDING);
var label_done = GmailApp.getUserLabelByName(LABEL_DONE);

// The threads currently assigned to the 'pending' label
var threads = label_pending.getThreads();

// Process each one in turn, assuming there's only a single
// message in each thread
for (var t in threads) {
var thread = threads[t];

// Gets the message body
var message = thread.getMessages()[0].getBody();
var recipient = thread.getMessages()[0].getTo();

// Processes the messages here
orderinfo = message.split("example split");
rowdata = orderinfo[1].split(" ");


// Add message to sheet
sheet.appendRow([rowdata[1], recipient]);

// Set to 'done' by exchanging labels
thread.removeLabel(label_pending);
thread.addLabel(label_done);
  }
}

// main()
// Starter function; to be scheduled regularly
function main_emailDataToSpreadsheet() {
// Get the active spreadsheet and make sure the first
// sheet is the active one
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.setActiveSheet(ss.getSheets()[0]);

  // Process the pending emails
  processPending_(sh);
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
David K
  • 33
  • 1
  • 5
  • Is this a follow-up question of https://stackoverflow.com/q/50611877/1595451? – Rubén Jun 14 '18 at 14:47
  • Hi Ruben, this is a follow up of my previous question. I've run into an error where the script is pulling old information into the Google Sheet and not the email in the Pending label. Not sure what to do here. – David K Jun 14 '18 at 21:39
  • I think that followup question should mention that they are that. Gmail allow add multiple label to threads. I suggest you to include a way to check that the thread only have only one of the labels and not both. – Rubén Jun 14 '18 at 22:17

0 Answers0