Google Sheets Custom Function API: Return Sunrise/sunset + GMT/DST Offset based on lat/long

一世执手 提交于 2019-12-23 03:03:39

问题


I'd like to have the function return the sunrise or sunset formatted with the appropriate GMT offset based on the coordinates. I found a different API that lets you get sunrise/set times for a given date and location as well as providing GMT or DST offset.

function SunRiseSet(lat,long,date,type) {
 var response  
 UrlFetchApp.fetch("http://api.geonames.org/timezoneJSON?"+lat+"&lng="+long+"&username=northfacejmb"+lat+"&lng="+long+"&date="+date);

 var json = response.getContentText();
 var data = JSON.parse(json);
 var sunrise = data.dates.sunrise;
 var sunset = data.dates.sunset;
 var gmtOffset = data.gmtOffset;
 var dstOffset = data.dstOffset;

 if (type == 1) {
    return sunrise-gmtOffset;
                 } else {
      return sunset-gmtOffset;
           };
}

Any help is appreciated! - Jeremy

For context: Import Sunrise/Set based on coordinates into Google Sheet using API


回答1:


How about the following modifications?

Modification points :

  • From "GeoNames Web Services Documentation", the endpoint you want is http://api.geonames.org/timezone?lat=47.01&lng=10.2&username=demo&date=#####. (This is a sample.)
  • Data of sunrise and sunset is in the array of dates.
  • In your script, sunrise-gmtOffset sums string and number. sunrise is not calculated as the time.

The modified script which is reflected above points is as follows.

Modified script :

function SunRiseSet(lat,long,date,type) {
  var url = "http://api.geonames.org/timezoneJSON?lat="+lat+"&lng="+long+"&username=northfacejmb&date="+date;
  var response = UrlFetchApp.fetch(url);
  var json = response.getContentText();
  var data = JSON.parse(json);
  var sunrise = data.dates[0].sunrise;
  var sunset = data.dates[0].sunset;
  var gmtOffset = data.gmtOffset;
  var dstOffset = data.dstOffset;
  if (type == 1) {
    return conv(sunrise, gmtOffset);
  } else {
    return conv(sunset, gmtOffset);
  };
}

function conv(str, offset) {
  var r = str.replace(" ", "T");
  var converted = new Date(r);
  var d = new Date(converted.getTime() - offset * 60 * 60 * 1000);
  return Utilities.formatString("%s-%s-%s %s:%s:%s", d.getYear().toString(), addzero(d.getMonth()), addzero(d.getDate()), addzero(d.getHours()), addzero(d.getMinutes()), addzero(d.getSeconds()));
}

function addzero(num) {
  var str = num.toString();
  return str.length == 1 ? "0" + str : str;
}

Note :

  • sunrise-gmtOffset and sunset-gmtOffset for retrieving the result use your script. This modified script supposes these are the result you want.
  • It seems that sunrise and sunset are always the format of yyyy-MM-dd HH:mm. At conv(), this is used. If you want to use the regex, you can also use as follows.
    • var r = str.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/); var converted = new Date(parseInt(r[1], 10), parseInt(r[2], 10), parseInt(r[3], 10), parseInt(r[4], 10), parseInt(r[5], 10));

References :

  • GeoNames Web Services Documentation

If I misunderstand your question, I'm sorry.




回答2:


Using most of the code above, I was able to get it to work. had to include the [0] in the Json query. Also, the calculated sunrise/set with DST offset is already ready so no conversions are necessary.

function SunRiseSet(lat,long,date,type) {
  var url = "http://api.geonames.org/timezoneJSON?lat="+lat+"&lng="+long+"&username=northfacejmb&date="+date;
  var response = UrlFetchApp.fetch(url);
  var json = response.getContentText();
  var data = JSON.parse(json);
  var sunrise = data.dates[0].sunrise;
  var sunset = data.dates[0].sunset;
  var gmtOffset = data.gmtOffset;
  var dstOffset = data.dstOffset;
  if (type == 1) {
     return sunrise;
    } else {
     return sunset;
    };
}


来源:https://stackoverflow.com/questions/48121362/google-sheets-custom-function-api-return-sunrise-sunset-gmt-dst-offset-based

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!