javascript - 在 Excel 电子表格中运行 JQuery 地理定位脚本

标签 javascript jquery excel geolocation vba

是否可以在 Excel 电子表格中使用我的 jquery 脚本?我正在尝试使用地理定位来具有自动填写地址的功能。我能够通过 html 完成此操作。 http://jsfiddle.net/bobrierton/13ffw6ko/但我很好奇如何在 Excel 工作表中实现相同的目标。

我想让电子表格中的列为地址、城市、州、邮政编码,然后每次点击一个地址时,我都希望它预填并提供建议,例如我的 html 版本。

请问有人可以协助解决这个问题吗?

var placeSearch, autocomplete;
var componentForm = {
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  postal_code: 'short_name'
};

function initialize() {
  // Create the autocomplete object, restricting the search
  // to geographical location types.
  autocomplete = new google.maps.places.Autocomplete(
    /** @type {HTMLInputElement} */
    (document.getElementById('autocomplete')), {
      types: ['geocode']
    });
  // When the user selects an address from the dropdown,
  // populate the address fields in the form.
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    fillInAddress();
  });
}

// [START region_fillform]
function fillInAddress() {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();

  for (var component in componentForm) {
    document.getElementById(component).value = '';
    document.getElementById(component).disabled = false;
  }

  // Get each component of the address from the place details
  // and fill the corresponding field on the form.
  for (var i = 0; i < place.address_components.length; i++) {
    var addressType = place.address_components[i].types[0];
    if (componentForm[addressType]) {
      var val = place.address_components[i][componentForm[addressType]];
      document.getElementById(addressType).value = val;
    }
  }
  //var keys=[];for (var key in place.address_components[0]) keys.push(key);
  //alert(keys):
  document.getElementById('autocomplete').value = 
    place.address_components[0]['long_name'] + ' ' +
    place.address_components[1]['long_name'];
  
  /*document.getElementById('route').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete').value : '');*/
  document.getElementById('route').value = '';
}

// [START region_geolocation]
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = new google.maps.LatLng(
        position.coords.latitude, position.coords.longitude);
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
    <body onload="initialize()">
    <div id="locationField">
      <div class="clearfix">
    	<label for="street_<cfoutput>#Add#</cfoutput>">Mailing Address 1:</label>
    	<input type="text" name="street_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete" size="54" maxlength="120" message="Please enter owner #Peoplecount#'s mailing address." onFocus="geolocate()" value="">
    </div>

    <div class="clearfix">
    	<label for="m2street_<cfoutput>#Add#</cfoutput>">Mailing Address 2:</label>
    	<input type="text" name="m2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route" size="54" maxlength="120" value="">
    </div>
      
    <div class="clearfix">
    	<label for="city_<cfoutput>#Add#</cfoutput>">City:</label>
    	<input type="text" name="city_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s mailing city." value="">
        </div>
        
        <div class="clearfix">
    	<label for="state_<cfoutput>#Add#</cfoutput>">State:</label>
    	<input type="text" name="state_#Add#" required="yes" id="administrative_area_level_1" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing state." value="">
    </div>
            
            <div class="clearfix">
    	<label for="street_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
    	<input type="text" name="postal_#Add#" required="yes" id="postal_code" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing zip code." value="">
    </div>
      </div>

最佳答案

Is it possible to use my jquery script in an excel spreadsheet? I am trying to use the geolocation to have the feature of an automatic address fill in.

没有。您的脚本依赖于 Google Maps API,这需要浏览器上下文才能正常工作。

虽然您可以在 Windows 脚本宿主中运行 JavaScript,但您需要的远不止 JavaScript。您需要一个浏览器 API,包括地理定位 API 和 AJAX。你不会在 Excel 中找到它。

但是您可以发挥创意。您可以使用脚本发出 HTTP 请求(尽管不是正常的 AJAX 方式)。您或许还可以执行外部应用程序来为您获取职位。

关于javascript - 在 Excel 电子表格中运行 JQuery 地理定位脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33806380/

相关文章:

javascript - 在 AJAX (Javascript) 中异步执行同步函数

javascript - 使用 JavaScript 中的 setInterval 函数自动刷新

javascript - 光滑 slider 中心模式

jquery - 使用 jQuery validate 忽略某些验证规则

vba - Excel VBA中的公共(public)与私有(private)/昏暗

java - JAVA如何获取excel表中指定位置单元格的值

vba - 数据透视表 : Detect When Pivot Field is Collapsed

javascript - svg-pan-zoom -- 在不同的浏览器尺寸中平移并缩放到同一位置

javascript - 覆盖图像覆盖自定义 Google map 网络中的标记

javascript - if 语句在 jquery 中不起作用