Javascript 从ContactsPP服务获取联系人,以便使用气体自动完成
如果我必须使用JQuery autocomplete multiple来获取emailsemail而不是电话或其他详细信息,那么如何使用apps脚本编写getContacts代码 它应该以这样一种方式返回:如果一个人有两个用一个名字保存的电子邮件ID,那么必须将其添加到返回的JSON数组中 例如,如果PERSON.apera1@gmail.com和pera1@yahoo.com那么名单必须是Javascript 从ContactsPP服务获取联系人,以便使用气体自动完成,javascript,jquery,jquery-ui,google-apps-script,google-contacts-api,Javascript,Jquery,Jquery Ui,Google Apps Script,Google Contacts Api,如果我必须使用JQuery autocomplete multiple来获取emailsemail而不是电话或其他详细信息,那么如何使用apps脚本编写getContacts代码 它应该以这样一种方式返回:如果一个人有两个用一个名字保存的电子邮件ID,那么必须将其添加到返回的JSON数组中 例如,如果PERSON.apera1@gmail.com和pera1@yahoo.com那么名单必须是 var emails = [ "PERSON.A" <pera1@gmail.com&
var emails = [
"PERSON.A" <pera1@gmail.com>,
"PERSON.A" <pera1@yahoo.com>,
...
];
假设您获得了一系列符合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)));
}
我喜欢添加一些控件,并确保格式是特定的
工作示例:
HTML
JavaScript
我开始试着只找到名字,并用它作为标签。然后我看到你无法辨别正确的接触。所以我把它改为抓取名字和电子邮件来使用
如果您需要,请使用从谷歌返回的数据的更完整示例更新您的帖子,并且应该很容易更新。或者如果您需要多个实例:不确定。必须进行调查。我想你可以,太棒了。我非常感谢您的帮助,我已经在JSFDDL中加载了大约615个联系人。第一次输入时速度会很快,第二次输入时有点慢,但速度很快,随着输入速度的降低。有什么方法可以提高性能吗?提示:数组已预加载615 countstack请看这里:这将查找一个名称。我还建议将minLength:0更改为minLength:2,以确保用户输入的字符数超过1个。这将有助于减少结果数量并提高脚本的响应能力。
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
response($.ui.autocomplete.filter(
addressBook, extractLast(request.term)));
}
<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>
// 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;
}
});
});