I m trying to make a script to search my inbox for email replies and timestamp my sheet when I reply is found.
I have email adresses in Col A that I have emailed and when any of them reply/email me back I then want to record the reply by putting a timestamp in Col C on the same row of the email that have replied.
What I have managed to so far:
I have rewritten the script many times and been able to timestamp when a reply is found on only 1 email adress but when there is more emails I get stuck.
I m trying to figure out how to write the loop and if statements to make the script loop through every row of emails and timestamp the correct rows.
I m not an expert at this but I do now have a decent understanding around things hence to all the searching, googling and youtubeing to edjucate myself. I have been trying to make this work for weeks and without success, would really appreciate some help and input on what is wrong here.
Here is the sequence that I m trying to achieve explained:
1. The search
- Check if there is email adresses in a Col A
- If there is emails then check if a response is already recorded (timestamp) in Col C
2. Recorded reply
- If there is a timestamp in Col C then skip that row and go to the next one and repeat step 1
3. No reply
If there is no timestamp then do a search with the email on that row in my inbox for replies.
If no reply is found then skip the row and repeat step 1
4. Reply found
- If a reply is found then timestamp Col C in the same row and go to the next one
5. Searched through all rows with data
- When the script has gone through every row with data in it will stop and exit the script.
Here is a link to the sheet and the script down below.
Spreadsheet: https://docs.google.com/spreadsheets/d/1HzLRtSFULijIVbtW8xcIPDOgQKCYdQ4TLN2FgEtSqIg/edit?usp=sharing
Script:
function myFunction() {
const sheet = SpreadsheetApp.openByUrl( https://docs.google.com/spreadsheets/d/1HzLRtSFULijIVbtW8xcIPDOgQKCYdQ4TLN2FgEtSqIg/edit#gid=1870420095 ).getSheetByName( Sheet1 );
const email = sheet.getRange( A:A ).getValues().flat();
const responseColumn = sheet.getRange( C:C ).getValues().flat();
const q = GmailApp.search(email);
var messages = q[0].threads.getMessages();
for (var i = 0; i < email.length; i++) {
if (email[i]) { // Make sure the cell is not empty
if (responseColumn[i]) {
continue; // Skip if response already recorded
}
if (q.length > 1){
for (var i = 0; i < messages.length; i++)
{sheet.getRange(i + 2, 3).setValue(new Date());
}
Logger.log(email);
}
}
}
}