TUTORIAL Synchronizing Asteridex and Google Contacts

bobkoure

Member
Joined
May 22, 2013
Messages
173
Reaction score
20
[edit]
This is not a how-to; I'd originally posted this tagged as a question, and now don't see a way to fix it back. It is a "how to get your google contacts that have phones listed into a file on google drive and how to share that file. It is not a way to import that file's contents into Asteridex. That's why it was a question. Sorry for any confusion.
[/edit]

I've been looking for a way to synchronize Asteridex and Google Contacts; just one way Google to Asteridex, and only the contacts that have at least one phone number.
I've written Google Script to parse through all contacts, and list the ones with phone numbers in a CSV file on Google Drive.
Google scripts can be set to run periodically, and files on Google Drive can be shared, in that you then have a URL pointing to the file.
I have not written, but can write, another script that just lists new/changed contacts.
So... now I'm looking for some kind of howto for importing contacts into Asteridex. It's in MySQL? Anyone have an import/export cmd line script I might look at? I'm new to this and am not even really sure how to use Asteridex.
Thanks!


BTW, current iteration of that script appended below. To use, go to https://script.google.com , select "Create Blank Project" and paste it in. "Save", then "Run", pick 'main' (you'll need to do this twice, first time gets all the permissions needed).
To make the CSV file available, go to Google Drive, right-click the file, pick 'Share", you'll get a popup with the URI.

Code:
function main(){
var filename = 'GooglePhonelist.txt';
var found = DocsList.find(filename);
if( found.length > 0) {
    var file = found[0];
  } else {
    var file = DocsList.createFile(filename,'');
  }
 
file.clear();
 
var list = getPhoneContactsAsCSV();
file.append(list);
}
 
 
function getPhoneContactsAsCSV() {
  var header = '"Name","Home Phone","Work Phone","Mobile Phone"\r\n';
  var entries = new Array();
  // get all contacts
  var contacts = ContactsApp.getContacts();
  // for each contact
  for (var i=0; i<contacts.length; i++){
    var contact = contacts[i];
    var fullName = contact.getFullName();
 
    // for each phone number
    var phones = contact.getPhones();
    if(phones.length > 0) {
      var homePhone = '';
      var workPhone = '';
      var mobilePhone = '';
      for (var p=0; p<phones.length; p++){
          var phoneNum = phones[p].getPhoneNumber();
          var label = phones[p].getLabel();
          if (label == 'HOME_PHONE') homePhone = '"' + phoneNum + '"';
          if (label == 'WORK_PHONE') workPhone = '"' + phoneNum + '"';
          if (label == 'MOBILE_PHONE') mobilePhone = '"' + phoneNum + '"';
      }
      csvline = '"' + fullName + '",' + homePhone + ',' + workPhone + ',' + mobilePhone + '\r\n';
      entries.push(csvline);
    }
  }
  entries.sort();
  joined = entries.join('');
  phoneCSV = header + joined;
 
  return(phoneCSV);
}
 
// APIs used:
// https://developers.google.com/apps-script/reference/docs-list/file
// https://developers.google.com/apps-script/reference/contacts/
 

Bham Tony

New Member
Joined
Oct 4, 2015
Messages
21
Reaction score
2
This is by no means meant to be an improvement, just slightly modified. I use the output in phpMyAdmin / astridex / user1 / Import I drop the header since I'm not using it there and I just use the label thing to modify the fullName so if John Doe has multiple numbers they will all go in their own entry. I used this as a one-off export / import, not for maintaining a synced list.
Code:
function main(){
var filename = 'GooglePhonelist.txt';
var found = DriveApp.getFilesByName(filename);
if( found.length > 0) {
    var file = found[0];
  } else {
    var file = DriveApp.createFile(filename,'');
  }
 
//file.clear();
 
var list = getPhoneContactsAsCSV();
file.setContent(list);
}
 
 
function getPhoneContactsAsCSV() {
  var header = '"Name","Home Phone","Work Phone","Mobile Phone"\r\n';
  var entries = new Array();
  // get all contacts
  var contacts = ContactsApp.getContacts();
  // for each contact
  for (var i=0; i<contacts.length; i++){
    var contact = contacts[i];
    var fullName = contact.getFullName();
 
    // for each phone number
    var phones = contact.getPhones();
    if(phones.length > 0) {
      var homePhone = '';
      var workPhone = '';
      var mobilePhone = '';
      for (var p=0; p<phones.length; p++){
          var phoneNum = phones[p].getPhoneNumber();
          var label = phones[p].getLabel();
        if (phoneNum.length >0)
        {
          fullName = fullName.replace(' Home','').replace(' Mobile','').replace(' Work','');
         
          if (label == 'HOME_PHONE')
            fullName = fullName+" Home";
          else if (label == 'WORK_PHONE')
            fullName = fullName+" Work";
          else if (label == 'MOBILE_PHONE')
            fullName = fullName+" Mobile";
         
           
         
          homePhone = '"' + phoneNum.replace(' ','').replace('-','').replace('(','').replace(')','').replace('+','').replace(/^1+/, '') + '"';
          csvline = ',"' + fullName + '","*",' + homePhone + ',' + workPhone + ',' + mobilePhone + '\r\n';
          if (homePhone.length ==12)//if it's a number push it
              entries.push(csvline);
        }
      }
      //csvline = ',"' + fullName + '",' + homePhone + ',,\r\n';
     
    }
  }
  entries.sort();
  joined = entries.join('');
  phoneCSV = joined;//No header required for import
 
  return(phoneCSV);
}
 
// APIs used:
// https://developers.google.com/apps-script/reference/docs-list/file
// https://developers.google.com/apps-script/reference/contacts/
 
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top