-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_sheet_google_maps.js
More file actions
101 lines (91 loc) · 3.25 KB
/
google_sheet_google_maps.js
File metadata and controls
101 lines (91 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Google Apps Script: Custom Google Sheets Formulas for Google Maps Geocoding
*
* Purpose:
* - `GOOGLEMAPSLAT`: Retrieves the latitude of a location from Google Maps.
* - `GOOGLEMAPSLONG`: Retrieves the longitude of a location from Google Maps.
*
* Features:
* - Allows optional restriction to a specific area using an additional argument.
* - Uses Google Maps Geocoding API.
* - Provides error handling for invalid locations or API issues.
*
* Setup:
* 1. Obtain a Google Maps Geocoding API key from the Google Cloud Console.
* 2. Replace `YOUR_API_KEY` with your actual API key in the code below.
*
* Usage in Google Sheets:
* - `=GOOGLEMAPSLAT("location text", "restrict area")`
* - `=GOOGLEMAPSLONG("location text", "restrict area")`
*
* Note:
* This script requires the Google Maps Geocoding API to be enabled in the Google Cloud Console.
*/
// Replace with your Google Maps API Key
const API_KEY = 'API-KEY';
// =GOOGLEMAPSLAT(C9, "Brazil")
/**
* Retrieves the latitude of a given location.
* @param {string} location - The location to search (e.g., "New York").
* @param {string} [restrictArea] - Optional area restriction (e.g., "USA", "Brazil").
* @return {number|string} - Latitude of the location or an error message.
*/
function GOOGLEMAPSLAT(location, restrictArea) {
const geocodeResult = geocodeLocation(location, restrictArea);
if (geocodeResult.error) {
return `Error: ${geocodeResult.error}`;
}
return geocodeResult.lat;
}
/**
* Retrieves the longitude of a given location.
* @param {string} location - The location to search (e.g., "New York").
* @param {string} [restrictArea] - Optional area restriction (e.g., "USA").
* @return {number|string} - Longitude of the location or an error message.
*/
function GOOGLEMAPSLONG(location, restrictArea) {
const geocodeResult = geocodeLocation(location, restrictArea);
if (geocodeResult.error) {
return `Error: ${geocodeResult.error}`;
}
return geocodeResult.lng;
}
/**
* Helper function to perform geocoding using Google Maps API.
* @param {string} location - The location to search.
* @param {string} [restrictArea] - Optional area restriction.
* @return {Object} - Object containing lat, lng, or error information.
*/
function geocodeLocation(location, restrictArea) {
try {
// Build the API URL
const baseUrl = 'https://maps.googleapis.com/maps/api/geocode/json';
const queryParams = {
address: location,
key: API_KEY,
};
if (restrictArea) {
queryParams.components = `country:${restrictArea}`;
}
const url = `${baseUrl}?${Object.entries(queryParams)
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
.join('&')}`;
// Fetch and parse the API response
const response = UrlFetchApp.fetch(url);
const json = JSON.parse(response.getContentText());
if (json.status !== 'OK') {
return { error: `API Error: ${json.status}` };
}
// Extract latitude and longitude from the response
const locationData = json.results[0]?.geometry?.location;
if (!locationData) {
return { error: 'Location not found.' };
}
return {
lat: locationData.lat,
lng: locationData.lng,
};
} catch (error) {
return { error: `Unexpected Error: ${error.message}` };
}
}