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

نشرت: 2022-02-26

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

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

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

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

Google Maps in Google Sheets

استخدام خرائط جوجل داخل أوراق جوجل

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

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

1. حساب المسافات في أوراق جوجل

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

=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. عكس الترميز الجغرافي في أوراق جوجل

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

=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. احصل على إحداثيات العنوان

احصل على خطوط الطول والعرض لأي عنوان على خرائط 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. قم بقياس وقت الرحلة باستخدام خرائط Google

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

=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 Sheet أيضًا التخزين المؤقت وإليك كيفية تنفيذه.

 // 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 في رسائل البريد الإلكتروني والمستندات