Home > Software design >  Error Parsing Google Maps Data from XML using Apps Script in Google Sheets
Error Parsing Google Maps Data from XML using Apps Script in Google Sheets

Time:11-27

I'm trying to extract the "formatted_address" from an XML that is created using a Google Maps API. I can't get past the XML part of the code. I'm getting the error "Exception: Bad request: http://0 (line 176)". Is there something I'm missing? How can I modifiy this? Any guidance is appreciated. Line 176 is "var extractXml = UrlFetchApp.fetch(addUrl);" The idea is to look up a "sloppy" address and return a Google standardized version in another cell in Google Sheets. The snip is the XML as seen in browser when I paste in the URL. The "formatted_address" part is what I'm trying to return.Google Sheet Snip

   function addressFormat() {
   var ss = SpreadsheetApp.getActive();
   var sss = ss.getSheetByName("format");
   var addr = sss.getRange(1,1,).getValue();
   var apiKey = "API Key Here";
   var  addUrl = "https://maps.googleapis.com/maps/api/geocode/xml?address=" & addr & "&key=" & apiKey;
   var extractXml = UrlFetchApp.fetch(addUrl);
   var contentsXml = extractXml.getContentText;
   var xmlAdd = XmlService.parse(contentsXml);
   var response = xmlAdd.getRootElement();
   var records = response.getChild('GeocodeResponse');
   var recordList = records.getChild('result');
   var formattedAddd = recordList('formatted_address')

   return formattedAddd.getValue;

}

Snip of XML

CodePudding user response:

Modification points:

  • I thought that var addUrl = "https://maps.googleapis.com/maps/api/geocode/xml?address=" & addr & "&key=" & apiKey; should be var addUrl = "https://maps.googleapis.com/maps/api/geocode/xml?address=" encodeURIComponent(addr) "&key=" apiKey; or var addUrl = `https://maps.googleapis.com/maps/api/geocode/xml?address=${encodeURIComponent(addr)}&key=${apiKey}`;.

  • Please add () to var contentsXml = extractXml.getContentText;.

  • From your script, I thought that you might want to retrieve the value of formatted_address. In this case, it is required to modify your script for retrieving the value from XML data.

When these points are reflected in your script, how about the following modification?

Modified script:

function addressFormat() {
  var apiKey = "API Key Here"; // Please set your API key.

  var ss = SpreadsheetApp.getActive();
  var sss = ss.getSheetByName("format");
  var addr = sss.getRange(1, 1).getValue();
  var addUrl = "https://maps.googleapis.com/maps/api/geocode/xml?address="   encodeURIComponent(addr)   "&key="   apiKey; // or var addUrl = `https://maps.googleapis.com/maps/api/geocode/xml?address=${encodeURIComponent(addr)}&key=${apiKey}`;
  var extractXml = UrlFetchApp.fetch(addUrl);
  var contentsXml = extractXml.getContentText();
  var xmlAdd = XmlService.parse(contentsXml);
  var root = xmlAdd.getRootElement();
  var ns = root.getNamespace();
  var formatted_address = root.getChild("result", ns).getChild("formatted_address", ns).getValue();
  return formatted_address;
}

Note:

  • From your showing image, I guessed that your API key can be used for the API. Please be careful about this.

Reference:

  • Related