Google Sheets 自定义函数 API:根据纬度/经度返回日出/日落 + GMT/DST 偏移量 [英] Google Sheets Custom Function API: Return Sunrise/sunset + GMT/DST Offset based on lat/long
问题描述
我想让函数根据坐标返回格式化为适当 GMT 偏移量的日出或日落.我发现了一个不同的 API,可以让您获取给定日期和位置的日出/日落时间,并提供 GMT 或 DST 偏移量.
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
对于上下文:导入日出/集使用 API 将坐标转换为 Google Sheet
推荐答案
使用上面的大部分代码,我能够让它工作.必须在 Json 查询中包含 [0].此外,计算出的带有 DST 偏移量的日出/日落已经准备就绪,因此无需进行转换.
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;
};
}
这篇关于Google Sheets 自定义函数 API:根据纬度/经度返回日出/日落 + GMT/DST 偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!