老實說,我對此知之甚少,上一次我做任何形式的編程是在 12 年前的高中。
我需要為低預算拍攝創建一個時間表,但是,這發生在 8 月,我需要發送一個未來幾天的每日時間表,因為它會發生變化。
我一直在努力研究如何可能修改它,以便它也可以包括其他暮光之城,但它一直給我一個錯誤:
// 對于一個白癡,我做錯了什么?理想情況下,它將以這樣一種方式構建,以便我也可以在未來的專案中使用它,放置一個螢屏抓取 - 如果公式可以參考其他單元格并隨著這些單元格的更新而更新,那就太好了: 谷歌表格的螢屏截圖
function SolarTimes(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;
var civil_dawn = data.results.civil_twilight_begin;
var civil_dusk = data.results.civil_twilight_end;
var nautical_dawn = data.results.nautical_twilight_begin;
var nautical_dusk = data.results.nautical_twilight_end;
var day_length = data.results.day_length;
{ if (type == "Sunrise")
return sunrise;
else if (type == "Sunset")
return sunset;
else if (type = "Civil_Dawn")
return civildawn;
else if (type == "Civil_Dusk")
return civildusk;
else if (type == "Nautical_Dawn")
return nauticaldawn;
else if (type == "Nautical_Dusk")
return nauticaldusk;
else
return day_length};
}
uj5u.com熱心網友回復:
這是一個處理數字日期并驗證引數的實作。
/**
* Gets the sunrise or sunset time or day length at a location on a date.
*
* @param {36.7201600} latitude The north–south position to use.
* @param {-4.4203400} longitude The east-west position to use.
* @param {"sunrise"} type One of "all", "sunrise", "sunset", "civil_dawn", "civil_dusk", "nautical_dawn", "nautical_dusk" or "day_length".
* @param {D2} date Optional. Defaults to the current date.
* @return {String|String[][]} The requested time as a text string. With "all", an array of types and times.
* @customfunction
*/
function Daylight(latitude, longitude, type, date) {
// see https://stackoverflow.com/a/72675674/13045193
// note: api.sunrise-sunset.org/json does not handle polar night nor midnight sun correctly
'use strict';
const [lat, lng, key, dateString] = _validate(arguments);
const url = `https://api.sunrise-sunset.org/json?lat=${lat}&lng=${lng}&date=${dateString}`;
const { results, status } = JSON.parse(UrlFetchApp.fetch(url).getContentText());
if (key === 'all') {
return Object.keys(results).map(key => [key, results[key]]);
}
return results[key] ?? NaN;
/**
* Validates function arguments.
*/
function _validate(args) {
if (args.length < 3 || args.length > 4) {
throw new Error(`Wrong number of arguments to Daylight. Expected 3 or 4 arguments, but got ${args.length} arguments.`);
}
const lat = Number(latitude);
const lng = Number(longitude);
if (latitude === '' || Number.isNaN(lat) || lat > 90 || lat < -90 || longitude === '' || Number.isNaN(lng) || lng > 180 || lng < -180) {
throw new Error(`Daylight expected a numeric latitude [-90, 90] and longitude [-180, 180], but got the ${typeof latitude} '${latitude}' and the ${typeof longitude} '${longitude}'.`);
}
return [
lat,
lng,
type.toLowerCase().replace('dawn', 'twilight_begin').replace('dusk', 'twilight_end'),
_dateToISO8601(date),
];
}
/**
* Parses a date or string to an ISO8601 date string.
*/
function _dateToISO8601(date) {
if (date === '' || (date == null)) {
date = new Date();
}
if (typeof date === 'string') {
date = new Date(Date.parse(date));
}
if (Object.prototype.toString.call(date) !== '[object Date]') {
throw new Error(`Daylight expected a date, but '${date}' is a ${typeof date}.`);
}
return Utilities.formatDate(date, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), 'yyyy-MM-dd');
}
}
uj5u.com熱心網友回復:
代碼看起來或多或少很好。但是單元格包含我認為的小錯誤。應該是這樣的:
=Solartimes($G$3,$H$3,text($B$6,"yyyy-mm-dd"),C6)
至于我建議switch/case用于這種情況的代碼:
function SolarTimes(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 {results} = JSON.parse(json);
switch (type.toLowerCase()) {
case ('sunrise'): return results.sunrise;
case ('sunset'): return results.sunset;
case ('civil_dawn'): return results.civil_twilight_begin;
case ('civil_dusk'): return results.civil_twilight_end;
case ('nautical_dawn'): return results.nautical_twilight_begin;
case ('nautical_dusk'): return results.nautical_twilight_end;
case ('day_length'): return results.day_length;
}
return '--';
}
它的作業方式大致相同,但看起來更干凈。
以防萬一。該行:
var {results} = JSON.parse(json);
是相同的:
var data = JSON.parse(json);
var results = data.results;
看:
- 解構賦值
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493175.html
標籤:json api 谷歌应用脚本 进口 谷歌表格公式
