Forum Discussion
Expression.SyntaxError: Token Eof esperado.
Obtención de Expression.SyntaxError: Token Eof esperado. error en el siguiente código. Lo he comprobado todo y me parece correcto. ¿Cuál podría ser el problema aquí?
= let
GoogleDistance = (origin as text, destination as text) =>
let
apiKey = key,
url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" & Text.Replace(origin, " ", "+") & "&destinations=" & Text.Replace(destination, " ", "+") & "&key=" & apiKey,
attemptedSource = try Json.Document(Web.Contents(url)) otherwise null,
isValid = attemptedSource <> null and "rows" in attemptedSource,
Rows = if isValid then attemptedSource[rows] else null,
Elements = if Rows <> null and List.Count(Rows) > 0 and Record.HasFields(Rows{0}, "elements") then Rows{0}[elements] else null,
Distance = if Elements <> null and List.Count(Elements) > 0 and Record.HasFields(Elements{0}, "distance") then Elements{0}[distance][text] else "Unavailable",
Duration = if Elements <> null and List.Count(Elements) > 0 and Record.HasFields(Elements{0}, "duration") then Elements{0}[duration][text] else "Unavailable"
in
[Distance = Distance, Duration = Duration]
in
GoogleDistanceExpression.SyntaxError: Token Eof esperado.
5 Replies
- Syndicate_Admin
Administrator
Hola @anwernuman ,
Se espera que Expression.SyntaxError: Token Eof. El error en Power Query suele sugerir un carácter que falta o está mal colocado, como un paréntesis de cierre que falta, una palabra clave que falta o una referencia de variable con formato incorrecto. Uno de los posibles problemas del código es que la clave de variable no está definida. En la línea:
apiKey = key,si la clave no está asignada en ninguna parte, Power Query producirá un error. Asegúrese de que la clave esté definida correctamente o reemplácela por una cadena de clave de API real.
Otro problema es el = antes de let al principio del código. La instrucción let no debe tener un = antes. La sintaxis correcta es definir la función directamente sin un signo igual:
letEn lugar de
= letAdemás, asegúrese de que Web.Contents(url) devuelva un JSON válido. Si no tiene el formato correcto, se producirá un error en Json.Document(Web.Contents(url). Puedes probar la respuesta de la API mediante Text.FromBinary(Web.Contents(url)) para inspeccionar la respuesta sin procesar.
Esta es una versión corregida de su código:
let GoogleDistance = (origin as text, destination as text) => let apiKey = "your_api_key_here", // Ensure apiKey is defined properly url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" & Text.Replace(origin, " ", "+") & "&destinations=" & Text.Replace(destination, " ", "+") & "&key=" & apiKey, attemptedSource = try Json.Document(Web.Contents(url)) otherwise null, isValid = attemptedSource <> null and "rows" in attemptedSource, Rows = if isValid then attemptedSource[rows] else null, Elements = if Rows <> null and List.Count(Rows) > 0 and Record.HasFields(Rows{0}, "elements") then Rows{0}[elements] else null, Distance = if Elements <> null and List.Count(Elements) > 0 and Record.HasFields(Elements{0}, "distance") then Elements{0}[distance][text] else "Unavailable", Duration = if Elements <> null and List.Count(Elements) > 0 and Record.HasFields(Elements{0}, "duration") then Elements{0}[duration][text] else "Unavailable" in [Distance = Distance, Duration = Duration] in GoogleDistanceEsta versión elimina el = innecesario, garantiza que apiKey se asigne correctamente y mantiene la sintaxis correctamente estructurada. Pruebe esto y verifique si el problema se resuelve.
Saludos
- Syndicate_Admin
Administrator
- Syndicate_Admin
Administrator
La clave está definida correctamente, pero sigo recibiendo el error
- Syndicate_Admin
Administrator
Esta línea de código está causando un problema. M no tiene un equivalente directo del operador IN en DAX.
"rows" in attemptedSourceSi está comprobando si attemptedSource tiene un campo llamado filas, use
Record.HasFields(attemptedSource, "rows"),- Syndicate_Admin
Administrator
Eres increíble. Esto funcionó