আপনি অ্যাপস স্ক্রিপ্ট ব্যবহার করে গুগল শিটস থেকে রিজিওন লুকআপ এপিআই (Region Lookup API) কল করার মাধ্যমে অফলাইনে অঞ্চলগুলির প্লেস আইডি (Place ID) খুঁজে নিতে পারেন। যেসব ডেটাসেটে ইনপুট প্যারামিটারগুলি অস্পষ্ট এবং একাধিক প্লেস আইডিতে (যেমন "10 Main Street") পরিণত হতে পারে, সেগুলির জন্য এটি সুপারিশ করা হয়, যার জন্য ডিবাগিংয়ের প্রয়োজন হতে পারে। একটি উদাহরণ স্ক্রিপ্ট প্রদান করা হয়েছে, যেখানে নিম্নলিখিত ফাংশনগুলি রয়েছে:
-
SearchRegionByLocationএমন একটি স্থান অনুসন্ধান করে যার সীমানায় নির্দিষ্ট অক্ষাংশ/দ্রাঘিমাংশ স্থানাঙ্ক অন্তর্ভুক্ত থাকে। -
SearchRegionByAddressএমন একটি স্থান অনুসন্ধান করে যার সীমানার মধ্যে নির্দিষ্ট ঠিকানাটি অন্তর্ভুক্ত। -
SearchRegionByPlaceIdনির্দিষ্ট স্থান আইডি ব্যবহার করে কোনো স্থান অনুসন্ধান করে। -
LookupRegionByNameনির্দিষ্ট নামের কোনো স্থান খুঁজে বের করে। -
LookupRegionByUnitCodeনির্দিষ্ট ইউনিট কোডসহ একটি স্থান খুঁজে বের করে।
অঞ্চল অনুসন্ধান কাস্টম ফাংশন
কাস্টম ফাংশনগুলো ব্যবহার করার জন্য, আপনাকে প্রথমে অ্যাপস স্ক্রিপ্ট স্ক্রিপ্ট এডিটরে কাস্টম ফাংশনগুলোর .gs স্ক্রিপ্ট কোড যোগ করতে হবে। স্ক্রিপ্টটি লোড হয়ে গেলে, আপনি অন্য যেকোনো স্প্রেডশীট ফাংশনের মতোই এই ফাংশনগুলো কল করতে পারবেন। স্ক্রিপ্টগুলো সেল থেকে ইনপুট ডেটা গ্রহণ করে। শীট থেকে কাস্টম ফাংশনগুলো এভাবে কল করুন: =LookupRegionByName(A2,C2,D2,E2)
ফাংশনটি থাকা সেলটিতে এবং তার ডানদিকের দুটি সেলে ফলাফল আউটপুট করা হয়। যদি সম্ভাব্য স্থান আইডি পাওয়া যায়, তবে সেই স্থান আইডিগুলোর ফলাফলও সংলগ্ন সেলগুলোতে আউটপুট করা হবে। যাচাইয়ের জন্য, গুগল ম্যাপসে অঞ্চলের নাম এবং পলিগন প্রদর্শনকারী স্থানটির পৃষ্ঠার একটি লিঙ্ক দেওয়া থাকে। নিম্নলিখিত উদাহরণে, যদি ফাংশনটি A1 সেলে পেস্ট করা হয়, তাহলে ফলাফলটি দেখতে এইরকম হবে:
| স্থান আইডি | পৃষ্ঠার URL রাখুন | ত্রুটি কোড |
|---|---|---|
| ChIJLQQwv4qBb0gRIMaY1COLDQU | https://www.google.com/maps/search/?api=1&query=%20&query_place_id=ChIJLQQwv4qBb0gRIMaY1COLDQU |
পূর্ববর্তী উদাহরণে, অনুরোধটি সফল হওয়ায় ত্রুটির সেলটি খালি রয়েছে। যেসব ত্রুটির কারণে স্ক্রিপ্টটি সঠিকভাবে চলতে পারে না (যেমন একটি বৈধ এপিআই কী বাদ দেওয়া), সেই ত্রুটিটি ফাংশন ধারণকারী সেলটিতে একটি মন্তব্য হিসাবে প্রদর্শিত হবে।
অ্যাপস স্ক্রিপ্টে কাস্টম ফাংশনগুলো যোগ করুন
- গুগল শিটসে একটি স্প্রেডশীট খুলুন।
- এক্সটেনশন > অ্যাপস স্ক্রিপ্ট মেনু আইটেমটি নির্বাচন করুন।
- স্ক্রিপ্ট এডিটর থেকে যেকোনো কোড মুছে ফেলুন।
- নিচের উদাহরণ থেকে কোডটি কপি করে স্ক্রিপ্ট এডিটরে পেস্ট করুন।
-
YOUR_API_KEYএমন একটি প্রকল্পের অবাধ API কী দিয়ে প্রতিস্থাপন করুন যেখানে অঞ্চল অনুসন্ধান API সক্রিয় করা আছে। - উপরে, সেভ (Save বাটনে ক্লিক করুন।
/** * @fileoverview Provides custom Geo functions in Google Sheets for matching boundaries. * * SearchRegionByLocation searches for a region whose boundary contains the specified latitude/longitude coordinates. * * SearchRegionByAddress searches for a region whose boundary contains the specified address. * * SearchRegionByPlaceId searches for a region whose boundary contains the specified place ID. * * LookupRegionByName looks up a region with the specified name. * * LookupRegionByUnitCode looks up a region with the specified unit code. * @OnlyCurrentDoc */ var api_key = "YOUR_API_KEY"; // An unrestricted key is recommended for local use only (deployment is NOT recommended). function format_url(place_id) { return place_id && 'https://www.google.com/maps/search/?api=1&query=%20&query_place_id=' + place_id; } function format_result(result) { let matches = result.matches || []; let firstMatch = result.matches[0] || {}; let placeId = firstMatch.matchedPlaceId || ''; let debugInfo = firstMatch.debugInfo || ''; let candidates = firstMatch.candidatePlaceIds || []; return [[ placeId || '(NULL)', format_url(placeId), debugInfo, candidates[0], format_url(candidates[0]), candidates[1], format_url(candidates[1]), candidates[2], format_url(candidates[2]) ]]; } /** * Searches for a place whose boundary contains the specified latitude/longitude coordinates. * * @param {number} latitude - The latitude where the place will be found (e.g., 37.531939). * @param {number} longitude - The longitude where the place will be found (e.g., -122.2994121). * @param {string} place_type - The place type of the place ("postal_code", "administrative_area_level_1", "administrative_area_level_2", "locality", or "country"). * * @return {string} The place_id of the place, or null if none was found. * * @customfunction */ function SearchRegionByLocation( latitude, longitude, place_type) { var data = { "search_values": [ { "latlng": { 'latitude': latitude, 'longitude': longitude }, // { latitude: 37.531939, longitude: -122.2994121 } "place_type": place_type, "region_code": null, "language_code": null, } ] }; var options = { 'method' : 'post', 'contentType': 'application/json', 'headers': { 'X-Goog-Api-Key' : api_key }, // Convert the JavaScript object to a JSON string. 'payload' : JSON.stringify(data) }; var response = UrlFetchApp.fetch( 'https://regionlookup.googleapis.com/v1alpha:searchRegion', options); var resultText = response.getContentText(); console.log(resultText); var result = JSON.parse(resultText); return format_result(result); } /** * Searches for a place whose boundary contains the specified address. * * @param {string} address - An address within the place boundaries (e.g., "1505 Salado Dr, Mountain View, CA"). * @param {string} place_type - The geo type id of the place (e.g., "locality"). * @param {string} [region_code='us'] - The region code of the place (e.g., "US"). * @param {string} [language_code='en'] - The language code of the place's name. (e.g., "en"). * * @return {string} The place_id of the place, or null if none was found. * * @customfunction */ function SearchRegionByAddress( address, place_type, region_code, language_code) { var data = { "search_values": { "address": address, "place_type" : place_type, "region_code": region_code || 'us', "language_code": language_code || 'en', } }; var options = { 'method' : 'post', 'contentType': 'application/json', 'headers': { 'X-Goog-Api-Key' : api_key }, // Convert the JavaScript object to a JSON string. 'payload' : JSON.stringify(data) }; var response = UrlFetchApp.fetch( 'https://regionlookup.googleapis.com/v1alpha:searchRegion', options); var resultText = response.getContentText(); console.log(resultText); var result = JSON.parse(resultText); return format_result(result); } /** * Searches for a place with the specified place ID. * * @param {string} place_id - The place ID to search for. * @param {string} place_type - The geo type id of the place (e.g., "locality"). * @param {string} [region_code='us'] - The region code of the place (e.g., "US"). * @param {string} [language_code='en'] - The language code of the place's name. (e.g., "en"). * * @return {string} The place_id of the place, or null if none was found. * * @customfunction */ function SearchRegionByPlaceId( place_id, place_type, region_code, language_code) { var data = { "search_values": { "place_id": place_id, "place_type" : place_type, "region_code": region_code || 'us', "language_code": language_code || 'en', } }; var options = { 'method' : 'post', 'contentType': 'application/json', 'headers': { 'X-Goog-Api-Key' : api_key }, // Convert the JavaScript object to a JSON string. 'payload' : JSON.stringify(data) }; var response = UrlFetchApp.fetch( 'https://regionlookup.googleapis.com/v1alpha:searchRegion', options); var resultText = response.getContentText(); console.log(resultText); var result = JSON.parse(resultText); return format_result(result); } /** * Looks up a place with the specified name. * * @param {string} place_name - The name of the place (e.g., "Palo Alto"). * @param {string} place_type - The geo type id of the place (e.g., "locality"). * @param {string} [region_code='us'] - The region code of the place (e.g., "US"). * @param {string} [language_code='en'] - The language code of the place's name. (e.g., "en"). * * @return {string} The place_id of the place, or null if none was found. * * @customfunction */ function LookupRegionByName( place, place_type, region_code, language_code) { var data = { "identifiers": [ { "place": '' + place, "place_type": place_type, "region_code": region_code || 'us', "language_code": language_code || 'en', } ] }; var options = { 'method' : 'post', 'contentType': 'application/json', 'headers': { 'X-Goog-Api-Key' : api_key }, // Convert the JavaScript object to a JSON string. 'payload' : JSON.stringify(data), //'muteHttpExceptions' : true, }; var response = UrlFetchApp.fetch( 'https://regionlookup.googleapis.com/v1alpha:lookupRegion', options); var resultText = response.getContentText(); console.log(resultText); var result = JSON.parse(resultText); return format_result(result); } /** * Looks up a place with the specified unit code. * * @param {string} place_name - The name of the place (e.g., "Palo Alto"). * @param {string} place_type - The geo type id of the place (e.g., "locality"). * @param {string} [region_code='us'] - The region code of the place (e.g., "US"). * @param {string} [language_code='en'] - The language code of the place's name. (e.g., "en"). * * @return {string} The place_id of the place, or null if none was found. * * @customfunction */ function LookupRegionByUnitCode( unit_code, place_type, region_code, language_code) { var data = { "identifiers": [ { "unit_code": '' + unit_code, "place_type": place_type, "region_code": region_code || 'us', "language_code": language_code || 'en', } ] }; var options = { 'method' : 'post', 'contentType': 'application/json', 'headers': { 'X-Goog-Api-Key' : api_key }, // Convert the JavaScript object to a JSON string. 'payload' : JSON.stringify(data), //'muteHttpExceptions' : true, }; var response = UrlFetchApp.fetch( 'https://regionlookup.googleapis.com/v1alpha:lookupRegion', options); var resultText = response.getContentText(); console.log(resultText); var result = JSON.parse(resultText); return format_result(result); }