Google Apps Script邮件添加格式签名及姓名格式转换问题
Let's break down how to solve both of your problems with straightforward code adjustments:
1. Adding a Formatted Email Signature with Logo & Bold Company Name
The key here is to use HTML-formatted email (instead of plain text) and embed your logo as an inline attachment. Here's how to modify your sendEmail function:
First, update the MailApp.sendEmail call to use htmlBody instead of body, and include your logo as an inline attachment with a unique cid (content ID) reference:
function sendEmail(form) { const sSheet = SpreadsheetApp.getActiveSpreadsheet(); const file = DriveApp.getFileById(sSheet.getId()); const documentUrl = file.getUrl(); var toEmail = form.toAddress; var ccEmail = form.ccAddress; var subject = form.subject; var message = form.message; const folderId = ""; const ssId = sSheet.getId(); const returnFlag= 'blob'; // --- Existing sheet logic remains the same --- if (form.process == "sendHotelRegistration"){ var fileName = "Hotel Registration Form"; var sheetId = sSheet.getSheetByName("Hotel Registration form").getSheetId(); var sheet= sSheet.getSheetByName("Hotel Registration form"); sheet.showSheet(); } else if (form.process == "updateRegistry"){ var fileName = "Hotel Contract Document"; var sheetId = "All"; } var pdfFile = createPDF(folderId, ssId, fileName, sheetId, returnFlag); // --- NEW: Prepare HTML content with signature --- // 1. Get your logo from Drive (replace with your logo's file ID) const logoFile = DriveApp.getFileById("YOUR_LOGO_FILE_ID"); const logoBlob = logoFile.getAs("image/png"); logoBlob.setName("company-logo.png"); // 2. Build HTML message with signature const htmlMessage = ` ${message} <br><br> <hr> <img src="cid:logo" alt="Company Logo" width="150"> <p><strong>Your Company Name</strong><br> Street Address | City, State | Phone: (123) 456-7890</p> `; // 3. Send email with HTML body and inline logo MailApp.sendEmail({ to: toEmail, cc: ccEmail, subject: subject, htmlBody: htmlMessage, attachments: [pdfFile, logoBlob], inlineImages: { logo: logoBlob } // Maps the cid:logo to the blob }); }
Notes:
- Replace
YOUR_LOGO_FILE_IDwith the actual ID of your logo file in Google Drive (make sure the script has permission to access it). - Adjust the logo width, company name, and contact details to match your brand.
- The
inlineImagesparameter links thecid:logoin your HTML to the actual image blob, so it displays directly in the email instead of as a separate attachment.
2. Converting contractorName to Title Case (Supports 2-4 Word Names)
Instead of using .toLowerCase(), create a reusable function that capitalizes the first letter of each word and makes the rest lowercase. This works for 2-4 word names (and scales to more if needed):
// Helper function to convert text to title case function toTitleCase(str) { if (!str) return ""; // Split into words, capitalize each, then join back return str.toLowerCase().split(" ").map(word => { // Skip empty strings (in case of extra spaces) if (!word) return ""; // Capitalize first character, add the rest of the word return word.charAt(0).toUpperCase() + word.slice(1); }).join(" "); } // Then replace your existing line with: html.contractorName = toTitleCase(data.contractorName);
Edge Case Handling (Optional):
If you need to account for names with prefixes like Mc, O', or van, you can tweak the function to handle those:
function toTitleCase(str) { if (!str) return ""; return str.toLowerCase().split(" ").map(word => { if (!word) return ""; // Handle common name prefixes const prefixes = ["mc", "o'", "van", "de"]; const lowerWord = word.toLowerCase(); for (const prefix of prefixes) { if (lowerWord.startsWith(prefix)) { return prefix.charAt(0).toUpperCase() + prefix.slice(1) + word.charAt(prefix.length).toUpperCase() + word.slice(prefix.length + 1); } } // Default case: capitalize first letter return word.charAt(0).toUpperCase() + word.slice(1); }).join(" "); }
This will correctly format names like mcdonald → McDonald, o'connor → O'Connor, or van dyke → Van Dyke.
内容的提问来源于stack exchange,提问作者DevB1

