I'm trying to set a number format for a message in an email (this script is an email sender that takes data from a cell and puts it into a message). I don't really know how to code, but I've managed to put this together and get it to work well, so that's why my mistake might seem incredibly obvious.
Here's the working script without my attempt at number formatting:
function sendEmails() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Emails'); // change Sheet1 to the name of your sheet
const data = sh.getRange(2, 1, sh.getLastRow() - 1, 5).getValues();
data.forEach((r, i) => {
let sendemailValue = r[1];
if (sendemailValue === 'X') {
let lrow = sh.getLastRow();
let name = r[0];
let amount = r[2];
let item = r[3];
let emailAddress = r[4]; //this is column 5
let message =
'Hello ' + name + ', your receipt is ' + amount + ' for your ' + item;
let subject = 'Your Receipt.';
if (emailAddress) {
MailApp.sendEmail(emailAddress, subject, message);
} else {
console.log(`Row: ${i + 2}`); //will let you know what row it's failing on
}
}
});
}
And here is my attempt which failed:
function sendEmails() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Emails'); // change Sheet1 to the name of your sheet
const data = sh.getRange(2, 1, sh.getLastRow() - 1, 5).getValues();
data.forEach((r, i) => {
let sendemailValue = r[1];
if (sendemailValue === 'X') {
let lrow = sh.getLastRow();
let name = r[0];
let amount = r[2];
amount.setNumberFormat('$#,##0.00;$(#,##0.00)');
let item = r[3];
let emailAddress = r[4]; //this is column 5
let message =
'Hello ' + name + ', your receipt is $' + amount + ' for your ' + item;
let subject = 'Your Receipt.';
if (emailAddress) {
MailApp.sendEmail(emailAddress, subject, message);
} else {
console.log(`Row: ${i + 2}`); //will let you know what row it's failing on
}
}
});
}
The change is that I added amount.setNumberFormat("$#,##0.00;$(#,##0.00)"), which doesn't work.
The error I get is:
"TypeError: amount.setNumberFormat is not a function".