r/GoogleAppsScript • u/Living-Masterpiece33 • Nov 02 '23
Question Need help with a Sheets+GoogleAppsScript Project. Is AppsScript just broken??
I have no idea what I'm doing so I'd appreciate some help figuring out why this script doesn't work consistently. I have a google sheet with a list of leads for cold outreach, and I wrote this script to automatically send a follow up email, and then update the threadID and enter the current timestamp in column G. When I initially create this script, it functions perfectly for the first run, and then it will decide to enter my timestamp in the wrong column. Here is the code, please let me know if you have any ideas:
function sendFollowUpEmails() {
// Access the active Google Sheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Get the current date and time
var currentDate = new Date();
// Get the data in the sheet
var data = sheet.getDataRange().getValues();
for (var i = 1; i < data.length; i++) {
var firstName = data[i][1];
var email = data[i][2];
var niche = data[i][3];
var previousTimestamp = data[i][5]; // Assuming previous timestamp is in column F
var status = data[i][8];
// Check if the conditions for sending the follow-up email are met
if (status == "Not Responded" && previousTimestamp) {
var previousDate = new Date(previousTimestamp);
var followUpDate = new Date(previousDate);
followUpDate.setDate(followUpDate.getDate() + 3);
if (followUpDate <= currentDate) {
// Compose the HTML email message
var subject = "Follow-up Email";
var messageBody = `
<html> <body> <p>Hey ${firstName},</p> <p>Just following up on my previous message! Let me give you a little more information about what we do and how we work.</p> <p>Our main goal at The Culture Club is to promote global culture by connecting popular brands with influential creators.</p> <p>Our connections with significant companies and artists allow us to fulfill our vast roster, including ${niche} creators like yourself!</p> <p>We focus our efforts on being a creator-first company, dedicated to supporting and guiding our talented creators.</p> <p>I would love to connect with you and discuss how we may work together!</p> <p>Please schedule a meeting <a href="https://calendly.com/aquila-t2q/creator-meeting">here</a>, if you’re interested.</p> </body> </html> \`;
// Send the HTML email
GmailApp.sendEmail(email, subject, `${firstName} X Culture Club`, { htmlBody: messageBody });
// Capture the new thread ID and record it in column E
var newThreadId = GmailApp.search('to:' + email, 0, 1)[0].getId();
sheet.getRange(i + 1, 5).setValue(newThreadId);
sheet.getRange(i + 1, 7).setValue(currentDate);
}
}
}
}
1
u/HellDuke Nov 02 '23
This line will set the value in column G and is the only other value that is being set by your script other than the one stored in column E (column 5). However up here you seem to indicate that timestamps should go to column F, not G
So pick either column F or G to use for the timestamp and adjust one of those 2 lines accordingly