تقنية

صيغ خرائط جوجل لجداول بيانات جوجل


يمكنك جلب قوة خرائط Google إلى جداول بيانات Google باستخدام صيغ بسيطة بدون أي تعليمات برمجية. لا تحتاج إلى الاشتراك في Google Maps API ويتم تخزين جميع النتائج من خرائط Google مؤقتًا في الورقة، لذلك من غير المرجح أن تصل إلى أي حدود للحصة النسبية.

لإعطائك مثالاً سريعًا، إذا كان لديك عنوان البداية في العمود A وعنوان الوجهة في العمود B، فستكون الصيغة مثل =GOOGLEMAPS_DISTANCE(A1, B1, "driving") سوف تحسب بسرعة المسافة بين النقطتين.

أو قم بتعديل الصيغة قليلاً =GOOGLEMAPS_TIME(A1, B1, "walking") لمعرفة المدة التي يستغرقها الإنسان للمشي من نقطة إلى أخرى.

إذا كنت ترغب في تجربة صيغ خرائط Google دون الدخول في التفاصيل الفنية، فما عليك سوى عمل نسخة من ورقة Google هذه وستكون جاهزًا.

استخدام خرائط جوجل داخل جداول بيانات جوجل

يشرح هذا البرنامج التعليمي كيف يمكنك بسهولة كتابة وظائف خرائط Google المخصصة داخل جداول بيانات Google والتي ستساعدك على:

  1. حساب المسافات بين مدينتين أو أي عناوين.
  2. احسب زمن السفر (المشي أو القيادة أو ركوب الدراجة) بين نقطتين.
  3. احصل على إحداثيات خطوط الطول والعرض لأي عنوان على خرائط Google.
  4. استخدم الترميز الجغرافي العكسي للعثور على العنوان البريدي من إحداثيات GPS.
  5. طباعة اتجاهات القيادة بين أي نقطة على وجه الأرض.
  6. احصل على العنوان من الرمز البريدي نفسه.

1. حساب المسافات في جداول بيانات Google

حدد الأصل والوجهة ووضع السفر (المشي أو القيادة) وستعيد الوظيفة المسافة بين النقطتين بالأميال.

=GOOGLEMAPS_DISTANCE("NY 10005", "Hoboken NJ", "walking")

/**
 * Calculate the distance between two
 * locations on Google Maps.
 *
 * =GOOGLEMAPS_DISTANCE("NY 10005", "Hoboken NJ", "walking")
 *
 * @param {String} origin The address of starting point
 * @param {String} destination The address of destination
 * @param {String} mode The mode of travel (driving, walking, bicycling or transit)
 * @return {String} The distance in miles
 * @customFunction
 */
const GOOGLEMAPS_DISTANCE = (origin, destination, mode) => {
  const { routes: [data] = [] } = Maps.newDirectionFinder()
    .setOrigin(origin)
    .setDestination(destination)
    .setMode(mode)
    .getDirections();

  if (!data) {
    throw new Error('No route found!');
  }

  const { legs: [{ distance: { text: distance } } = {}] = [] } = data;
  return distance;
};

2. عكس الترميز الجغرافي في جداول بيانات Google

حدد خط الطول وخط العرض واحصل على العنوان الكامل للنقطة من خلال الترميز الجغرافي العكسي للإحداثيات.

=GOOGLEMAPS_DISTANCE("NY 10005", "Hoboken NJ", "walking")

/**
 * Use Reverse Geocoding to get the address of
 * a point location (latitude, longitude) on Google Maps.
 *
 * =GOOGLEMAPS_REVERSEGEOCODE(latitude, longitude)
 *
 * @param {String} latitude The latitude to lookup.
 * @param {String} longitude The longitude to lookup.
 * @return {String} The postal address of the point.
 * @customFunction
 */

const GOOGLEMAPS_REVERSEGEOCODE = (latitude, longitude) => {
  const { results: [data = {}] = [] } = Maps.newGeocoder().reverseGeocode(latitude, longitude);
  return data.formatted_address;
};

3. احصل على إحداثيات GPS للعنوان

احصل على خط الطول وخط العرض لأي عنوان على خرائط Google.

=GOOGLEMAPS_LATLONG("10 Hanover Square, NY")

/**
 * Get the latitude and longitude of any
 * address on Google Maps.
 *
 * =GOOGLEMAPS_LATLONG("10 Hanover Square, NY")
 *
 * @param {String} address The address to lookup.
 * @return {String} The latitude and longitude of the address.
 * @customFunction
 */
const GOOGLEMAPS_LATLONG = (address) => {
  const { results: [data = null] = [] } = Maps.newGeocoder().geocode(address);
  if (data === null) {
    throw new Error('Address not found!');
  }
  const { geometry: { location: { lat, lng } } = {} } = data;
  return `${lat}, ${lng}`;
};

