Google Sheets के साथ Region Lookup API का इस्तेमाल करना

Apps Script का इस्तेमाल करके, Google Sheets से Region Lookup API को कॉल करके, ऑफ़लाइन क्षेत्रों के लिए जगह के आईडी देखे जा सकते हैं. हमारा सुझाव है कि इस विकल्प का इस्तेमाल उन डेटासेट के लिए करें जिनमें इनपुट पैरामीटर अस्पष्ट हैं और एक से ज़्यादा प्लेस आईडी (उदाहरण के लिए, "10 मेन स्ट्रीट") से मेल खा सकते हैं. इसके लिए, डीबग करने की ज़रूरत पड़ सकती है. यहां एक उदाहरण स्क्रिप्ट दी गई है. इसमें ये फ़ंक्शन शामिल हैं:

  • SearchRegionByLocation उस जगह को खोजता है जिसकी सीमा में अक्षांश/देशांतर के दिए गए निर्देशांक शामिल हैं.
  • SearchRegionByAddress उस जगह को खोजता है जिसकी सीमा में दिया गया पता शामिल हो.
  • SearchRegionByPlaceId, दिए गए जगह के आईडी के हिसाब से जगह खोजता है.
  • LookupRegionByName से, दी गई जगह का नाम खोजा जाता है.
  • LookupRegionByUnitCode, दिए गए यूनिट कोड के हिसाब से जगह की जानकारी खोजता है.

रीजन लुकअप के कस्टम फ़ंक्शन

कस्टम फ़ंक्शन का इस्तेमाल करने के लिए, आपको सबसे पहले Apps Script के स्क्रिप्ट एडिटर में कस्टम फ़ंक्शन .gs स्क्रिप्ट कोड जोड़ना होगा. स्क्रिप्ट लोड होने के बाद, फ़ंक्शन को उसी तरह कॉल किया जा सकता है जिस तरह किसी अन्य स्प्रेडशीट फ़ंक्शन को कॉल किया जाता है. स्क्रिप्ट, सेल से इनपुट डेटा लेती हैं. शीट से कस्टम फ़ंक्शन को इस तरह कॉल करें: =LookupRegionByName(A2,C2,D2,E2)

नतीजे, फ़ंक्शन वाले सेल और उसके दाईं ओर मौजूद दो सेल में दिखते हैं. अगर जगह के आईडी उपलब्ध हैं, तो उन आईडी के नतीजे भी आस-पास की सेल में दिखेंगे. पुष्टि करने के लिए, Google Maps में क्षेत्र का नाम और पॉलीगॉन दिखाने वाले जगह के पेज का लिंक दिया जाता है. यहां दिए गए उदाहरण में, अगर फ़ंक्शन को सेल A1 में चिपकाया जाता है, तो नतीजे इस तरह दिखेंगे:

जगह का आईडी स्थान पेज URL गड़बड़ी का कोड
ChIJLQQwv4qBb0gRIMaY1COLDQU https://www.google.com/maps/search/?api=1&query=%20&query_place_id=ChIJLQQwv4qBb0gRIMaY1COLDQU

ऊपर दिए गए उदाहरण में, अनुरोध पूरा हो गया है. इसलिए, गड़बड़ी वाला सेल खाली है. अगर स्क्रिप्ट को सही तरीके से चलाने में कोई गड़बड़ी होती है (जैसे कि मान्य एपीआई पासकोड को छोड़ना), तो गड़बड़ी उस सेल के लिए टिप्पणी के तौर पर दिखेगी जिसमें फ़ंक्शन शामिल है.

Apps Script में कस्टम फ़ंक्शन जोड़ना

  1. Google Sheets में कोई स्प्रेडशीट खोलें.
  2. एक्सटेंशन > Apps Script मेन्यू आइटम चुनें.
  3. स्क्रिप्ट एडिटर में मौजूद सभी कोड मिटाएं.
  4. यहां दिए गए उदाहरण से कोड को कॉपी करें और स्क्रिप्ट एडिटर में चिपकाएं.
  5. YOUR_API_KEY को उस प्रोजेक्ट से जुड़ी एपीआई कुंजी से बदलें जिस पर कोई पाबंदी नहीं है. साथ ही, उस प्रोजेक्ट के लिए क्षेत्र की जानकारी पाने वाला एपीआई चालू होना चाहिए.
  6. सबसे ऊपर, सेव करें पर क्लिक करें.
/**
 * @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);
}