5

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. I am capable of doing this through html. http://jsfiddle.net/bobrierton/13ffw6ko/ But I am curious on how I can achieve the same goal in an excel sheet.

I want to have the columns in the spreadsheet be Address, City, State, Zipcode and then every time an address is clicked into I want it to prefill and give recommendations like my html version here.

Will someone please assist in how this may be possible.

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>
Community
  • 1
  • 1
David Brierton
  • 6,977
  • 12
  • 47
  • 104
  • There are some techniques for running javascript in excel described in these answers: http://stackoverflow.com/questions/20171885/excel-macros-with-javascript http://stackoverflow.com/questions/848246/how-can-i-use-javascript-within-an-excel-macro – Starscream1984 Nov 19 '15 at 14:26
  • are you saying it is possible? I cannot find this anywhere. I was able to find how to create a map of it but not how to do the automatic address fill – David Brierton Nov 19 '15 at 15:15
  • Seems theoretically possible, but probably not easy. I am pretty curious about why you want to do this in the first place? – Starscream1984 Nov 19 '15 at 17:26
  • yes, perhaps by knowing the reason and the target to achieve we can help you to find alternative way. – yogipriyo Nov 25 '15 at 02:50
  • well the reason is because I made them an app using HTML5 & JS that uses the Geolocation like the fiddle above. And the target is they want their excel spreadsheet to do the same when u click in address field as you start typing they want it to start suggesting addresses like the fiddle above. Then when they select the address have it fill in the appropriate fields (address,city,state,zip). Theres an address column so every time an address field is starting to be typed into it should start showing address matches like the fiddle above – David Brierton Nov 25 '15 at 14:05
  • I don't think thats possible dude, you are better off using google sheets so you can run gscript (which is pretty much the same as javascript). – Huan Zhang Nov 30 '15 at 12:23

1 Answers1

0

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.

No. Your script is relying on the Google Maps API, and that's going to require a browser context to work properly.

While you can run JavaScript in a Windows Script Host, you need far more than JavaScript. You need a browser API, complete with geolocation API, and AJAX. You won't find that in Excel.

You can however get creative. You can make HTTP requests with your script (although not the normal AJAX way). You can probably also execute an external application to get the position for you.

Brad
  • 159,648
  • 54
  • 349
  • 530