4. طباعة اتجاهات القيادة بين العناوين

حدد عنوان الأصل وعنوان الوجهة ووضع السفر وستستخدم الوظيفة واجهة برمجة تطبيقات خرائط Google لطباعة اتجاهات القيادة خطوة بخطوة.

=GOOGLEMAPS_DIRECTIONS("NY 10005", "Hoboken NJ", "walking")

/**
 * Find the driving direction between two
 * locations on Google Maps.
 *
 * =GOOGLEMAPS_DIRECTIONS("NY 10005", "Hoboken NJ", "walking")
 *
 * @param {String} origin The address of starting point
 * @param {String} destination The address of destination
 * @param {String} mode The mode of travel (driving, walking, bicycling or transit)
 * @return {String} The driving direction
 * @customFunction
 */
const GOOGLEMAPS_DIRECTIONS = (origin, destination, mode = 'driving') => {
  const { routes = [] } = Maps.newDirectionFinder()
    .setOrigin(origin)
    .setDestination(destination)
    .setMode(mode)
    .getDirections();
  if (!routes.length) {
    throw new Error('No route found!');
  }
  return routes
    .map(({ legs }) => {
      return legs.map(({ steps }) => {
        return steps.map((step) => {
          return step.html_instructions.replace(/<[^>]+>/g, '');
        });
      });
    })
    .join(', ');
};

5. قم بقياس وقت الرحلة باستخدام خرائط جوجل

حدد عنوان البداية وعنوان الوجهة ووضع السفر وستقوم الوظيفة بقياس الوقت التقريبي لرحلتك بين العناوين المحددة، بشرط وجود طريق.

=GOOGLEMAPS_DURATION("NY 10005", "Hoboken NJ", "walking")

/**
 * Calculate the travel time between two locations
 * on Google Maps.
 *
 * =GOOGLEMAPS_DURATION("NY 10005", "Hoboken NJ", "walking")
 *
 * @param {String} origin The address of starting point
 * @param {String} destination The address of destination
 * @param {String} mode The mode of travel (driving, walking, bicycling or transit)
 * @return {String} The time in minutes
 * @customFunction
 */
const GOOGLEMAPS_DURATION = (origin, destination, mode = 'driving') => {
  const { routes: [data] = [] } = Maps.newDirectionFinder()
    .setOrigin(origin)
    .setDestination(destination)
    .setMode(mode)
    .getDirections();
  if (!data) {
    throw new Error('No route found!');
  }
  const { legs: [{ duration: { text: time } } = {}] = [] } = data;
  return time;
};

وظائف خرائط جوجل في الأوراق

نصيحة: تحسين أداء الصيغة عن طريق التخزين المؤقت

تستخدم وظائف جداول بيانات Google داخليًا واجهة برمجة تطبيقات خرائط Google لحساب الطرق والمسافات ووقت السفر. تقدم Google حصة محدودة لعمليات الخرائط، وإذا كانت صفحتك تنفذ عددًا كبيرًا جدًا من الاستعلامات في مدة قصيرة، فمن المحتمل أن ترى أخطاء مثل “”تم استدعاء الخدمة عدة مرات في يوم واحد” أو شيء مشابه.

للتغلب على مشكلة الحصة النسبية، يوصى باستخدام ذاكرة التخزين المؤقت المضمنة في Apps Script لتخزين النتائج، وإذا كانت نتائج إحدى الوظائف موجودة بالفعل في الحالة، فستقدم طلبًا أقل إلى خرائط Google.

تستخدم وظائف الخرائط داخل ورقة Google هذه أيضًا التخزين المؤقت وإليك كيفية تنفيذها.

// The cache key for "New York" and "new york  " should be same
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);
};

/**
 * Calculate the travel time between two locations
 * on Google Maps.
 *
 * =GOOGLEMAPS_DURATION("NY 10005", "Hoboken NJ", "walking")
 *
 * @param {String} origin The address of starting point
 * @param {String} destination The address of destination
 * @param {String} mode The mode of travel (driving, walking, bicycling or transit)
 * @return {String} The time in minutes
 * @customFunction
 */
const GOOGLEMAPS_DURATION = (origin, destination, mode = 'driving') => {
  const key = ['duration', 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) {
    throw new Error('No route found!');
  }
  const { legs: [{ duration: { text: time } } = {}] = [] } = data;
  // Store the result in internal cache for future
  setCache(key, time);
  return time;
};

انظر أيضًا: تضمين خرائط Google في رسائل البريد الإلكتروني والمستندات



Source link


اكتشاف المزيد من مرابع التكنولوجيا

اشترك للحصول على أحدث التدوينات المرسلة إلى بريدك الإلكتروني.

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى

اكتشاف المزيد من مرابع التكنولوجيا

اشترك الآن للاستمرار في القراءة والحصول على حق الوصول إلى الأرشيف الكامل.

Continue reading