function main( workbook: ExcelScript.Workbook, emailBody: string, emailSubject: string, receivedTime: string ): void { // Get the worksheet named "Logs" const sheet = workbook.getWorksheet("Logs"); // Helper function to strip HTML tags and decode entities function stripHtml(html: string): string { // First, convert common HTML line breaks to newlines let text = html.replace(//gi, '\n') .replace(/<\/div>/gi, '\n') .replace(/<\/p>/gi, '\n') .replace(/<\/li>/gi, '\n'); // Remove all remaining HTML tags text = text.replace(/<[^>]*>/g, ''); // Decode common HTML entities text = text.replace(/ /g, ' ') .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, "'") .replace(/'/g, "'"); // Clean up excessive whitespace but preserve newlines text = text.replace(/ +/g, ' '); // Multiple spaces to single space text = text.replace(/\n\s+/g, '\n'); // Remove spaces after newlines text = text.replace(/\n{3,}/g, '\n\n'); // Max 2 newlines in a row return text.trim(); } // Helper function to extract text between two strings function extractBetween(text: string, start: string, end: string): string { const startIndex = text.indexOf(start); if (startIndex === -1) return ""; const afterStart = text.substring(startIndex + start.length); const endIndex = afterStart.indexOf(end); if (endIndex === -1) return afterStart.trim(); return afterStart.substring(0, endIndex).trim(); } // Strip HTML from email body first const cleanBody = stripHtml(emailBody); // Parse all fields from clean email body const alertLevelMatch = cleanBody.match(/LEVEL (.*?) ALERT/); const alertLevel = alertLevelMatch ? alertLevelMatch[1].trim() : ""; const asset = extractBetween(cleanBody, "Asset: ", "\n"); const threshold = extractBetween(cleanBody, "Threshold: ", "\n"); const proximity = extractBetween(cleanBody, "Proximity: ", "\n"); const time = extractBetween(cleanBody, "Time: ", "\n"); const location = extractBetween(cleanBody, "Location: ", "\n"); const embargo = extractBetween(cleanBody, "Alert Embargo: ", "\n"); // Split coordinates into latitude and longitude const coords = location.split(","); const latitude = coords.length > 0 ? coords[0].trim() : ""; const longitude = coords.length > 1 ? coords[1].trim() : ""; // Format received date const receivedDate = new Date(receivedTime); const dateString = receivedDate.toISOString().split('T')[0]; // YYYY-MM-DD format // Find the last row with data (skip header) const usedRange = sheet.getUsedRange(); const lastRow = usedRange ? usedRange.getRowCount() : 1; const newRow = lastRow + 1; // Add new row with all data sheet.getRange(`A${newRow}:K${newRow}`).setValues([[ dateString, // Date time, // Time alertLevel, // Alert Level "Lightning", // Alert Type asset, // Asset threshold, // Threshold proximity, // Proximity latitude, // Latitude longitude, // Longitude embargo, // Embargo (minutes) receivedTime // Received Timestamp ]]); }