I have a script that will capture data from the email and saved it to the next available row in a google sheet.
function RevisionHistory() {
// Use sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var logsheetname = "History";
// Gmail query
var query = "label:Status Update Notification";
// Search in Gmail, bind to array
var threads = GmailApp.search(query);
// Loop through query results
for (var i = 0; i < threads.length; i++)
{
// Get messages in thread, add to array
var messages = threads[i].getMessages();
// Used to find max index in array
var max = messages[0];
var maxIndex = 0;
// Loop through array to find maxIndexD = most recent mail
for (var j = 0; j < messages.length; j++) {
if (messages[j] > max) {
maxIndex = j;
max = messages[j];
}
}
// Find data
var mId = messages[maxIndex].getId() // ID used to create mail link
var from = messages[maxIndex].getFrom();
var cc = messages[maxIndex].getPlainBody();
var time = messages[maxIndex].getDate()
var sub = messages[maxIndex].getSubject();
var xx = ss.getSheetByName(logsheetname);
// Write data to sheet
xx.appendRow([cc, time, sub])
}
var label1 = GmailApp.getUserLabelByName("Status Update Notification"); // FROM
var label2 = GmailApp.getUserLabelByName("99 - Extracted Status Update"); // TO
var threads = label1.getThreads();
for (var i = 0; i < threads.length; i++) {
threads[i].addLabel(label2).removeLabel(label1).moveToArchive();
}
}
It places the contents to columns A B and C, now I need help in ensuring that the formulas will be placed on the same row but in columns E F and G, below are the formulas starting on row 2
E - =Right(C2,( len(C2)-find("-",C2)))
F - =MID(A2,SEARCH("to",A2)+3,SEARCH("by",A2)-SEARCH("to",A2)-4)
G - =MID(A2,SEARCH("by",A2)+3,SEARCH(".com",A2)-SEARCH("by",A2)+2)
UPDATE:
I was able to find the following script (modified a bit to meet my requirements), how I am worried that this might slow down the sheet because this sets the formula from the very first row as I have thousands of records. I need help in modifying in such a way that this will only copy the formula from the last row to the next row, so when my script captures the data from email and placed in row 3750, the script that I am looking for should just copy the formula from row 3749 and place it in row 3750 (columns E F and G only)
function CopyFormula() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss.getRange("E2").setFormula("=Right(C3,( len(C3)-find($D$1,C3)))");
var lr = ss.getLastRow();
var filldownrange = ss.getRange(2, 4, lr-1);
ss.getRange("E2").copyTo(filldownrange);
};