Depending upon the size of your data, you can store the data in "Properties Service" or "Cache." Properties Service can hold 9k of data per property name and a total of 500k. If the data will ever be larger than that, then you will need a different data storage option. For example, you could store the contact information in a text file in your Google Drive. You could obviously save your data to a database also. The point is, you need to store the contact information somewhere. You can not store it in a global variable, because a global variable will loose it's data as soon as the code stops running. Logger.log()
won't help you in this situation.
You can run code on a time interval with a time based trigger. You will need to set that up from the "Edit" menu and "Current project's triggers."
Here is an example of how you might store the data in a text file:
function getEmailAddressList(){
var addrA,addresses,addressFile,allContacts,
contactsAsJSON,i,j,thisAddress,thisContact;
allContacts = ContactsApp.getContacts();
//////////
addrA = [];
for (i = 0; i < allContacts.length; i++) {
addresses = allContacts[i].getEmails();
for (j = 0;j < addresses.length; j++) {
thisAddress = addresses[j].getAddress();
thisContact = addresses[j].getDisplayName();
addrA.push(thisAddress);
Logger.log(thisContact);
}
}
contactsAsJSON = JSON.stringify(addrA);
try{
addressFile = DriveApp.getFileById('id');
}catch(e){}
if (!addressFile) {
addressFile = DriveApp.createFile('My Contacts', contactsAsJSON);
Logger.log('New file ID: ' + addressFile.getId());//Get the new file ID and hard code into
//the code above
}
addressFile.setContent(contactsAsJSON);
};
Here is an example of how you might store all contacts in Cache Service with each contact being one property name:
function getEmailAddressList(){
var addresses,allContacts,Cache,i,j,thisAddress,thisContact;
Cache = CacheService.getDocumentCache();
allContacts = ContactsApp.getContacts();
//////////
for (i = 0; i < allContacts.length; i++) {
addresses = contact[i].getEmails();
for (j = 0;j < addresses.length; j++) {
thisAddress = addresses[j].getAddress();
thisContact = addresses[j].getDisplayName();
Cache.put(thisContact, thisAddress)
logger.log(thisContact);
}
}
};