Google Sheets failing to parse "distance" from Google Distance Matrix API - google-sheets

I am using Google Sheets as a mileage tracker to calculate the distance from my home to various addresses. This formula had been working for me for a few months, but recently stopped:
=ROUND((importxml("https://maps.googleapis.com/maps/api/distancematrix/xml?&origins="&"My+Home+Address"&"&destinations="&A1&"&sensor=false&units=imperial&alternatives=false&key=MY_API_KEY","/DistanceMatrixResponse/row/element/distance/value")/1609.344)*2,1)
I am using my own valid API key and have verified that the query works outside of Google Sheets. The funny thing is, if I change "/distance/value" to "/duration/value", Sheets returns a response just fine.
Here's what the returned XML looks like for the Distance Matrix:
<DistanceMatrixResponse>
<status>OK</status>
<origin_address>Home Address, USA</origin_address>
<destination_address>Destination Address, USA</destination_address>
<row>
<element>
<status>OK</status>
<duration>
<value>1983</value>
<text>33 mins</text>
</duration>
<distance>
<value>29971</value>
<text>18.6 mi</text>
</distance>
</element>
</row>
</DistanceMatrixResponse>
How do I get the "distance" to work again??

This works for me:
=IMPORTXML("https://maps.googleapis.com/maps/api/distancematrix/xml?&origins="&B1&"&destinations="&B2&"&sensor=false&alternatives=false&key="&B3,"/DistanceMatrixResponse/row/element/distance/text")
BTW, if you get rid of units=imperial, it defaults to metric and you won't have to /1609.344

Related

importxml from xml result google sheets

