使用API将基于坐标的日出/设置导入Google表格 [英] Import Sunrise/Set based on coordinates into Google Sheet using API
问题描述
我花了大约5个小时试图弄清楚这一点,并且陷入了最后一点。
到目前为止,我已经能够调用 https ://sunrise-sunset.org/api 返回我需要的所有数据。我遇到的问题是能够解析结果,以便将日出或日落值附加到我可以作为结果导出到Google表格中的变量。
理想情况下,在Google表格中,我可以像使用其他任何其他功能一样使用自定义函数。
< pre $
A1 = SunRiseSet(lat,long,date,type)
=5:11:12 PM
我想用type来指定函数返回的值(日出或日落)。
下面是我到目前为止的代码
函数SunRiseSet(lat,long ,日期,类型){
var response = UrlFetchApp.fetch(https://api.sunrise-sunset.org/json?lat=+ lat +& lng =+ long +& 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};
}
我确定这很简单,但到目前为止我所做的一切尝试失败。非常感谢!
修改点:
-
日出
的值在data.results.sunrise
。 -
data.results.sunrise
和data.results.sunset
可以通过使用JSON.parse()
进行解析来检索。
上面反映的修改后的脚本如下所示。
修改过的脚本:
函数SunRiseSet(lat,long,date,type){
var response = UrlFetchApp.fetch(https://api.sunrise-sunset.org/json? LAT = + LAT + &安培; LNG = +长+ &安培;日期=+日期);
var json = response.getContentText();
var data = JSON.parse(json);
var sunrise = data.results.sunrise;
var sunset = data.results.sunset;
if(type == 1){
return sunrise;
} else {
return sunset;
};
用法:
- 请将
= SunRiseSet(lat,long,date,type)
添加到电子表格中的单元格。 例如, - 当您希望
日出时
- 请将
= SunRiseSet(36.7201600,-4.4203400,2017-12-31,1)
到电子表格上的单元格。请用双引号括住date
。 - 当您想要
日落
。
- 请将
= SunRiseSet(36.7201600,-4.4203400,2017-12-31)
Spreadsheet上的单元格。除1
至类型
外,您可以使用。您也可以使用类型类似示例。
- 请将
这些参数来自日落和日出时间API 。
参考文献:
如果我误解了你的问题,请告诉我。我想修改。
I've spent about 5 hours trying to figure this out and am getting stuck on the last bit.
So far, I have been able to call the https://sunrise-sunset.org/api to return all the data I need. The issue I'm running into is being able to parse the result so that it appends the sunrise or sunset value to a variable that I can export as a result in the google sheet.
Ideally, in the google sheet, I'd be able to use the custom function like any other like this
A1=SunRiseSet(lat,long,date,type)
="5:11:12 PM"
I'd like to use type to specify what value the function should return (sunrise or sunset).
Here is the code I have so far
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};
}
I'm sure this is really easy, but so far everything I've tried has failed. Thank you so much!
How about the following modifications?
Modification points :
- The value of
sunrise
is atdata.results.sunrise
. data.results.sunrise
anddata.results.sunset
can be retrieved by parsing usingJSON.parse()
.
The modified script which reflected above is as follows.
Modified script :
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();
var data = JSON.parse(json);
var sunrise = data.results.sunrise;
var sunset = data.results.sunset;
if (type == 1) {
return sunrise;
} else {
return sunset;
};
}
Usage :
- Please put
=SunRiseSet(lat,long,date,type)
to a cell on Spreadsheet.
For example,
- When you want
sunrise
.- Please put
=SunRiseSet(36.7201600,-4.4203400,"2017-12-31",1)
to a cell on Spreadsheet. Please enclosedate
by double quotes.
- Please put
- When you want
sunset
.- Please put
=SunRiseSet(36.7201600,-4.4203400,"2017-12-31")
to a cell on Spreadsheet. You can use except for1
totype
. Also you can use withouttype
like the sample.
- Please put
These parameters are from Sunset and sunrise times API.
References :
If I misunderstand your question, please tell me. I would like to modify.
这篇关于使用API将基于坐标的日出/设置导入Google表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!