Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

javascript - Google script for Alphavantage gives weird results

I am trying to make a google script which i want to use in Google sheets. I followed a tutorial video on youtube and i had the idea that my script was working well.

But then two strange things happen, and i can't figure out why.

Below is my code, this code is exactly doing what i want. It is showing the EPS of IBM in the google sheets and also in the log. However the moment i change the APIkey from "demo" to my own APIkey it is not working anymore. At that moment it is still showing the EPS in the LOG, but i will get an empty cell in Google Sheets.

I have no idea why that happens.

/**
 * Imports api data from alphavantage 
 * @customfunction
 */
function apiav(a) {
  var res = UrlFetchApp.fetch(
    'https://www.alphavantage.co/query?function=OVERVIEW&symbol=IBM&apikey=demo'
  );
  var content = res.getContentText();
  var json = JSON.parse(content);
  var overviewvalue = json['EPS'];
  Logger.log(overviewvalue);
  return overviewvalue;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try adding {validateHttpsCertificates: false} to your UrlFetchApp.fetch() to ignores any invalid certificates for HTTPS requests.

Your code should look like this:

function apiav(a) {
  var res = UrlFetchApp.fetch('https://www.alphavantage.co/query?function=OVERVIEW&symbol=IBM&apikey=ABCDEFGH', {validateHttpsCertificates: false});
  var content = res.getContentText();
  var json = JSON.parse(content);
  var overviewvalue = json['EPS'];
  Logger.log(overviewvalue);
  return overviewvalue;
}

Output:

enter image description here

Reference:

UrlFetchApp


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...