简体   繁体   中英

How to use a function output as a variable in GAS and not run the function each time?

I'm new to Javascript/Coding in general and am trying to solve a problem.

I have a program that pulls all my contact emails into an array then sorts through them and adds new emails. Pulling all the contacts takes too long (2000 contacts!) and I want to have Google Apps Script run this part automatically every 5 minutes to I have an updated list if I want to run the sort function.

Is it possible to make the contact pulling part of my function it's own function then use logger.log to save the values for use later? How can a seperate function access the logged info from this new function?

function getEmailAddressList(){

var addrA = [];


 var contact = ContactsApp.getContacts();      

 //////////

  for(var i = 0; i < contact.length; i++){     

    var addresses = contact[i].getEmails();     

   for(var j = 0;j < addresses.length; j++){                            

      var address = addresses[j].getAddress();            

     addrA.push(address); 

     logger.log(addrA);      
    }; 


};

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);      
    }
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM