问题
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?
回答1:
- There is a text of
⚡ some text here ⚡
in a cell of the Spreadsheet. - You want to send an email by retrieving the text from Spreadsheet and decoding from
⚡ some text here ⚡
to⚡ some text here ⚡
. - You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several answers.
Issue:
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 ⚡
.
Solution:
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".
- Retrieve the value from the cell of Spreadsheet.
- Retrieve
9889
from⚡
. - Decode the character code of
9889
withString.fromCharCode()
.- By this,
9889
is converted to⚡
.
- By this,
- Send the decoded text as an email.
Pattern 1:
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.
Sample script:
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 bothGmailApp.sendEmail()
andMailApp.sendEmail()
, because the version is 4.0. But if you want to use the unicode of other newer versions, I recommend to useMailApp.sendEmail()
.- This has already been mentioned by sinaraheneba's comment.
In this sample script, the characters less than Unicode 5.2 can be used. Please be careful this.
Pattern 2:
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.
Sample script:
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 course, this sample script can be used for both
⚡
of Unicode 4.0 and🤖
of Unicode 8.0.
Note:
- I'm not sure about your whole Spreadsheet and script. So I proposed above sample scripts. The important point of this workaround is the methodology. So please modify the scripts to your actual situation.
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.
References:
- String.fromCharCode()
- String.fromCodePoint()
- unescape()
If I misunderstood your question and this was not the result you want, I apologize.
来源:https://stackoverflow.com/questions/57847697/insert-emoji-unicode-from-google-sheets-to-an-email-using-script-editor