Give me feedback to my code. Dont change anything yet, since it works fine. function…
- User
Give me feedback to my code. Dont change anything yet, since it works fine.
function sendEmails() { const ss = SpreadsheetApp.openById(SPREADSHEET_ID); const sheetNames = ['Nicht erreicht', 'Nicht erreicht 02', 'Nicht erreicht 03']; const contentSheet = ss.getSheetByName('Email Content');
if (!contentSheet) { Logger.log('Email Content sheet not found. Please create a sheet named "Email Content".'); return; }
const contentData = contentSheet.getDataRange().getValues(); const emailContent = {};
contentData.forEach(row => { if (row[0] && row[1]) { emailContent[row[0]] = row[1]; Logger.log(
Extracted email content for label '${row[0]}': ${row[1]}); } });sheetNames.forEach(sheetName => { const sheet = ss.getSheetByName(sheetName); if (sheet) { processSheet(sheet, emailContent); } }); }
function processSheet(sheet, emailContent) { const data = sheet.getDataRange().getValues(); const headers = data.shift();
const mailSendenIndex = headers.indexOf('Mail senden?'); const crmStatusIndex = headers.indexOf('CRM Status'); const emailAddressIndex = headers.indexOf('E-Mail-Adresse'); const threadIdIndex = headers.indexOf('Thread-ID');
const rowsToDelete = [];
data.forEach((row, index) => { const crmStatus = row[crmStatusIndex]; const shouldSendEmail = row[mailSendenIndex] === 'Ja'; const recipient = row[emailAddressIndex]; const threadId = row[threadIdIndex]; const currentLabel = sheet.getName();
codeif (shouldSendEmail) { sendEmailAndUpdateLabel(recipient, threadId, currentLabel, emailContent); rowsToDelete.push(index + 2); } else if (crmStatus && !crmStatus.includes('Nicht erreicht')) { updateLabelToErfolg(threadId); rowsToDelete.push(index + 2); }});
for (let i = rowsToDelete.length - 1; i >= 0; i--) { sheet.deleteRow(rowsToDelete[i]); } }
function sendEmailAndUpdateLabel(recipient, threadId, currentLabel, emailContent) { Logger.log('sendEmailAndUpdateLabel called with parameters:'); Logger.log('recipient: ' + recipient); Logger.log('threadId: ' + threadId); Logger.log('currentLabel: ' + currentLabel); Logger.log('emailContent: ' + JSON.stringify(emailContent));
const subject = emailContent['Subject'] || 'Re: Follow-up'; const body = emailContent[currentLabel] || 'This is a follow-up email.';
Logger.log('subject: ' + subject); Logger.log('body: ' + body);
const thread = GmailApp.getThreadById(threadId); if (!thread) { Logger.log('Thread not found: ' + threadId); return; }
Logger.log('Thread found: ' + threadId);
- Assistant