Escribí un script de aplicación de hoja de Google que requiere algunas llamadas API. Cuando ejecuto el script dentro del editor de App Script, funciona bien.
Pero cuando llamo a la fórmula desde Google Sheets, arroja un error que dice "clave de API no válida" mientras sé que la API es correcta.
const api_key = ""; const response = UrlFetchApp.fetch("https://serpapi.com/search.json?q=" + keyword +"&hl="+"&gl="+location+"&num=100"+ "&engine = Google" + "&google_domain=google.co.uk"+"&api_key="+api_key);Aquí está todo el guión.
function GetRank(keyword, domain, location) { const response = UrlFetchApp.fetch("https://serpapi.com/search.json?q=" + keyword +"&hl="+"&gl="+location+"&num=100"+ "&engine = Google" + "&google_domain=google.co.uk"+"&api_key=xxx-xxx-xxx"); const json = response.getContentText(); const results = JSON.parse(json); const final = results.organic_results; for(let i = 0; i< final.length; i++){ rank = 'Not in Top 100'; links = final[i].link; if(links.includes(domain)){ rank = final[i].position; show = final[i].link; break; } } console.log(rank, show); return [rank, show]; }