Google Apps脚本:将所有Google联系人导出为CSV [英] Google Apps Script: Export all Google contacts as CSV
问题描述
我刚刚开始学习Google Apps脚本来解决一项实际任务.我想编写一个脚本,将我的Google帐户中的所有联系人定期导出到CSV文件中,并将其发送到预定义的电子邮件地址.如果我的联系人列表中会发生数据丢失,这应该是一种自动版本备份.
I have just started to learn Google Apps Script to solve one practical task. I'd like to write a script that periodically exports all contacts in my Google account into a CSV file and sends it to a predefined email address. This should be a kind of automatic versioned backup if data loss will occur in my contact list.
实际上,我想做的是在脚本中使用Google联系人的网络界面中可用的本机导出功能(更多>导出).我浏览了Google API,但是我无法在Google API中找到满足我需要的服务或对象.可能吗?
Actually what I am trying to do is to use the native export functionality available in the web interface of the Google contacts (More > Export) in my script. I skimmed through the Google API, but I could not find a service or object in the Google API that does what I need. Is it possible at all?
推荐答案
以下是我整理的内容,可将联系人中的一些基本字段转换为可能是csv输出的内容.您可能想要添加更多字段,并可能使用不同的定界符.
Here's something I threw together to get a few of the basic fields in your contacts into something that could be a csv output. You may want to add more fields and perhaps use different delimiters.
function getAllContacts() {
var contactsA=ContactsApp.getContacts();
var s='';
var br='<br />';//line delimiter change to linefeed when not using html dialog
var dlm=' ~~~ ';//field delimiter
for(var i=0;i<contactsA.length;i++)
{
s+=Utilities.formatString('<br />\'%s\',\'%s\',\'%s\',\'%s\',\'%s\'%s',
(typeof(contactsA[i].getFullName())!='undefined')?contactsA[i].getFullName():'',
(typeof(contactsA[i].getAddresses().map(function (v,i,A) { return A[i].getAddress();}))!='undefined')?contactsA[i].getAddresses().map(function (v,i,A) { return A[i].getAddress();}).join(dlm):'',
(typeof(contactsA[i].getEmails().map(function(v,i,A) {return A[i].getAddress();}))!='undefined')?contactsA[i].getEmails().map(function(cV,i,A) {return A[i].getAddress();}).join(dlm):'',
(typeof(contactsA[i].getPhones().map(function(v,i,A){return A[i].getPhoneNumber();}))!='undefined')?contactsA[i].getPhones().map(function(v,i,A){return A[i].getPhoneNumber();}).join(dlm):'',
(typeof(contactsA[i].getCompanies().map(function(v,i,A){return A[i].getCompanyName();}))!='undefined')?contactsA[i].getCompanies().map(function(v,i,A){return A[i].getCompanyName();}).join(dlm):'',br);
}
var ui=HtmlService.createHtmlOutput(s).setWidth(800) ;
SpreadsheetApp.getUi().showModelessDialog(ui, 'Contacts')
}
这篇关于Google Apps脚本:将所有Google联系人导出为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!