0

JQueryオートコンプリート「複数」を使用して電子メールを取得する必要がある場合は、アプリケーションスクリプトを使用してgetContacts()をコーディングするにはどうすればよいですか?ContactsAppから連絡先を取得する

enter image description here

それは、人は、単一の名前で保存された2つの電子メールIDを持っている場合、それはJSON配列を返すに追加する必要がありますように返す必要があります。

例PERSON.Aが[email protected][email protected]を持っているならば、リストには、電子メールの配列を取得と仮定

var emails = [ 
     "PERSON.A" <[email protected]>, 
     "PERSON.A" <[email protected]>, 
... 
    ]; 

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

なければなりませんRFC準拠のアドレスであれば、それを選ぶことができます。例えば:私は少しコントロールを追加し、フォーマットを確保したいと

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

:次のようになります。この

'Homer Simpson' [email protected] 
'Homer Simpson' [email protected] 

基礎:hを入力

addressBook = [ 
    "'Homer Simpson' [email protected]", 
    "'Homer Simpson' [email protected]", 
    "'Marge Simpson' [email protected]", 
    "'Bart Simpson' [email protected]", 
    "'Bart Simpson' [email protected]", 
    "'Lisa Simpson' [email protected]", 
    "'Maggie Simpson' [email protected]" 
]; 

は2つの結果を返す必要があります具体的です。

の作業例: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' [email protected]", 
    "'Homer Simpson' [email protected]", 
    "'Marge Simpson' [email protected]", 
    "'Bart Simpson' [email protected]", 
    "'Bart Simpson' [email protected]", 
    "'Lisa Simpson' [email protected]", 
    "'Maggie Simpson' [email protected]" 
]; 

$(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から返されたデータのより完全な例を投稿に追加してください。更新するのは簡単です。

+1

あなたは空想を取得したい場合:https://jsfiddle.net/Twisty/hvLdp11j/6/ – Twisty

+1

それとも、複数のインスタンスが必要な場合:https://jsfiddle.net/Twisty/hvLdp11j/9/ – Twisty

+0

わかりません。調査しなければならないだろうか。あなたは容疑者にすることができます。 – Twisty

関連する問題