2

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.

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?

TecMan
  • 2,743
  • 2
  • 30
  • 64
  • Looks to me like everything you will need is right [here](https://developers.google.com/apps-script/reference/contacts/contacts-app) I think you will have to make the csv list and save it in a file yourself. But my guess is that it's fairly simple things to do. – Cooper Sep 08 '17 at 19:47
  • You can probably get what you need at Gmail Contacts the old version. Click the MORE and you'll see the export. I think this is the [link](https://www.google.com/contacts/u/0/?cplus=1#contacts). – Cooper Sep 09 '17 at 05:35
  • @Cooper, did you understand me? I need to do this **programmatically** from a GAS script. – TecMan Sep 09 '17 at 05:46
  • I don't think I did but I do now – Cooper Sep 09 '17 at 07:00

1 Answers1

2

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')
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • I should have be more clear in my request. I need a CSV file with all data about my contacts I can use later to import into my Google account to restore all my contacts in the case of data loss. 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). I will add this requirement to the question now. – TecMan Sep 09 '17 at 05:28
  • @TecMan your requirements maybe slightly different in format, but this is a good start for you to develop your own implementation. You might consider outputting to a Google Sheet rather than emailing a CSV. *Have a go and update your question if you get stuck.* – JSDBroughton Sep 13 '17 at 13:05
  • What seems to be the problem? Did you get any errors? – Cooper Dec 15 '20 at 17:32
  • You're right but I don't know why. I'll work on it and try to figure it out – Cooper Dec 15 '20 at 18:46
  • Actually I've got it to run in some spreadsheets but not others. The one where it's not running is where I've manually entered scopes. – Cooper Dec 15 '20 at 19:27
  • The places where it wasn't running were Cloud Platform Projects and I found out that I needed to enable the Contacts API – Cooper Dec 17 '20 at 18:33