I'm trying to get some text out of google sheets using the script editor to send an email. The text contains an emoji unicode, however, when the email is sent it prints the plain text instead of displaying the unicode emoji.\
What I'm seeing in the email:
⚡ some text here ⚡
What I'd like to see in the email:
'⚡ some text here ⚡'
The text I have saved within google sheets:
⚡ some text here ⚡
The script I use to get the text out of google sheets.
var emailText = myTemplate.getRange(x, 9).getValue();
What am I doing wrong here?
⚡ some text here ⚡ in a cell of the Spreadsheet.⚡ some text here ⚡ to ⚡ some text here ⚡.If my understanding is correct, how about this answer? Please think of this as just one of several answers.
Under the situation that there is a text of ⚡ some text here ⚡ in a cell, when the value is retrieved from the cell using setValue() and setValues(), ⚡ some text here ⚡ is retrieved as the text value. By this, when this value is sent as an email, ⚡ is used as the text. So ⚡ is required to be decoded to ⚡.
Here, as one of several solutions, I convert from ⚡ to ⚡ using the following flow. As a sample case, it supposes that ⚡ some text here ⚡ is putting in the cell "A1".
9889 from ⚡.9889 with String.fromCharCode().
9889 is converted to ⚡.In this pattern, an email is sent by converting from ⚡ to ⚡. Before you run the script, please put ⚡ some text here ⚡ to the cell "A1" of the active sheet.
In this script, ⚡ is converted to ⚡ using String.fromCharCode(). This method can be used by Google Apps Script.
var emailAddress = "###";
var sheet = SpreadsheetApp.getActiveSheet();
var value = sheet.getRange("A1").getValue(); // ⚡ some text here ⚡
var converted = value.replace(/&#(\w.+?);/g, function(_, p) {return String.fromCharCode(p)}); // ⚡ some text here ⚡
GmailApp.sendEmail(emailAddress, "sample", converted);
MailApp.sendEmail(emailAddress, "sample", converted);
In the case of ⚡, it can be sent with both GmailApp.sendEmail() and MailApp.sendEmail(), because the version is 4.0. But if you want to use the unicode of other newer versions, I recommend to use MailApp.sendEmail().
In this sample script, the characters less than Unicode 5.2 can be used. Please be careful this.
In this pattern, I would like to propose the sample script for using the characters of the unicode of newer version. In this case, 🤖(🤖) of Unicode 8.0 is used. Before you run the script, please put 🤖 some text here 🤖 to the cell "A1" of the active sheet.
In this script, 🤖 is converted to 🤖 using String.fromCodePoint() instead of String.fromCharCode(). Unfortunately, in the current stage, this method cannot be directly used by Google Apps Script. So the polyfill is used. When you use this, please run myFunction().
/*! http://mths.be/fromcodepoint v0.1.0 by @mathias */
if (!String.fromCodePoint) {
(function() {
var defineProperty = (function() {
// IE 8 only supports `Object.defineProperty` on DOM elements
try {
var object = {};
var $defineProperty = Object.defineProperty;
var result = $defineProperty(object, object, object) && $defineProperty;
} catch(error) {}
return result;
}());
var stringFromCharCode = String.fromCharCode;
var floor = Math.floor;
var fromCodePoint = function() {
var MAX_SIZE = 0x4000;
var codeUnits = [];
var highSurrogate;
var lowSurrogate;
var index = -1;
var length = arguments.length;
if (!length) {
return '';
}
var result = '';
while (++index < length) {
var codePoint = Number(arguments[index]);
if (
!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
codePoint < 0 || // not a valid Unicode code point
codePoint > 0x10FFFF || // not a valid Unicode code point
floor(codePoint) != codePoint // not an integer
) {
throw RangeError('Invalid code point: ' + codePoint);
}
if (codePoint <= 0xFFFF) { // BMP code point
codeUnits.push(codePoint);
} else { // Astral code point; split in surrogate halves
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
codePoint -= 0x10000;
highSurrogate = (codePoint >> 10) + 0xD800;
lowSurrogate = (codePoint % 0x400) + 0xDC00;
codeUnits.push(highSurrogate, lowSurrogate);
}
if (index + 1 == length || codeUnits.length > MAX_SIZE) {
result += stringFromCharCode.apply(null, codeUnits);
codeUnits.length = 0;
}
}
return result;
};
if (defineProperty) {
defineProperty(String, 'fromCodePoint', {
'value': fromCodePoint,
'configurable': true,
'writable': true
});
} else {
String.fromCodePoint = fromCodePoint;
}
}());
}
function myFunction() {
var emailAddress = "###";
var sheet = SpreadsheetApp.getActiveSheet();
var value = sheet.getRange("A1").getValue(); // 🤖 some text here 🤖
var converted = value.replace(/&#(\w.+?);/g, function(_, p) {return String.fromCodePoint(p)}); // 🤖 some text here 🤖
// GmailApp.sendEmail(emailAddress, "sample", converted); // This cannot be used for Unicode 8.0. https://stackoverflow.com/a/50883782/7108653
MailApp.sendEmail(emailAddress, "sample", converted);
}
⚡ of Unicode 4.0 and 🤖 of Unicode 8.0.unescape() can be also used for above situations. But it has already been known that this is deprecated. So I didn't propose the sample script for using this.If I misunderstood your question and this was not the result you want, I apologize.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With