Skip to content

Instantly share code, notes, and snippets.

@gdscbot
Created March 5, 2024 05:32
Show Gist options
  • Save gdscbot/550bb855b7b4f072eb49cb1b8da4984c to your computer and use it in GitHub Desktop.
Save gdscbot/550bb855b7b4f072eb49cb1b8da4984c to your computer and use it in GitHub Desktop.
Ido's Google Apps script Workshop
function isFib(x) {
let a=1, b=1;
while(b<=x){
if(b==x){
return true;
}
let temp = b;
b+=a;
a=temp;
}
return false;
}
function firstNFib(n) {
let a=1, b=1;
const res = [a, b];
if(n==1) {
return [1];
}
if(n<=0){
return [];
}
while(res.length<n){
let temp = b;
b+=a;
a=temp;
res.push(b);
}
return res;
}
function addZeroPadding(string, k=2){
while(string.toString().length < k){
string = "0"+string;
}
return string;
}
function formatDate(date) {
return `${date.getFullYear()}-${addZeroPadding(date.getMonth()+1)}-${addZeroPadding(date.getDate())}`
}
function weatherDate(dates) {
const mapped_date = dates.flat().filter(x=>x && x < new Date());
if(mapped_date.length === 0){
return [];
}
if(!mapped_date.every(x=>x instanceof Date)) {
throw new Error("Invalid date in data");
}
const minDate=formatDate(new Date(Math.min(...mapped_date)));
const maxDate=formatDate(new Date(Math.max(...mapped_date)));
// https://archive-api.open-meteo.com/v1/archive?latitude=43.5483&longitude=79.6627&start_date=start_date &end_date=end_date&daily=temperature_2m_mean&timezone=America%2FNew_York
const url = `https://archive-api.open-meteo.com/v1/archive?latitude=43.5483&longitude=79.6627&start_date=${minDate}&end_date=${maxDate}&daily=temperature_2m_mean&timezone=America%2FNew_York`;
const res = UrlFetchApp.fetch(url);
if(res.getResponseCode() != 200){
throw new Error("API request failed with status: " + res.getResponseCode());
}
const data = JSON.parse(res.getContentText());
const dict = {};
data.daily.time.forEach((x,i)=> {
dict[x] = data.daily.temperature_2m_mean[i];
});
return dates.map(x=>dict[formatDate(new Date(x))]);
}
function getCurrentTemp() {
const url = "https://api.open-meteo.com/v1/forecast?latitude=43.5483&longitude=79.6627&current=temperature_2m";
const res = UrlFetchApp.fetch(url);
if(res.getResponseCode() != 200){
throw new Error("API request failed with status: " + res.getResponseCode());
}
const data = JSON.parse(res.getContentText());
return data.current.temperature_2m;
}
function logCurrentDate() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data Gathering").appendRow([new Date(), getCurrentTemp()]);
}
function pressButton() {
logCurrentDate();
var html = HtmlService.createHtmlOutputFromFile("index");
SpreadsheetApp.getUi().showModalDialog(html, "Logged data!")
}
function showCurrentTemp() {
Browser.msgBox("Curent tempature is " + getCurrentTemp() + "C")
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("Weather Data")
.addItem("Get current tempature", "showCurrentTemp")
.addSeparator()
.addSubMenu(ui.createMenu("Log Data")
.addItem("Log data with HTML", "pressButton")
.addItem("Log data", "logCurrentDate")).addToUi();
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body style="text-align:center; overflow:hidden; height:100vh; background-color:white; width: 100vw">
<iframe width="200" height="200" src="https://www.youtube.com/embed/1ARb7r0yY9k?si=L49_KZuOBYB5uQgo&autoplay=1" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.9.2/dist/confetti.browser.min.js"></script>
<script>confetti({
particleCount: 500,
spread: 60,
ticks: 500
})</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment