javascript - Getting contacts from ContactsApp Service for autocomplete using GAS -
how code getcontacts() using apps script if i'll have use jquery autocomplete "multiple", fetch emails(email not phone or other details).
it should return in such way if person has 2 email ids saved under single name, has added returning json array.
example if person.a has pera1@gmail.com , pera1@yahoo.com list has be
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 field on tab when selecting item .on( "keydown", function( event ) { if ( event.keycode === $.ui.keycode.tab && $( ).autocomplete( "instance" ).menu.active ) { event.preventdefault(); } }) .autocomplete({ minlength: 0, source: function( request, response ) { // delegate autocomplete, extract 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 current input terms.pop(); // add selected item terms.push( ui.item.value ); // add placeholder comma-and-space @ end terms.push( "" ); this.value = terms.join( ", " ); return false; } }); } );
assuming array of email addresses rfc compliant, can pick them apart. example:
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" ];
typing h
should return 2 results:
'homer simpson' hsimpson@fox.net 'homer simpson' hsimpson@theplant.com
the basics of like:
source: function(request, response) { // delegate autocomplete, extract last term response($.ui.autocomplete.filter( addressbook, extractlast(request.term))); }
i add little control , ensure format specific.
working example: 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 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 field on tab when selecting 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 autocomplete, extract 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 current input terms.pop(); // add selected item terms.push(ui.item.value); // add placeholder comma-and-space @ end terms.push(""); this.value = terms.join(", "); return false; } }); });
i started trying find name , using label. saw not discern right contact. switched grabbing name , email use.
if need, update post more complete example of data being returned google, , should easy update.
Comments
Post a Comment