Googleスプレッドシート用のGoogleマップの数式

公開: 2022-02-26

コーディングなしの簡単な数式を使用して、GoogleマップのパワーをGoogleスプレッドシートにもたらすことができます。 Google Maps APIに登録する必要はなく、Google Mapsのすべての結果がシートにキャッシュされるため、割り当て制限に達する可能性はほとんどありません。

簡単な例を挙げると、列Aに開始アドレスがあり、列Bに宛先アドレスがある場合、 =GOOGLEMAPS_DISTANCE(A1, B1, "driving")のような式は、2点間の距離をすばやく計算します。

または、式=GOOGLEMAPS_TIME(A1, B1, "walking")を少し変更して、ある地点から別の地点まで人が歩くのにかかる時間を確認します。

技術的な詳細に立ち入ることなくGoogleマップの数式を試してみたい場合は、このGoogleスプレッドシートのコピーを作成するだけで、準備は完了です。

Google Maps in Google Sheets

Googleスプレッドシート内でのGoogleマップの使用

このチュートリアルでは、Googleスプレッドシート内にカスタムのGoogleマップ機能を簡単に記述して、次のことを行う方法について説明します。

  1. 2つの都市または任意の住所間の距離を計算します。
  2. 2地点間の移動時間(徒歩、運転、自転車)を計算します。
  3. Googleマップ上の任意の住所の緯度と経度の座標を取得します。
  4. 逆ジオコーディングを使用して、GPS座標から住所を検索します。
  5. 地球上の任意の地点間の運転ルートを印刷します。
  6. 郵便番号自体からアドレスを取得します。

1.Googleスプレッドシートで距離を計算します

出発地、目的地、移動モード(徒歩または運転)を指定すると、この関数は2点間の距離をマイル単位で返します。

=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 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マップで旅行時間を測定します

出発地の住所、目的地の住所、移動モードを指定すると、ルートが存在する場合、機能は指定された住所間のおおよその旅行時間を測定します。

=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 Maps APIを使用して、ルート、距離、移動時間を計算します。 Googleはマップ操作の割り当てを制限しており、シートが短期間に実行するクエリが多すぎると、「サービスが1日に何度も呼び出されました」などのエラーが発生する可能性があります。

この問題を回避するには、Apps Scriptの組み込みキャッシュを使用して結果を保存することをお勧めします。関数の結果がケースにすでに存在する場合は、Googleマップへのリクエストを1つ少なくします。この中のマップ関数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マップを埋め込む