I have the below script that works fine but I am wanting the four numbers and post code in the body (an example is 3260 TA4 33Q) to be bold and red but the rest is fine as it is. I have read a lot of examples and have added something in but it won't action what I am after. It's within the first line of the htmlBody so thought maybe that it would come under a header...? Maybe it is because is it a variable...? (stockNumber +) I am trying to make this very simple for customers to know exactly what to enter in the form. Any assistance would be hugely appreciated.
function Email11() {
function copyData() { }
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("S/A - Darrin");
var destSheet = ss.getSheetByName("Payments Taken");
var dateTaken = sourceSheet.getRange("G39").getValue();
var paymentMethod = sourceSheet.getRange("H39").getValue();
var salesAdvisor = sourceSheet.getRange("F4").getValue();
var customerName = sourceSheet.getRange("C11").getValue();
var stockNumber = sourceSheet.getRange("A21").getValue();
var model = sourceSheet.getRange("D21").getValue();
var year = sourceSheet.getRange("C21").getValue();
var location = sourceSheet.getRange("D24").getValue();
var amount = sourceSheet.getRange("E39").getValue();
destSheet.appendRow([dateTaken, paymentMethod, salesAdvisor, customerName, stockNumber, model, year, location, amount]);
function copyData() { }
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("S/A - Darrin");
var destSheet = ss.getSheetByName("Departures");
var dateSold = sourceSheet.getRange("A9").getValue();
var salesAdvisor = sourceSheet.getRange("F4").getValue();
var customerName = sourceSheet.getRange("C11").getValue();
var stockNumber = sourceSheet.getRange("A21").getValue();
var model = sourceSheet.getRange("D21").getValue();
var year = sourceSheet.getRange("C21").getValue();
var location = sourceSheet.getRange("D24").getValue();
destSheet.appendRow([dateSold, salesAdvisor, customerName, stockNumber, model, year, location]);
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId;
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin");
var fileName = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("A19").getDisplayValue();
var folder = DriveApp.getFoldersByName("Sales Agreements").next();
var Blob = sheetName.getParent().getBlob().getAs('application/pdf');
folder.createFile(Blob).setName(fileName);
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();
//var email = Session.getUser().getEmail();
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("F6");
var emailAddress = emailRange.getValue();
var stockNumber = ss.getRange("B7").getValue();
var subject = "Darrin " + stockNumber;
var htmlBody = (stockNumber + " (Your Reference)\n \nPlease click the below link and complete the declaration form.\n \nYou will need the reference above to compete the form (4 numbers a space and the post code), you can copy and paste it as it needs to be an identical match.\n \nhttps://fill.boloforms.com/signature/9fd2152e-e490-4351-a4a8-56ab8287595f?p=view.\n \nThank you for your business.");
htmlBody += "<h1 style='font-size: 16.5pt; font-weight: bold; text-decoration; color: #990000: underline'>Header</h1>";
var backgroundArray = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("A1:K").getBackgrounds();
var checkForYellow = backgroundArray.filter(x => x.includes('#ffff00')).length;
if (checkForYellow) {
console.log("Script will not run due to cells in yellow.");
SpreadsheetApp.getActiveSpreadsheet().toast("Script will not run due to cells in yellow."); //toast will be visible in sheets ui
}
else {
console.log("Running the script.");
SpreadsheetApp.getActiveSpreadsheet().toast("Running script."); //toast will be visible in sheets ui
var requestData = { "method": "GET", "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
var shID = getSheetID("S/A - Darrin") //Get Sheet ID of sheet name "Master"
var url = "https://docs.google.com/spreadsheets/d/" + ssID + "/export?format=pdf&id=" + ssID + "&gid=" + shID;
var result = UrlFetchApp.fetch(url, requestData);
var contents = result.getContent();
var bcc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("F6").getDisplayValues().flat().join(",");
var filename = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("S/A - Darrin").getRange("A19").getDisplayValue();
MailApp.sendEmail(emailAddress, subject, htmlBody, { attachments: [result.getBlob().setName(`${filename}.pdf`)], bcc });
You’re currently building your htmlBody as plain text, so Gmail is treating it as just text and ignoring your inline styles.
To make the stockNumber bold and red, wrap it in an HTML element (<span>) with inline styles, and pass it to MailApp.sendEmail() as the htmlBody parameter, not the plain text body.
Here’s a minimal working example based on your code:
var stockNumber = ss.getRange("B7").getValue();
var styledStockNumber = "<span style='color:red; font-weight:bold;'>" + stockNumber + "</span>";
var htmlBody =
styledStockNumber + " (Your Reference)<br><br>" +
"Please click the below link and complete the declaration form.<br><br>" +
"You will need the reference above to complete the form (4 numbers a space and the post code), " +
"you can copy and paste it as it needs to be an identical match.<br><br>" +
"<a href='https://fill.boloforms.com/signature/9fd2152e-e490-4351-a4a8-56ab8287595f?p=view'>Open the form</a><br><br>" +
"Thank you for your business.";
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: htmlBody,
attachments: [result.getBlob().setName(`${filename}.pdf`)],
bcc: bcc
});