Excel Google 地图插件






4.50/5 (6投票s)
如果你在 Excel 中有经纬度坐标,并且想在 Google 地图上显示它们,这个简单的 Excel 插件可以帮助你实现。
引言
你可以使用 Excel 插件来扩展 Excel 的内置功能。 我觉得它们非常有用,我想在这里分享一个简单的 Excel 插件,向你展示它们的工作原理,以及你如何创建自己的插件。
Using the Code
将本文的 zip 文件下载到你的计算机上,解压缩 Excel 插件:Google Maps.xlam,并在 Excel 中打开它。
或者,选择“开发工具”>“Excel 插件”>“浏览...”> 选择插件 > “确定”> 勾选插件 > “确定”,让 Excel 在每次启动时加载该插件。
如果你在 Excel 中没有看到“开发工具”选项卡,可以通过选择“文件”>“选项”>“自定义功能区”> 勾选“开发工具”> “确定”来显示它。
如果 Excel 每次启动时都要求你启用该插件,你可以将其位置添加到信任位置,方法是选择“文件”>“选项”>“信任中心”>“信任中心设置...”>“信任位置”>“添加新位置...”> 选择位置 > “确定”> “确定”。
该插件在“加载项”选项卡中添加了一个 Google 地图按钮。
要查看它的功能,请复制以下表格并将其粘贴到 Excel 中
纬度 | 经度 | Label |
42.3149 | -83.0364 | 温莎 |
49.0323 | -119.4682 | 奥索尤斯 |
45.5231 | -122.6765 | 波特兰 |
突出显示经纬度、标签 - 但不要突出显示标题 - 然后转到“加载项”选项卡并单击 Google 地图按钮。
该插件会在你的temp目录中生成一个 Google 地图 JavaScript HTML 文件,并在你的默认网络浏览器中打开它。 Google 地图包含三个标记,每个标记对应表格中的一个坐标。 你可以将鼠标悬停在每个标记上以查看其标签以及经纬度。
你可以通过选择“开发工具”>“Visual Basic”>“VBAProject (Google Maps.xlam)”>“模块”>“Module1”来查看 VBA 代码以及它如何生成 Google 地图。
带有IRibbonControl
对象的子程序会在单击 Google 地图按钮时收到通知。
'*****************************************************************************
Sub OnGoogleMapsButton(control As IRibbonControl)
'
' This sub is notified when the user clicks the Google Maps button in the
' Add-Ins tab.
'
Dim Row As Range
Dim FileName As String
Dim Label As String
Dim Latitude As String
Dim Longitude As String
' Example; the following cells are in the active sheet:
' Not highlighted:
' A B C
' +----------+------------+---------+
' 1 | Latitude | Longitude | Label |
' +----------+------------+---------+
' Highlighted:
' +----------+-----------+----------+
' 2 | 42.3149 | -83.0364 | Windsor |
' +----------+-----------+----------+
' 3 | 49.0323 | -119.4682 | Osoyoos |
' +----------+-----------+----------+
' 4 | 45.5231 | -122.6765 | Portland |
' +----------+-----------+----------+
If Selection.Columns.Count < 2 Then
MsgBox "You need to highlight at least 2 columns; _
(1) the latitude and (2) the longitude.", vbCritical + vbOKOnly
Exit Sub
End If
If Selection.Columns.Count > 3 Then
MsgBox "You can't highlight more than 3 columns; (1) the latitude, _
(2) the longitude, and (3) a label.", vbCritical + vbOKOnly
Exit Sub
End If
' FileName = "C:\Users\Sotirios\AppData\Local\Temp\Google Maps.html"
FileName = Environ("Temp") & "\Google Maps.html"
Open FileName For Output As #1
Print #1, "<!DOCTYPE html>"
Print #1, "<html>"
Print #1, " <head>"
Print #1, " <meta name=" + Chr$(34) + "viewport" + Chr$(34) + _
" content=" + Chr$(34) + "initial-scale=1.0, user-scalable=no" + Chr$(34) + ">"
Print #1, " <meta charset=" + Chr$(34) + "utf-8" + Chr$(34) + ">"
Print #1, " <title>Google Maps</title>"
Print #1, " <style>"
Print #1, " html, body, #map-canvas"
Print #1, " {"
Print #1, " height: 100%;"
Print #1, " margin: 0px;"
Print #1, " padding: 0px"
Print #1, " }"
Print #1, " </style>"
Print #1, " <script src=" + Chr$(34) + _
"https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true" + Chr$(34) + "></script>"
Print #1, " <script>"
Print #1, ""
Print #1, "function initialize()"
Print #1, "{"
Print #1, " var mapOptions ="
Print #1, " {"
Print #1, " zoom: 2,"
Print #1, " center: new google.maps.LatLng(0, 0)"
Print #1, " };"
Print #1, ""
Print #1, " var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);"
For Each Row In Selection.Rows
' Latitude = "42.3149"
Latitude = Trim(Row.Cells(, 1).Text)
If IsNumeric(Latitude) = False Then
Row.Cells(, 1).Activate
MsgBox "The latitude (in the active cell) needs to be numeric.", vbCritical + vbOKOnly
Close #1
Exit Sub
End If
' Longitude = "-83.0364"
Longitude = Trim(Row.Cells(, 2).Text)
If IsNumeric(Longitude) = False Then
Row.Cells(, 2).Activate
MsgBox "The longitude (in the active cell) needs to be numeric.", vbCritical + vbOKOnly
Close #1
Exit Sub
End If
If Selection.Columns.Count = 3 Then
' Label = "Windsor"
Label = Trim(Row.Cells(, 3).Text)
Else
Label = "Marker " + CStr(Row.Row)
End If
Print #1, ""
Print #1, " var marker" + CStr(Row.Row) + "= new google.maps.Marker("
Print #1, " {"
Print #1, " position: new google.maps.LatLng(" + Latitude + ", " + Longitude + "),"
Print #1, " title: " + Chr$(34) + Label + ": (" + Latitude + ", " + Longitude + ")" + _
"\nDrag this marker to get the latitude and longitude at a different location." + _
Chr$(34) + ","
Print #1, " draggable: true,"
Print #1, " map: map"
Print #1, " });"
Print #1, ""
Print #1, " google.maps.event.addListener(marker" + CStr(Row.Row) + _
", 'dragend', function(event)"
Print #1, " {"
Print #1, " var Title = marker" + CStr(Row.Row) + ".getTitle();"
Print #1, " var SubStrings = Title.split(" + Chr$(34) + "\n" + Chr$(34) + ");"
Print #1, " marker" + CStr(Row.Row) + ".setTitle(SubStrings[0] + " + _
Chr$(34) + "\n" + Chr$(34) + " + "; Chr$(34) + _
"The latitude and longitude at this location is: " + Chr$(34) + " + marker" + _
CStr(Row.Row) + ".getPosition().toString());"
Print #1, " });"
Next Row
Print #1, "}"
Print #1, ""
Print #1, "google.maps.event.addDomListener(window, 'load', initialize);"
Print #1, ""
Print #1, " </script>"
Print #1, " </head>"
Print #1, " <body>"
Print #1, " <div id=" + Chr$(34) + "map-canvas" + Chr$(34) + "></div>"
Print #1, " </body>"
Print #1, "</html>"
Close #1
ActiveWorkbook.FollowHyperlink Address:=FileName, NewWindow:=True
End Sub
你可以通过在Microsoft Office 自定义 UI 编辑器中打开插件来查看插件如何将 Google 地图按钮添加到“加载项”选项卡,以及如何通知 VBA 子程序何时单击该按钮。
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon>
<tabs>
<tab idMso="TabAddIns">
<group id="GroupMaps" label="Maps">
<button id="ButtonGoogleMaps" image="Google-Maps" size="large" _
label="Google" screentip="Show highlighted latitudes and longitudes in Google Maps" _
supertip="The latitude should be in the first highlighted column, the longitude in the second, _
and a label (if desired) in the third." onAction="OnGoogleMapsButton" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
“image=
”告诉 Excel 为 Google 地图按钮显示哪个图标; 在这种情况下,它是来自 IconArchive 的.png文件,并使用自定义 UI 编辑器 For Microsoft Office 添加到插件中。
“onAction=
”告诉 Excel 在单击 Google 地图按钮时通知哪个 VBA 子程序。