Last active
August 15, 2023 05:15
-
-
Save ducchetrongminh/b3d5547689eae1367e5c09e1bfbd5440 to your computer and use it in GitHub Desktop.
Vịt làm Data Geocode - Hàm Google Sheets biến địa chỉ nhập tay thành dữ liệu chuẩn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
HÀM GOOGLE SHEETS BIẾN ĐỊA CHỈ NHẬP TAY THÀNH DỮ LIỆU CHUẨN | |
Được viết bởi Vịt làm Data @ https://www.youtube.com/@vitlamdata | |
Video hướng dẫn sử dụng: {{LINK}} | |
Github Gist: https://gist.github.com/ducchetrongminh/b3d5547689eae1367e5c09e1bfbd5440 | |
------------------------------ | |
HƯỚNG DẪN SỬ DỤNG | |
Bước 1: Copy file Google Sheets có sẵn tại link này: https://docs.google.com/spreadsheets/d/1DjnrgvlAh6NvdP8yd4S0BleOeMQ8ueayd3n_tCFIBXg/copy | |
HOẶC copy toàn bộ đoạn code này vào Apps Script của một file Google Sheets. Hướng dẫn cụ thể tại đây: https://developers.google.com/apps-script/quickstart/custom-functions#set_up_the_script | |
Bước 2: Mở file Google Sheets, dùng hàm =VITLAMDATA_GEOCODE("địa chỉ của bạn"). Hàm này sẽ trả về dữ liệu chuẩn từ Google Maps. | |
[số nhà, tên đường, quận huyện, tỉnh thành, quốc gia, địa chỉ chuẩn, vĩ độ, kinh độ] | |
Bước 3: Sau khi hàm đã trả về dữ liệu, copy kết quả trả về và "paste value only" (Ctrl+Shift+V) để lưu lại kết quả. | |
Lý do cần phải làm bước này | |
(1) hàm này cho phép 1,000 lần sử dụng/ngày (xem thêm phần GIỚI HẠN) | |
(2) Google Sheets sẽ chạy lại hàm này mỗi khi bạn mở file. | |
Do đó nếu bạn không paste value, mỗi khi mở lại file Google Sheets, các ô đang có hàm này sẽ chạy lại và hết số lượng cho phép. | |
------------------------------ | |
GIỚI HẠN | |
Đoạn code này dùng hàm geocode của Google Maps, tối đa 1,000 lần/ngày cho người dùng bình thường, và 10,000 lần/ngày cho Google Workspace account. | |
Xem thêm giới hạn tại: https://developers.google.com/apps-script/guides/services/quotas#:~:text=Google%20Map%20Geocode%20calls | |
Xem thêm hàm geocode của Google Maps: https://developers.google.com/apps-script/reference/maps/geocoder#geocode(String) | |
Nếu bạn cần dùng nhiều hơn 1,000 lần/ngày, copy file này từ những tài khoản Google khác nhau. | |
*/ | |
/** | |
* Trả về dữ liệu chuẩn từ địa chỉ nhập tay. Được làm bởi Vịt làm Data 🐥 https://www.youtube.com/@vitlamdata | |
* @param {string} address Địa chỉ dưới dạng ký tự. | |
* @returns Dữ liệu chuẩn. | |
* @customfunction | |
*/ | |
function VITLAMDATA_GEOCODE(address) { | |
if (!address) { | |
return null | |
} | |
let data = Maps.newGeocoder().geocode(address).results[0] | |
// Logger.log(data) | |
let addressComponents = data.address_components | |
let streetNumber = extractGeocodeComponentByType(addressComponents, "street_number") | |
let route = extractGeocodeComponentByType(addressComponents, "route") | |
let adminLevel2 = extractGeocodeComponentByType(addressComponents, ["administrative_area_level_2", "locality"]) | |
let adminLevel1 = extractGeocodeComponentByType(addressComponents, "administrative_area_level_1") | |
let country = extractGeocodeComponentByType(addressComponents, "country") | |
let formattedAddress = data.formatted_address | |
let lat = data.geometry.location.lat | |
let lng = data.geometry.location.lng | |
return [[streetNumber, route, adminLevel2, adminLevel1, country, formattedAddress, lat, lng]] | |
} | |
function extractGeocodeComponentByType(components, types) { | |
if (typeof types === "string") { | |
types = [types] | |
} | |
let filteredComponents = components.filter(component => { | |
return types.some(filteringType => component.types.includes(filteringType)) | |
}) | |
if (filteredComponents.length) { | |
return filteredComponents[0].long_name | |
} | |
else { | |
return 'Undefined' | |
} | |
} | |
function testGeocode() { | |
Logger.log(VITLAMDATA_GEOCODE('18 hai ba trung, tphcm')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment