The problem I have is the following: this is the email I am receiving from this sender (format is always the same, number of rows differs greatly (4 to 100) (Table inside email):
I have modified this code to suit my needs
"Good morning
TEST Emergency has the following CMO vacancies – hope you can help
Date | Shift | Department | Position | Hospital | Rate per hr
Monday, 16 November 2020 | 0800-1800 | EMERGENCY | CMO | TEST |$130
Monday, 16 November 2020 | 1400-2400 | EMERGENCY | CMO | TEST |$145
Tuesday, 17 November 2020 | 1400-2400 | EMERGENCY | CMO | TEST |$145
Sunday, 22 November 2020 | 1200-2200 | EMERGENCY | CMO | TEST |$145
....Signature"
I am using this code to extract it:
var SEARCH_QUERY = 'label: Testing';
function appendData_(sheet, array2d) {
sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
}
// Main function, the one that you must select before run
function saveEmails() {
var array2d = getEmails_(SEARCH_QUERY);
if (array2d) {
appendData_(SpreadsheetApp.getActiveSheet(), array2d);
}
}
// Will get the emails and return a list
function getEmails_(q) {
var emails = [];
// Sheet header columns
emails.push(["Date","Shift","Department","Hospital","Rate"]);
var threads = GmailApp.search(q);
for (var i in threads) {
var thread=threads[i];
//var data = threads[i].getPlainBody();
var msgs = threads[i].getMessages();
for (var j in msgs) {
var msg = msgs[j];
// Values to get and store
var data = msg.getPlainBody().replace(/(Kee(.|\n|\r)*)/ig,'').replace(/(\r\n\r\n)/g,',').replace(/(.)*help/g,'').replace(/((\w)*day)/g,'').split('\\W+');//.match(/(16 November 2020)/ig);
//return date;
var shiftdate = msg.getPlainBody().match(/(\d{1,2}\s(Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|Sept|September|Oct|October|Nov|November|Dec|December)\s\d{4})/ig);
var shifttime = msg.getPlainBody().match(/(\d{4}-\d{4})/ig);
var department = msg.getPlainBody().match(/(EMERGENCY)/g);
var hospital = msg.getPlainBody().match(/(TEST)/g);
var rate = msg.getPlainBody().match(/(\$\d{3})/g);
// for (let i=0 i < shiftdate.length; i++){
emails.push([shiftdate,shifttime,department,hospital,rate]);
console.log(emails)
// }
}
return emails;
console.log(emails)
}
//Will clear the sheet and add all emails there
function appendData_(sheet, array2d) {
sheet.clear();
sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
}
Now the above code nets me this:
[ 'Date', 'Shift', 'Department', 'Hospital', 'Rate' ],
[ [ '16 November 2020',
'16 November 2020',
'17 November 2020',
'22 November 2020' ],
[ '0800-1800', '1400-2400', '1400-2400', '1200-2200' ],
[ 'EMERGENCY', 'EMERGENCY', 'EMERGENCY', 'EMERGENCY' ],
[ 'TEST', 'TEST', 'TEST', 'TEST' ],
[ '$130', '$145', '$145', '$145' ] ] ]
When inserting the data into the spreadsheet, I only get two rows. The title array, and the first value and the correct copy of the first value in each of the sub-arrays:
Date | Shift | Department | Position | Hospital | Rate per hr
16 November 2020 | 0800-1800 | EMERGENCY | CMO | TEST |$130
I am not sure how to proceed, as my end goal is the following:
In the end I want to further process the data so that a Row shows:
Start Date | End Date | Shift Start | Shift End | Department | Position | Hospital | Rate per hr
Meaning the script would need to go through the dates and extract each Start and End date for consecutive dates and then push each last consecutive date to Array "End Date". This would need to be compared against the Time (I can split that array later on).
Now, What would be the best way to proceed?
- Should I try to insert the data into the columns now, and then create a code that will read each row and compare the date, and time to find consecutive values? In which case:
1.1 How can I push each value contained in sub-array to its own cell in a column?
I have tried
array.forEach((item,index,array) => array[index] = [item]);
but it didn't seem to work.
The problem with above is that after execution is that A,B,C,D,E,F all contain their respective array and display the first value. Any and all input would be very very much appreciated.
- Should I create new arrays with the first, second etc value from each and then put them in a for loop with an if statement? Matching start times and then reassigning the [0] value from the last consecutive array into a new array?
Meaning:
firstArray [16 November 2020],[0800-1800],[EMERGENCY],[CMO],[TEST],[$130]
secondArray[17 November 2020],[1400-2400],[EMERGENCY],[CMO],[TEST],[$145]
for(var line in firstArray)
{
var isMatched = secondArray.indexOf(firstArray[1]);
if (isMatched !== -1)
{
var matchedValFromArray2 = secondArray[isMatched]
};
}