I am trying to upgrade the code below with the ability to accept an array input.
const md5 = (key = '') => {
const code = key.toLowerCase().replace(/\s/g, '');
return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, key)
.map((char) => (char + 256).toString(16).slice(-2))
.join('');
};
const getCache = (key) => {
return CacheService.getDocumentCache().get(md5(key));
};
// Store the results for 6 hours
const setCache = (key, value) => {
const expirationInSeconds = 6 * 60 * 60;
CacheService.getDocumentCache().put(md5(key), value, expirationInSeconds);
};
const GOOGLEMAPS_DISTANCE = (origin, destination, mode = 'driving') => {
const key = ['distance', origin, destination, mode].join(',');
// Is result in the internal cache?
const value = getCache(key);
// If yes, serve the cached result
if (value !== null) return value;
const { routes: [data] = [] } = Maps.newDirectionFinder()
.setOrigin(origin)
.setDestination(destination)
.setMode(mode)
.getDirections();
if (!data) {
GOOGLEMAPS_DISTANCE;
}
const { legs: [{ distance: { text: distance } } = {}] = [] } = data;
// Store the result in internal cache for future
setCache(key, distance);
return distance;
};
Currently, the code is able to find the distance between two given addresses and return it for single inputs. In order to work around Google's API request limit, I have added the ability for it to cache previous values. Also, the code re-runs anytime it reaches an error (such as the API request limit).
Now, I would like to upgrade the function to be able to accept an array for origin and destination. I found the Google documentation for adding this ability by using the map call, but I can't seem to make it work. I would highly appreciate it if anyone would be kind enough to respond, and help me with this.
