使用脚本编辑器将表情符号 unicode 从 Google 表格插入到电子邮件中

2024-01-30

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

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

&#9889 some text here &#9889

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

“⚡这里有一些文字⚡”

我在谷歌表格中保存的文本:

⚡ some text here ⚡

我用来从谷歌表格中获取文本的脚本。

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

我在这里做错了什么?


  • 有一段文字是⚡ some text here ⚡在电子表格的单元格中。
  • 您想要通过从电子表格检索文本并解码来发送电子邮件⚡ some text here ⚡ to ⚡ some text here ⚡.
  • 您希望使用 Google Apps 脚本来实现此目的。

如果我的理解是正确的,这个答案怎么样?请将此视为多个答案之一。

Issue:

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

解决方案:

在这里,作为几种解决方案之一,我从⚡ to 使用以下流程。作为一个示例案例,它假设⚡ some text here ⚡正在放入单元格“A1”。

  1. 从电子表格的单元格中检索值。
  2. 取回9889 from ⚡.
  3. Decode the character code of 9889 with String.fromCharCode().
    • 这样,9889被转换为.
  4. 将解码后的文本作为电子邮件发送。

模式一:

在此模式中,通过转换来发送电子邮件⚡ to 。在运行脚本之前,请输入⚡ some text here ⚡到活动工作表的单元格“A1”。

示例脚本:

在这个脚本中,⚡被转换为 using 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() and MailApp.sendEmail(),因为版本是4.0。但如果你想使用其他较新版本的unicode,我建议使用MailApp.sendEmail().

    • 这已经被提到过西纳拉赫内巴的评论 https://stackoverflow.com/questions/57847697/insert-emoji-unicode-from-google-sheets-to-an-email-using-script-editor#comment102122358_57847697.
  • 在此示例脚本中,可以使用小于 Unicode 5.2 的字符。请小心这一点。

模式2:

在这种模式中,我想提出使用新版本的 unicode 字符的示例脚本。在这种情况下,🤖(????使用 Unicode 8.0 的 )。在运行脚本之前,请输入🤖 some text here 🤖到活动工作表的单元格“A1”。

示例脚本:

在这个脚本中,🤖被转换为???? using 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(); // &#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 和????统一码 8.0。

Note:

  • 我不确定你的整个电子表格和脚本。所以我提出了上面的示例脚本。此解决方法的重点是方法论。所以请根据您的实际情况修改脚本。
  • unescape()也可用于上述情况。但众所周知,这已被弃用。所以我没有提出使用它的示例脚本。

参考:

  • String.fromCharCode() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
  • String.fromCodePoint() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
  • 逃逸() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用脚本编辑器将表情符号 unicode 从 Google 表格插入到电子邮件中 的相关文章