Last active
January 13, 2025 14:21
-
-
Save Tech500/c59fd445f0a5f41975a0bfe7ce55ee89 to your computer and use it in GitHub Desktop.
Perpetual data logging to Google Sheets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const sheet_id = "1hBT_nM_UehFrAa7o_lUW-eZEOpnFSgUZqsDaygV7XgQ"; | |
| const headers = ['dtstamp', 'temp', 'heatindex', 'humidity', 'dewpoint', 'pressure', 'diff']; | |
| const now = new Date(); | |
| const sheetName = `${getMonthNames(now.getMonth())} ${now.getFullYear()}`; | |
| const ss = SpreadsheetApp.openById(sheet_id); | |
| let sheet = ss.getSheetByName(sheetName); | |
| function doGet(e) { | |
| //Change var name = e.paraameter.value for values to be logged | |
| var dtstamp = e.parameter.dtstamp; | |
| var temp = e.parameter.temp; | |
| var heatindex = e.parameter.heatindex; | |
| var humidity = e.parameter.humidity; | |
| var dewpoint = e.parameter.dewpoint; | |
| var pressure = e.parameter.pressure; | |
| var diff = e.parameter.diff; | |
| // data = var to be appended to every row of Goole Sheet. | |
| const data = [dtstamp, temp, heatindex, humidity, dewpoint, pressure, diff]; | |
| // Logs data to the console | |
| console.log(dtstamp, temp, heatindex, humidity, dewpoint, pressure, diff); | |
| //Checks for end of the month; if true creates new sheet. | |
| if (isEndOfMonth(now)) { | |
| createNewSheet(sheetName, ss, data); | |
| } else { | |
| logData(sheet, data); | |
| } | |
| } | |
| //Retreves name of month. | |
| function getMonthNames(index) { | |
| const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; | |
| return index !== undefined ? months[index] : months; | |
| } | |
| //Finds date for the end of the month | |
| function isEndOfMonth(date) { | |
| const endOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0); | |
| return date.getDate() === endOfMonth.getDate(); | |
| } | |
| //Creates new sheet with correct month. | |
| function createNewSheet(sheetName, ss, data) { | |
| let sheet = ss.getSheetByName(sheetName); | |
| if (!sheet) { | |
| sheet = ss.insertSheet(sheetName); | |
| sheet.appendRow(headers); | |
| } | |
| sheet.appendRow(data); | |
| } | |
| //If not end of month date, opens sheet, writes data and apends data to row. | |
| function logData(sheet, data) { | |
| if (!sheet) { | |
| const ss = SpreadsheetApp.openById(sheet_id); | |
| sheet = ss.insertSheet(sheetName); | |
| sheet.appendRow(headers); | |
| } | |
| sheet.appendRow(data); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ESP32 Code snippet for sending data to Google Sheets:
// Global
String GOOGLE_SCRIPT_ID = "Put Deployment Id between quotes xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Copy Deployment Id when deploying script.
}
//Function
void googleSheet() // Sends data to Google Sheets
{
}