Home > Mobile >  Importing Twilight Data to google sheets
Importing Twilight Data to google sheets

Time:06-19

I'm going to be honest, I know very little about any of this and the last time I did any form of programming was in high school 12 years ago.

I am needing to create a schedule for a low budget shoot, however, that is happening in August and I need to send out a daily schedule for the upcoming days as it changes.

I've been trying to work out how to potentially amend this so that it could include the other twilight times as well, but it keeps giving me an error:

// for an idiot, what am I doing wrong? Ideally it would be constructed in such a way that I can use it on future projects as well, placing a screen grab - it would be great if the formula can make reference to other cells and update as those cells update: screengrab of google sheets

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};
}

CodePudding user response:

The code looks more or less fine. But the cell contains the little error I think. It should be thusly:

=Solartimes($G$3,$H$3,text($B$6,"yyyy-mm-dd"),C6)

As for the code I'd propose to use switch/case for this 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 '--';
}

It works about the the same way but looks cleaner.


Just in case. The line:

var {results} = JSON.parse(json);

is the same as:

var data = JSON.parse(json);
var results = data.results;

See:

  • Related