Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
¿Cómo manejar #Error en Google Sheets cuando no existe una propiedad JSON?

Estoy usando el siguiente script en Hojas de cálculo de Google para recuperar ciertas propiedades de la empresa (restringidas por el símbolo de cotización) (en JSON) de Yahoo! Finance y presentarlas en celdas adyacentes en la hoja.

Estoy recuperando 3 propiedades; esta lista se ampliará en el futuro. Actualmente estoy tratando de lograr un manejo adecuado de errores.

 function yahooFinance(symbol) { const url = 'https://query2.finance.yahoo.com/v10/finance/quoteSummary/' + encodeURI(symbol) + '?modules=price,assetProfile,summaryDetail,incomeStatementHistory,' + 'balanceSheetHistory,defaultKeyStatistics,financialData,calendarEvents,' + 'recommendationTrend,upgradeDowngradeHistory,majorHoldersBreakdown' ; const response = UrlFetchApp.fetch(url, { muteHttpExceptions: true }); const responseCode = response.getResponseCode(); if (responseCode === 200) { const quote = JSON.parse(response.getContentText()); const summaryDetail = quote.quoteSummary.result[0].summaryDetail; const divYield = summaryDetail.dividendYield.fmt || "-"; const payoutRatio = summaryDetail.payoutRatio.fmt || "-"; const marketCap = summaryDetail.marketCap.fmt || "-"; return [[ divYield , payoutRatio, marketCap ]]; } else { return "-"; } }

El problema es: si una determinada propiedad no existe para un determinado símbolo, Google Sheets devuelve un error: #Error TypeError: Cannot read property 'marketCap' of undefined (line 43).

Mi pregunta es: ¿cómo puedo devolver un - (guión) en este caso?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Reemplace la línea de código por

 const marketCap = summaryDetail.marketCap == null ? '-' : summaryDetail.marketCap.fmt;
about 4 years ago · Juan Pablo Isaza Report

0

Considere establecer una fórmula para la celda donde está vinculando el valor.

Ejemplo adaptado de esta respuesta :

 var cell = sheet.getRange("B5"); cell.setFormula("=IFNA(B5,'Invalid value')");
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!