How to make data with api output only if it is there, if not so that 0 is set, for Google Dox, here’s a piece of code

function fData(yourname) { var toonJSON = undefined; var toonJSON = UrlFetchApp.fetch("https://сайт.api" + yourname +""); Utilities.sleep(100); return toonJSON.getContentText(); } function name(yourname) { var cache = CacheService.getDocumentCache(); var toonJSON = fData(yourname); var toon = Utilities.jsonParse(toonJSON); return toon; } function inf(toonName) { var toon = name(toonName); var toonInfo = [toon.weekly[0].level]; return toonInfo; 

I understand the code should look like

 function inf(toonName) { var toon = name(toonName); var info = '0'; for (var i = 0; i < toon.weekly[i]; i++) { info = toon.weekly[i].level; } return info; 

but apparently something missed = (

  • "https://сайт.api" + yourname +"" - there is clearly an error. You need either a parameter or a slash. It can not be хттпc://сайт.apiимя - nick_n_a
  • the script works, but gives an error if there is no data, copied crookedly, there is a slash, an error in the var toonInfo = [toon.weekly [0] .level]; if there is no further data and ends with [] gives an error, but it is necessary to put 0 instead of error =) - Ivan Ivkin
  • Which line of code contains no data? Maybe it's just if ohm check and filter? - nick_n_a
  • I don’t know how to do it = (I just found the code ready, I need to correct it so that I don’t display data if I don’t have it, but put 0 instead of data - Ivan Ivkin
  • Trace - find exactly where the error is (no data). Here the trace will give an answer in much less time than they answer on the form if they answer. - nick_n_a

1 answer 1

Found for (var i = 0; i <toon.weekly [i] - the length of the array is always toon.weekly.length. You didn’t specify that this line beats an error - and that would simplify the question. Ie your question would sound correct to - how to get the length of the array. - nick_n_a yesterday

Thanks nick_n_a problem solved =)

  • Already asked your question repeatedly - use the search on the site (keep in touch) google spreadsheets - Lyuba Ivanova