Return 公司名称,未定义 Phone 来自 Google ContactsApp 的号码和标签组
Return Company Name, undefined Phone Number and Label Group from Google ContactsApp
我是 Google 脚本的新手,我从这个论坛找到了非常有用的信息(谢谢!!)。我正在尝试创建脚本以将 Google 联系人应用程序导出到 Sheet。我能够从下面的脚本中成功获取全名和电子邮件,但它只是 returning“公司字段”、“Phone 字段”和“联系人组”。我已经尝试了几种代码变体来尝试 return 公司的实际名称,Phone 号码和组(如果填充),但它只能 return 那些 header 值。在这方面的任何帮助都会很棒!
这是我的代码:
function getName() {
var contacts = ContactsApp.getContacts(), output = [];
var sheet = SpreadsheetApp.getActiveSheet();
// Create a header record
var header = [["Full Name", "Company", "Email", "Phone Number", "Tags"]]
var range = sheet.getRange(1,1,1,5);
range.setValues(header);
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
//Get Full Name
var fullname = contacts[i].getFullName();
if(fullname) {
output.push([fullname])
}
SpreadsheetApp.getActiveSheet().getRange(2, 1, output.length, 1).setValues(output);
// Gets Company Name Address
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var company = contacts[i].getCompanies();
if(company) {
output.push([company]);
}
} SpreadsheetApp.getActiveSheet().getRange(2, 2, output.length, 1).setValues(output);
// Gets Email Address
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var email = contacts[i].getEmailAddresses();
if(email) {
output.push([email]);
}
} SpreadsheetApp.getActiveSheet().getRange(2, 3, output.length, 1).setValues(output);
// Gets Phone Number
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var phone = contacts[i].getPhones();
if(phone) {
output.push([phone]);
}
}
SpreadsheetApp.getActiveSheet().getRange(2, 4, output.length, 1).setValues(output);
// Gets Tags
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var tags = contacts[i].getContactGroups();
if(tags) {
output.push([tags]);
}
} SpreadsheetApp.getActiveSheet().getRange(2, 5, output.length, 1).setValues(output);
}
方法 getCompanies
、getPhones
、getContactGroups
return 对象数组。您需要访问每个元素的值。
代码:
function getName() {
var contacts = ContactsApp.getContacts();
var sheet = SpreadsheetApp.getActiveSheet();
// Create a header record
var header = [["Full Name", "Company", "Email", "Phone Number", "Tags"]]
sheet.getRange(1, 1, 1, 5).setValues(header);
// Delimiter is set into next line within cell
// Change into comma if you want it delimited by comma
var delimiter = String.fromCharCode(10);
for (var i = 0, iLen = contacts.length; i < iLen; i++) {
// Get last row every loop
var row = sheet.getLastRow();
var fullname = contacts[i].getFullName();
sheet.getRange(row + 1, 1, 1, 1).setValue(fullname);
var company = contacts[i].getCompanies(), companies = [];
for (var j in company)
if (company[j].getCompanyName())
companies.push([company[j].getCompanyName()]);
sheet.getRange(row + 1, 2, 1, 1).setValue(companies.join(delimiter));
var email = contacts[i].getEmails(), emails = [];
for (var j in email)
if (email[j].getAddress())
emails.push([email[j].getAddress()]);
sheet.getRange(row + 1, 3, 1, 1).setValue(emails.join(delimiter));
var phone = contacts[i].getPhones(), phones = [];
for (var j in phone)
if (phone[j].getPhoneNumber())
phones.push([phone[j].getPhoneNumber()]);
sheet.getRange(row + 1, 4, 1, 1).setValue(phones.join(delimiter));
var tags = contacts[i].getContactGroups(), groups = [];
for (var j in tags)
// Default tag in my contacts seems to be "System Group: My Contacts", I did not include them
if (tags[j].getGroupName() && tags[j].getGroupName() != "System Group: My Contacts")
groups.push([tags[j].getGroupName()]);
sheet.getRange(row + 1, 5, 1, 1).setValue(groups.join(delimiter));
}
}
输出:
我是 Google 脚本的新手,我从这个论坛找到了非常有用的信息(谢谢!!)。我正在尝试创建脚本以将 Google 联系人应用程序导出到 Sheet。我能够从下面的脚本中成功获取全名和电子邮件,但它只是 returning“公司字段”、“Phone 字段”和“联系人组”。我已经尝试了几种代码变体来尝试 return 公司的实际名称,Phone 号码和组(如果填充),但它只能 return 那些 header 值。在这方面的任何帮助都会很棒!
这是我的代码:
function getName() {
var contacts = ContactsApp.getContacts(), output = [];
var sheet = SpreadsheetApp.getActiveSheet();
// Create a header record
var header = [["Full Name", "Company", "Email", "Phone Number", "Tags"]]
var range = sheet.getRange(1,1,1,5);
range.setValues(header);
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
//Get Full Name
var fullname = contacts[i].getFullName();
if(fullname) {
output.push([fullname])
}
SpreadsheetApp.getActiveSheet().getRange(2, 1, output.length, 1).setValues(output);
// Gets Company Name Address
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var company = contacts[i].getCompanies();
if(company) {
output.push([company]);
}
} SpreadsheetApp.getActiveSheet().getRange(2, 2, output.length, 1).setValues(output);
// Gets Email Address
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var email = contacts[i].getEmailAddresses();
if(email) {
output.push([email]);
}
} SpreadsheetApp.getActiveSheet().getRange(2, 3, output.length, 1).setValues(output);
// Gets Phone Number
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var phone = contacts[i].getPhones();
if(phone) {
output.push([phone]);
}
}
SpreadsheetApp.getActiveSheet().getRange(2, 4, output.length, 1).setValues(output);
// Gets Tags
var contacts = ContactsApp.getContacts(), output = [];
for(var i = 0, iLen = contacts.length; i < iLen; i++) {
var tags = contacts[i].getContactGroups();
if(tags) {
output.push([tags]);
}
} SpreadsheetApp.getActiveSheet().getRange(2, 5, output.length, 1).setValues(output);
}
方法 getCompanies
、getPhones
、getContactGroups
return 对象数组。您需要访问每个元素的值。
代码:
function getName() {
var contacts = ContactsApp.getContacts();
var sheet = SpreadsheetApp.getActiveSheet();
// Create a header record
var header = [["Full Name", "Company", "Email", "Phone Number", "Tags"]]
sheet.getRange(1, 1, 1, 5).setValues(header);
// Delimiter is set into next line within cell
// Change into comma if you want it delimited by comma
var delimiter = String.fromCharCode(10);
for (var i = 0, iLen = contacts.length; i < iLen; i++) {
// Get last row every loop
var row = sheet.getLastRow();
var fullname = contacts[i].getFullName();
sheet.getRange(row + 1, 1, 1, 1).setValue(fullname);
var company = contacts[i].getCompanies(), companies = [];
for (var j in company)
if (company[j].getCompanyName())
companies.push([company[j].getCompanyName()]);
sheet.getRange(row + 1, 2, 1, 1).setValue(companies.join(delimiter));
var email = contacts[i].getEmails(), emails = [];
for (var j in email)
if (email[j].getAddress())
emails.push([email[j].getAddress()]);
sheet.getRange(row + 1, 3, 1, 1).setValue(emails.join(delimiter));
var phone = contacts[i].getPhones(), phones = [];
for (var j in phone)
if (phone[j].getPhoneNumber())
phones.push([phone[j].getPhoneNumber()]);
sheet.getRange(row + 1, 4, 1, 1).setValue(phones.join(delimiter));
var tags = contacts[i].getContactGroups(), groups = [];
for (var j in tags)
// Default tag in my contacts seems to be "System Group: My Contacts", I did not include them
if (tags[j].getGroupName() && tags[j].getGroupName() != "System Group: My Contacts")
groups.push([tags[j].getGroupName()]);
sheet.getRange(row + 1, 5, 1, 1).setValue(groups.join(delimiter));
}
}