Extracting the last contact date for an email address (from or sent) from gmail into google sheets

Hi!

Hoping someone can assist. I am looking to extract the last contact date for either sent or received emails and send this into a google sheet.

I have the below script from someone online but I don’t know how to iterate this across a number of rows.

TIA

function myFunction() {
  // Get the value in the cell A1 (which will have the email to check the latest interaction)
  var ss = SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange('A1').getValue();
  
  // Search the date of the last message of the search from the email in cell A1 and log it
  var latestEmail = GmailApp.search('from:"'+ss+'"')[0].getLastMessageDate();
  Logger.log(latestEmail);
  
  // Print the date on the cell to the right
  SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange('B1').setValue(latestEmail);
}

This is a forum for Appsheet. You’d be better off posting this question in an App Script specific forum. There’s a good one on reddit that I know of.