从ContactsApp Service获取联系人以使用GAS自动完成

时间:2017-03-28 10:03:00

标签: javascript jquery jquery-ui google-apps-script google-contacts

如果我必须使用JQuery自动完成"多个"来获取电子邮件(仅限电子邮件而非电话或其他详细信息),如何使用apps脚本编写getContacts()代码。

enter image description here

它应该以这样的方式返回:如果一个人在一个名称下保存了2个电子邮件ID,则必须将其添加到返回的JSON数组中。

例如,如果PERSON.A有pera1@gmail.com和pera1@yahoo.com,那么列表必须是

var emails = [
      "PERSON.A" <pera1@gmail.com>,
      "PERSON.A" <pera1@yahoo.com>,
...
    ];

的javascript

emails = google.script.run.getContacts();

$( function() {
    var availableTags = emails;
    function split( val ) {
      return val.split( /,\s*/ );
    }
    function extractLast( term ) {
      return split( term ).pop();
    }

    $( "#recipients" )
      // don't navigate away from the field on tab when selecting an item
      .on( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).autocomplete( "instance" ).menu.active ) {
          event.preventDefault();
        }
      })
      .autocomplete({
        minLength: 0,
        source: function( request, response ) {
          // delegate back to autocomplete, but extract the last term
          response( $.ui.autocomplete.filter(
            availableTags, extractLast( request.term ) ) );
        },
        focus: function() {
          // prevent value inserted on focus
          return false;
        },
        select: function( event, ui ) {
          var terms = split( this.value );
          // remove the current input
          terms.pop();
          // add the selected item
          terms.push( ui.item.value );
          // add placeholder to get the comma-and-space at the end
          terms.push( "" );
          this.value = terms.join( ", " );
          return false;
        }
      });
  } );

1 个答案:

答案 0 :(得分:1)

假设您获得了一系列符合RFC标准的电子邮件地址,您可以将它们分开。例如:

addressBook = [
  "'Homer Simpson' hsimpson@fox.net",
  "'Homer Simpson' h.j.simpson@snpp.com",
  "'Marge Simpson' msimpson@fox.net",
  "'Bart Simpson' bsimpson@fox.net",
  "'Bart Simpson' i.c.weiner@fox.net",
  "'Lisa Simpson' hsimpson@fox.net",
  "'Maggie Simpson' maggie.simpson@fox.net"
];

键入h应返回2个结果:

'Homer Simpson' hsimpson@fox.net
'Homer Simpson' hsimpson@theplant.com

基本知识如下:

source: function(request, response) {
  // delegate back to autocomplete, but extract the last term
  response($.ui.autocomplete.filter(
  addressBook, extractLast(request.term)));
}

我想添加一点控件并确保格式是特定的。

工作示例:https://jsfiddle.net/Twisty/hvLdp11j/3/

<强> HTML

<div class="ui-widget">
  <div class="ui-widget-header ui-corner-top center">
    Select Recipients
  </div>
  <div class="ui-widget-content ui-corner-bottom">
    <input type="text" id="emails" />
  </div>
</div>

<强>的JavaScript

// Base code: http://jqueryui.com/autocomplete/#multiple
// Data example to mimic https://developers.google.com/apps-script/reference/contacts/contacts-app#getcontacts
var addressBook = [
  "'Homer Simpson' hsimpson@fox.net",
  "'Homer Simpson' h.j.simpson@snpp.com",
  "'Marge Simpson' msimpson@fox.net",
  "'Bart Simpson' bsimpson@fox.net",
  "'Bart Simpson' i.c.weiner@fox.net",
  "'Lisa Simpson' hsimpson@fox.net",
  "'Maggie Simpson' maggie.simpson@fox.net"
];

$(function() {
  function split(val) {
    return val.split(/,\s*/);
  }

  function extractLast(term) {
    return split(term).pop();
  }

  function findName(contact) {
    var name;
    var regex = /.(.+). .*/g;
    name = regex.exec(contact);
    return name[1];
  }

  function findNameEmail(contact) {
    var tmp, name, email;
    var regex = /.(.+). (.*)/g;
    tmp = regex.exec(contact);
    name = tmp[1];
    email = tmp[2];
    return name + " <" + email + ">";
  }

  $("#emails")
    // don't navigate away from the field on tab when selecting an item
    .on("keydown", function(event) {
      if (event.keyCode === $.ui.keyCode.TAB &&
        $(this).autocomplete("instance").menu.active) {
        event.preventDefault();
      }
    })
    .autocomplete({
      minLength: 0,
      source: function(request, response) {
        var names = [];
        $.each(addressBook, function(k, v) {
          names.push(findNameEmail(v));
        });
        // delegate back to autocomplete, but extract the last term
        response($.ui.autocomplete.filter(
          names, extractLast(request.term)));
      },
      focus: function() {
        // prevent value inserted on focus
        return false;
      },
      select: function(event, ui) {
        var terms = split(this.value);
        // remove the current input
        terms.pop();
        // add the selected item
        terms.push(ui.item.value);
        // add placeholder to get the comma-and-space at the end
        terms.push("");
        this.value = terms.join(", ");
        return false;
      }
    });
});

我开始尝试找到名称并将其用作标签。然后我看到你无法辨别出正确的联系方式。所以我把它改为抓住要使用的姓名和电子邮件。

如果您需要,请使用Google提供的更完整的数据示例更新您的帖子,并且应该很容易更新。