Skip to content

Instantly share code, notes, and snippets.

@Tech500
Last active July 9, 2023 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tech500/f4abd31e08a99243edefbedf2a4eb9c8 to your computer and use it in GitHub Desktop.
Save Tech500/f4abd31e08a99243edefbedf2a4eb9c8 to your computer and use it in GitHub Desktop.
// This is a Google Script that retrieves environmental data (datetime, temperature, heat index, humidity, dewpoint, pressure, and a difference value) from a web
// interface and appends it to a Google Spreadsheet. The sheet name is based on the current month and year, and a new sheet will be created with an additional number
// if the sheet with the same name already exists. If it's the last day of the current month, a new sheet for the next month will be created. The script has several
// functions, including getting the name of the current month, creating a new sheet with the specified name, adding headers to the new sheet, and appending the data
// to the sheet. Script is designed to create a new sheet at the end of each month with the name of the month and the year.
// Google Sheets App script created in parts by Google's Bard. Additional code by William Lucid 07/02/2023
// ESP32 Project repository: https://github.com/Tech500/CameraRainGauge
// "googleSheet" is the function in "CameraRainGauge.ino" that send data to Google Sheets "BME280 Logging."
const sheet_id = "Google sheet_id";
function doGet(e) {
const dtstamp = e.parameter.dtstamp;
const temp = e.parameter.temp;
const heatindex = e.parameter.heatindex;
const humidity = e.parameter.humidity;
const dewpoint = e.parameter.dewpoint;
const pressure = e.parameter.pressure;
const diff = e.parameter.diff;
const headers = ['dtstamp', 'temp', 'heatindex', 'humidity', 'dewpoint', 'pressure', 'diff'];
const data = [
[dtstamp, temp, heatindex, humidity, dewpoint, pressure, diff]
];
var now = new Date();
var month = now.getMonth();
var monthName = getMonthNames();
var year = now.getFullYear();
var endOfMonth = new Date(year, month + 1, 0);
function getMonthNames() {
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthName = months[new Date().getMonth()];
return monthName;
}
var sheetName = monthName + " " + now.getFullYear();
var ss = SpreadsheetApp.openById(sheet_id);
var sheet = ss.getSheetByName(sheetName);
testForEndOfMonth();
function MONTH(date) {
return date.getMonth();
}
function createNewSheet() {
console.log("createNewSheet");
var month = MONTH(new Date());
var monthName = getMonthNames()[month];
console.log(sheetName);
var sheet = SpreadsheetApp.getActive().insertSheet(sheetName);
sheet.appendRow(headers);
sheet.appendRow([dtstamp,temp,heatindex,humidity,dewpoint,pressure,diff]);
}
function logData(dtstamp, temp, heatindex, humidity, dewpoint, pressure, diff) {
console.log("logData");
var sheet = SpreadsheetApp.openById(sheet_id).getSheetByName(sheetName);
if (!sheet) {
sheet = SpreadsheetApp.openById(sheet_id).insertSheet(sheetName);
}
for (var i = 0; i < data.length; i++) {
// Append the data to sheet
sheet.appendRow([dtstamp,temp,heatindex,humidity,dewpoint,pressure,diff]);
}
}
function testForEndOfMonth() {
console.log("testForEndOfMonth");
if (now.getDate() === endOfMonth.getDate()) { /* Normal use for existing sheet */
//if (now = endOfMonth){ /* test creating new sheet */
console.log("createdSheet");
createNewSheet();
}else{
console.log("appendingData");
logData(dtstamp, temp, heatindex, humidity, dewpoint, pressure, diff);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment