สูตร Google Maps สำหรับ Google ชีต

เผยแพร่แล้ว: 2022-02-26

คุณสามารถนำพลังของ Google แผนที่มาสู่ Google ชีตของคุณโดยใช้สูตรง่ายๆ โดยไม่ต้องเขียนโค้ด คุณไม่จำเป็นต้องลงชื่อสมัครใช้ Google Maps API และผลลัพธ์ทั้งหมดจาก Google Maps จะถูกแคชไว้ในชีต ดังนั้นคุณจึงไม่น่าจะถึงขีดจำกัดโควตาใดๆ

เพื่อยกตัวอย่างอย่างรวดเร็ว หากคุณมีที่อยู่เริ่มต้นในคอลัมน์ A และที่อยู่ปลายทางในคอลัมน์ B สูตรอย่าง =GOOGLEMAPS_DISTANCE(A1, B1, "driving") จะคำนวณระยะห่างระหว่างจุดสองจุดอย่างรวดเร็ว

หรือแก้ไขสูตรเล็กน้อย =GOOGLEMAPS_TIME(A1, B1, "walking") เพื่อให้รู้ว่าต้องใช้เวลานานแค่ไหนกว่าจะเดินจากจุดหนึ่งไปอีกจุดหนึ่ง

หากคุณต้องการลองใช้สูตรต่างๆ ของ Google Maps โดยไม่ต้องเจาะลึกรายละเอียดทางเทคนิค เพียงทำสำเนา Google ชีตนี้ เท่านี้คุณก็พร้อมแล้ว

Google Maps in Google Sheets

การใช้ Google Maps ภายใน Google ชีต

บทช่วยสอนนี้อธิบายวิธีเขียนฟังก์ชัน Google Maps ที่กำหนดเองใน Google ชีตได้อย่างง่ายดาย ซึ่งจะช่วยคุณ:

  1. คำนวณระยะทางระหว่างสองเมืองหรือที่อยู่ใดๆ
  2. คำนวณเวลาเดินทาง (เดิน ขับรถ หรือขี่จักรยาน) ระหว่างสองจุด
  3. รับพิกัดละติจูดและลองจิจูดของที่อยู่ใดก็ได้บน Google Maps
  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. Reverse Geocoding ใน 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 Maps API เพื่อพิมพ์เส้นทางการขับขี่ทีละขั้นตอน

=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 Maps

ระบุที่อยู่ต้นทาง ที่อยู่ปลายทาง โหมดการเดินทาง และฟังก์ชันจะวัดเวลาการเดินทางโดยประมาณของคุณระหว่างที่อยู่ที่ระบุ หากมีเส้นทางอยู่

=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 Maps ในชีต

เคล็ดลับ: ปรับปรุงประสิทธิภาพด้วยการแคชผลลัพธ์

ฟังก์ชัน Google ชีตด้านบนทั้งหมดใช้ Google Maps API ภายในเพื่อคำนวณเส้นทาง ระยะทาง และเวลาเดินทาง Google เสนอโควต้าที่จำกัดสำหรับการทำงานของ Maps และหากแผ่นงานของคุณมีการสืบค้นข้อมูลมากเกินไปในระยะเวลาอันสั้น คุณอาจพบข้อผิดพลาด เช่น ""บริการถูกเรียกใช้หลายครั้งเกินไปในหนึ่งวัน" หรือสิ่งที่คล้ายกัน

เพื่อแก้ไขปัญหานี้ ขอแนะนำให้คุณใช้แคชในตัวของ 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 Maps ในอีเมลและเอกสาร