我正在尝试使用脚本编辑器从Google表格中获取一些文本来发送电子邮件。文本包含表情符号unicode,但是,在发送电子邮件时,它会打印纯文本而不显示unicode表情符号。\
我在电子邮件中看到的内容:
⚡ some text here ⚡
我想在电子邮件中看到的内容:
'⚡这里有一些文字⚡'
我在Google表格中保存的文本:
⚡ some text here ⚡
我用来从Google表格中获取文字的脚本。
var emailText = myTemplate.getRange(x, 9).getValue();
我在这里做什么错了?
答案 0 :(得分:2)
⚡ some text here ⚡
的文本。⚡ some text here ⚡
解码为⚡ some text here ⚡
来发送电子邮件。如果我的理解正确,那么这个答案如何?请认为这只是几个答案之一。
在单元格中有⚡ some text here ⚡
文本的情况下,当使用setValue()
和setValues()
从单元格中检索值时,⚡ some text here ⚡
的检索方式为文字值。这样,当将此值作为电子邮件发送时,⚡
被用作文本。因此,⚡
需要解码为⚡
。
在这里,作为几种解决方案之一,我使用以下流程将⚡
转换为⚡
。作为示例,假设⚡ some text here ⚡
放在单元格“ A1”中。
9889
检索⚡
。9889
解码String.fromCharCode()
的字符代码。
9889
被转换为⚡
。在这种模式下,通过从⚡
转换为⚡
发送电子邮件。在运行脚本之前,请将⚡ some text here ⚡
放在活动工作表的单元格“ A1”中。
在此脚本中,使用⚡
将⚡
转换为String.fromCharCode()
。 Google Apps脚本可以使用此方法。
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);
在⚡
的情况下,可以同时使用GmailApp.sendEmail()
和MailApp.sendEmail()
发送,因为版本是4.0。但是,如果您要使用其他较新版本的unicode,则建议使用MailApp.sendEmail()
。
在此示例脚本中,可以使用小于Unicode 5.2的字符。请注意这一点。
在这种模式下,我想提出一个示例脚本,以使用较新版本的unicode字符。在这种情况下,将使用Unicode 8.0的🤖
(?
)。在运行脚本之前,请将🤖 some text here 🤖
放在活动工作表的单元格“ A1”中。
在此脚本中,使用🤖
而非?
将String.fromCodePoint()
转换为String.fromCharCode()
。不幸的是,在当前阶段,Google Apps脚本无法直接使用此方法。因此,使用了polyfill。使用此功能时,请运行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);
}
⚡
和Unicode 8.0的?
。unescape()
也可以用于上述情况。但是,已经知道此方法已被弃用。因此,我没有提出使用该脚本的示例脚本。如果我误解了您的问题,而这不是您想要的结果,我深表歉意。