i'm trying to retrieve travel distance and travel time from this XML
URL :https://dev.virtualearth.net/REST/v1/Routes/DistanceMatrix?origins=30.90232,30.17693&destinations=31.10171,30.3131&travelMode=driving&o=xml&key=AngGTts45jKAUVLSgsymA4Vho1GJfd2PN5V11RenWFRjhfa5gRwexx5HSbNQYz-2
i tried to TEXTJOIN(",",FALSE,IMPORTXML(E9,"//*"))
but hard to extract , so any help with this to allocate and extract Travel distance and time
in excel using webservice and filterxml works fine but in google sheets i couldn't find solution
and this result for XML
<Copyright>Copyright © 2022 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright>
<BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri>
<StatusCode>200</StatusCode>
<StatusDescription>OK</StatusDescription>
<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
<TraceId>8fb637a9728f4476837032284c19ea52|DU0000274E|0.0.0.0|DU0000049D</TraceId>
<ResourceSets>
<ResourceSet>
<EstimatedTotal>1</EstimatedTotal>
<Resources>
<Resource xsi:type="DistanceMatrix">
<Origins>
<Coordinate>
<Latitude>30.90232</Latitude>
<Longitude>30.17693</Longitude>
</Coordinate>
</Origins>
<Destinations>
<Coordinate>
<Latitude>31.10171</Latitude>
<Longitude>30.3131</Longitude>
</Coordinate>
</Destinations>
<Results>
<Distance>
<DepartureTime xsi:nil="true"/>
<OriginIndex>0</OriginIndex>
<DestinationIndex>0</DestinationIndex>
<TravelDistance>42.261</TravelDistance>
<TravelDuration>78.5</TravelDuration>
<TotalWalkDuration>0</TotalWalkDuration>
</Distance>
</Results>
</Resource>
</Resources>
</ResourceSet>
</ResourceSets>
</Response>```
I believe your goal is as follows.
From i'm trying to retrieve travel distance and travel time from this XML URL, you want to retrieve the values of TravelDistance and TravelDuration from the XML data using IMPORTXML.
In this case, how about the following sample formula?
Sample formula:
=IMPORTXML(A1,"//*[local-name()='TravelDistance' or local-name()='TravelDuration']")
In this case, the cell "A1" has the URL shown in your question.
Result:
For example, when you want to retrieve the values to the columns, please use TRANSPOSE like =TRANSPOSE(IMPORTXML(A1,"//*[local-name()='TravelDistance' or local-name()='TravelDuration']")).

Google Sheets Filter function returns: #ERROR - 'formula parse error'

I am using the filter function in a master sheet to grab data from 2 other sheets in google sheets. Both of these sheets go through row M.
When I run the filter functions individually, they both run.
=FILTER('Query Manual'!A2:M,'Query Manual'!A2:A<>"")
=FILTER('Query CSI'!A2:M,'Query CSI'!A2:A<>"")
But when I combine them I get a formula parse error with the #ERROR response.
=(FILTER('Query Manual'!A2:M,'Query Manual'!A2:A<>""), FILTER('Query CSI'!A2:M,'Query CSI'!A2:A<>""))
I'm not sure why I'm getting this error when I put the two together, but individually they each work fine?
={FILTER('Query Manual'!A2:M,'Query Manual'!A2:A<>""); FILTER('Query CSI'!A2:M,'Query CSI'!A2:A<>"")}
Curly Brackets instead of commas. I put on my reading glasses and realized this lol.

How to get child nodes through importxml xpath query?

I'm trying to get the seperate <td>'s to show up in Google Sheet of a <tr> that I'm importing through IMPORTXML.
This code should get my match data based on the match ID I provide, and my player ID. I feel that simply adding /* or /td to end of Xpath should work, but that's the end of my knowledge.
I tried: adding /*, /td and other to end of xPath Query but doesn't seem to work.
Even disabled JavaScript and inspected website again but to no avail.
FORMULA:
=IMPORTXML("https://www.dotabuff.com/matches/5011379854";"//tr[contains(#class,'9764136')]")
Also tried:
=IMPORTXML("https://www.dotabuff.com/matches/5011379854";"//td[parent::tr[contains(#class,'9764136')]]")
Which only gives the first of all the /td's and not the rest.
Current outputis all mushed together:
"19LemthTop (Off)ZeusCoreTop (Off) Roaminglost27108.7k127933650626.5k-183-/-5m7m21m31m"
The output that I want is separate <td> on separate lines:
"19
LemthTop (Off)ZeusCoreTop (Off) Roaminglost
2
7
10
8.7k
127
9
336
506
26.5k
-
183
-/-
5m7m21m31m"
Issue and workaround:
Although I have tried to parse the values for each row, unfortunately, it seemed that td cannot be directly parsed using a xpath with IMPORTXML as each row. But fortunately, each table can be retrieved by IMPORTHTML and also each tab can be accessed. Using them, how about the following workaround?
Retrieve a table from the URL using IMPORTHTML.
Retrieve a row including the name corresponding to 9764136 you want using a query.
Modified formula:
=TRANSPOSE(SPLIT(TEXTJOIN("#",TRUE,QUERY(IMPORTHTML(A1,"table",1), "where Col4 contains '"&IMPORTXML(A1,"//a[contains(#href,'9764136')]")&"'", 0)),"#",TRUE,TRUE))
The URL of https://www.dotabuff.com/matches/5011379854 is put to the cell "A1".
After the table was retrieved, the row is retrieved from the table by the query.
The important point of this workaround is the methodology. I think that there are various formulas for retrieving the value. So please think of above sample formula as just one of them.
Result:
Note:
If you use above formula for other URL, an error might occur. Please be careful this.
References:
IMPORTHTML
IMPORTXML
TEXTJOIN
SPLIT
TRANSPOSE

Extract sub category info from XML using Google Sheets import xml

I am trying to capture elements of an qmd file (that is xml markup) using Google Sheets importxml. Based on the reply to my question Google ImportXML from QGIS metadata file I am able to capture the primary tag.
=transpose(IMPORTXML("https://drive.google.com/uc?id=1AI2C8hQnSOuuoyJXizYBszGmpMXW8xxT&export=download","//authid"))
However I can't access some elements
<spatialrefsys>
<proj4>+proj=utm +zone=55 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs</proj4>
<srsid>2449</srsid>
<srid>28355</srid>
<authid>EPSG:28355</authid>
<description>GDA94 / MGA zone 55</description>
<projectionacronym>utm</projectionacronym>
<ellipsoidacronym>GRS80</ellipsoidacronym>
<geographicflag>false</geographicflag>
</spatialrefsys>
</crs>
<extent>
<spatial minx="322783.17999999999301508" minz="0" crs="EPSG:28355" maxx="337384.35999999998603016" miny="8170597.66000000014901161" maxz="0" dimensions="2" maxy="8181833.33999999985098839"/>
<temporal>
<period>
<start></start>
<end></end>
</period>
</temporal>
</extent>
From the //extent I want minx, maxx
for XPath I have tried "//extent", "//extent/spatial", "//extent/spatial[#*]" and "#minx"
but get no result.
You want to retrieve the values of maxx and minx using IMPORTXML().
If my understanding is correct, how about this modification?
Modified formula:
=TRANSPOSE(IMPORTXML(A1,"//spatial/#maxx | //spatial/#minx"))
https://drive.google.com/uc?id=1AI2C8hQnSOuuoyJXizYBszGmpMXW8xxT&export=download is put in the cell "A1".
In this case, //spatial/#maxx | //spatial/#minx is used as the xpath.
Attibute values can be retrieved with #.
When you want to use these values as the number, you can use this.
If you want to use the values as the text, for example, how about the following formula?
=TRANSPOSE(ARRAYFORMULA(TEXT(IMPORTXML(A1,"//spatial/#maxx | //spatial/#minx"),"0.0000000000")))
Result:
Reference:
IMPORTXML
If I misunderstood your question and this was not the result you want, I apologize.

Google BigQuery exports numeric data with wrong decimal to Sheets

In our workflow we often export results from a query on BigQuery to Google Sheets, e.g. for distribution to customers in .xlsx format.
I'm running into something weird: when explicitly casting the output of a query to numeric, the export in Sheets gives errors in for the decimals.
For example,
select
cast('12.3' as numeric),
cast('12.34' as numeric),
cast(12.056 as numeric),
cast('12.345786' as float64)
Yields the following query result in the WebUI
Row f0_ f1_ f2_ f3_
1 12.3 12.34 12.056 12.345786
However, the result in Google Sheets (again, using the WebUI, option Save to Sheets), is this:
f0_ f1_ f2_ f3_
12.3 1234 12056 12345786
Only pattern I can see is that the decimal sign is erroneously dropped when there are two or more decimals.
I really have no clue what is causing this, let alone how to fix it. Exporting the data to .csv and .json does yield the correct result.
Help, anyone?

Resources