Timestamp Script That Updates When Script Runs

33 views
Skip to first unread message

TJ Huntley

unread,
Sep 24, 2021, 7:25:20 AM9/24/21
to Google Apps Script Community
I am completely new to Script. I am looking for a snippet of a script to insert a timestamp when this script runs. I have tried several onEdit scripts to no avail since the script doesn't trigger an edit. The only way I can see this working is by inserting a timestamp directly into the script. I would think this would be fairly easy for someone who knows more about scripting, but I am running into several issues not finding the correct method. The script below is what I am using. The problem is, some emails that I am parsing don't include a date, therefore a timestamp is needed.

function getRelevantMessages()
{
  var threads = GmailApp.search("newer_than:1d AND from:NewsJunkie.com AND -label:Process AND subject:You have news!",0,100);
  var messages=[];
  threads.forEach(function(thread)
                  {
                    messages.push(thread.getMessages()[0]);
                  });
  return messages;
}

function parseMessageData(messages)
{
  var records=[];
  if(!messages)
  {
    //messages are undefined or null or just empty
    return records;
  }
  for(var m=0;m<messages.length;m++)
  {
    var text = messages[m].getPlainBody();
    
    var matches = text.match(/(?:Your News\.\s+:\s|Source\s+:\s+|News Source:\s)([A-Za-z0-9\*].+)\s+(?:Station\s+|News Station\s+|Your News Station):\s+([A-Za-z0-9\*].+)\s+(?:Subscriber\.\s+:\s[A-Za-z0-9\*].+\s+QRL Code\s+:\s|Your QRL Code\s+:\s+|QRL Codes: \s)([0-9,\.].+)\s+(?:Remarks\s+:\s+|User Remarks:\s)([A-Za-z0-9,].+)/);
  

    if(!matches || matches.length < 5)
    {
      //No matches; couldn't parse continue with the next message
      continue;
    }
    var rec = {};
    rec.account = matches[1];
    rec.date = matches[4];
    rec.merchant= matches[3];
    rec.amount = matches[2];
    
    //cleanup data
    rec.amount = parseFloat(rec.amount.replace(/,/g, ''));

    records.push(rec);
  }
  return records;
}

function getMessagesDisplay()
{
  var templ = HtmlService.createTemplateFromFile('messages');
  templ.messages = getRelevantMessages();
  return templ.evaluate();  
}

function getParsedDataDisplay()
{
  var templ = HtmlService.createTemplateFromFile('parsed');
  templ.records = parseMessageData(getRelevantMessages());
  return templ.evaluate();
}

function saveDataToSheet(records)
{
  var spreadsheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1234567890abcdefghijklmnopqrstuvwxyz");
  var sheet = spreadsheet.getSheetByName("Sheet1");
  for(var r=0;r<records.length;r++)
  {
    sheet.appendRow([records[r].account, records[r].date, records[r].merchant, records[r].amount ] );
  }
}


function onEdit(event)
  var timezone = "GMT+8";
  var timestamp_format = "MM-dd-yyyy hh:mm:ss"; // Timestamp Format. 
  var updateColName = "Amount";
  var timeStampColName = "Date";
  var sheet = event.source.getSheetByName('Sheet4'); //Name of the sheet where you want to run this script.


  var actRng = event.source.getActiveRange();
  var editColumn = actRng.getColumn();
  var index = actRng.getRowIndex();
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
  var dateCol = headers[0].indexOf(timeStampColName);
  var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
  if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
    var cell = sheet.getRange(index, dateCol + 1);
    var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
    cell.setValue(date);
  }
}


function processTransactionEmails()
{
  var messages = getRelevantMessages();
  var records = parseMessageData(messages);
  saveDataToSheet(records);
  labelMessagesAsDone(messages);
  return true;
}

function labelMessagesAsDone(messages)
{
  var label = 'Processed';
  var label_obj = GmailApp.getUserLabelByName(label);
  if(!label_obj)
  {
    label_obj = GmailApp.createLabel(label);
  }
  
  for(var m =0; m < messages.length; m++ )
  {
     label_obj.addToThread(messages[m].getThread() );  
  }
  
}

function doGet()
{
  return getParsedDataDisplay();

  //return getMessagesDisplay();
}

Clark Lind

unread,
Sep 24, 2021, 12:28:10 PM9/24/21
to Google Apps Script Community
I believe onEdit() requires human interaction. Try onChange() instead. That should react to any page change, human or otherwise.
Reply all
Reply to author
Forward
0 new messages