使用脚本编辑器将Google表格中的Emoji unicode插入电子邮件中

时间:2019-09-09 03:56:39

标签: javascript email google-apps-script google-sheets

我正在尝试使用脚本编辑器从Google表格中获取一些文本来发送电子邮件。文本包含表情符号unicode,但是,在发送电子邮件时,它会打印纯文本而不显示unicode表情符号。\

我在电子邮件中看到的内容:

&#9889 some text here &#9889

我想在电子邮件中看到的内容:

'⚡这里有一些文字⚡'

我在Google表格中保存的文本:

⚡ some text here ⚡

我用来从Google表格中获取文字的脚本。

var emailText = myTemplate.getRange(x, 9).getValue();

我在这里做什么错了?

1 个答案:

答案 0 :(得分:2)

  • 电子表格的单元格中有⚡ some text here ⚡的文本。
  • 您要通过从电子表格中检索文本并从⚡ some text here ⚡解码为⚡ some text here ⚡来发送电子邮件。
  • 您想使用Google Apps脚本实现这一目标。

如果我的理解正确,那么这个答案如何?请认为这只是几个答案之一。

问题:

在单元格中有⚡ some text here ⚡文本的情况下,当使用setValue()setValues()从单元格中检索值时,⚡ some text here ⚡的检索方式为文字值。这样,当将此值作为电子邮件发送时,⚡被用作文本。因此,⚡需要解码为

解决方案:

在这里,作为几种解决方案之一,我使用以下流程将⚡转换为。作为示例,假设⚡ some text here ⚡放在单元格“ A1”中。

  1. 从电子表格的单元格中获取值。
  2. 9889检索⚡
  3. 9889解码String.fromCharCode()的字符代码。
    • 这样,9889被转换为
  4. 将解码后的文本作为电子邮件发送。

模式1:

在这种模式下,通过从⚡转换为发送电子邮件。在运行脚本之前,请将⚡ 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的字符。请注意这一点。

模式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(); // &#129302;  some text here &#129302;
  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()也可以用于上述情况。但是,已经知道此方法已被弃用。因此,我没有提出使用该脚本的示例脚本。

参考:

如果我误解了您的问题,而这不是您想要的结果,我深表歉意。