我正在尝试使用脚本编辑器从谷歌表格中获取一些文本以发送电子邮件。该文本包含一个 Unicode 表情符号,但是,当发送电子邮件时,它会打印纯文本,而不是显示 Unicode 表情符号。\
我在电子邮件中看到的内容:⚡ some text here ⚡
我希望在电子邮件中看到的内容:
'⚡这里有一些文字⚡'
我在谷歌表格中保存的文本:⚡ some text here ⚡
我用来从谷歌表格中获取文本的脚本。var emailText = myTemplate.getRange(x, 9).getValue();
我在这里做错了什么?
最佳答案
⚡ 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 字符的示例脚本。在这种情况下,
🤖
( 🤖
) 使用 Unicode 8.0。在运行脚本之前,请输入 🤖 some text here 🤖
到事件工作表的单元格“A1”。示例脚本:
在此脚本中,
🤖
转换为 🤖
使用 String.fromCodePoint()
而不是 String.fromCharCode()
.遗憾的是,在现阶段,Google Apps Script 无法直接使用此方法。所以使用了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 4.0 和 🤖
Unicode 8.0。 笔记:
unescape()
也可用于上述情况。但是已经知道这已被弃用。所以我没有提出使用这个的示例脚本。 引用:
如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
关于javascript - 使用脚本编辑器将谷歌表格中的表情符号 unicode 插入电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57847697/