小编Jer*_*man的帖子

使用 API 将基于坐标的日出/日落导入 Google Sheet

我花了大约 5 个小时试图解决这个问题,但我陷入了最后一点。

到目前为止,我已经能够调用https://sunrise-sunset.org/api返回我需要的所有数据。我遇到的问题是能够解析结果,以便将日出或日落值附加到一个变量中,我可以将其作为结果导出到谷歌表中。

理想情况下,在谷歌表格中,我能够像其他这样的函数一样使用自定义函数

A1=SunRiseSet(lat,long,date,type)  
Run Code Online (Sandbox Code Playgroud)

=“下午 5:11:12”

我想使用 type 来指定函数应返回什么值(日出或日落)。

这是我到目前为止的代码

function SunRiseSet(lat,long,date,type) {
 var response = UrlFetchApp.fetch("https://api.sunrise-sunset.org/json?lat="+lat+"&lng="+long+"&date="+date);  
  var json = response.getContentText();
  Logger.log(json);

  var data = JSON.stringify(json);
  Logger.log(data);
  var data = json;

  var sunrise = data.sunrise;

  var sunset = data.results.sunset

 type=1; 
  if(type==1){
   return this.sunrise}
  else{
   return this.sunset};
}
Run Code Online (Sandbox Code Playgroud)

我确信这真的很容易,但到目前为止我尝试过的一切都失败了。太感谢了!

javascript json google-sheets google-apps-script

5
推荐指数
1
解决办法
3854
查看次数