Last active
January 22, 2023 13:32
-
-
Save Tech500/09ce3e83757d6dc87679819c37866dc8 to your computer and use it in GitHub Desktop.
This file contains 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
2nd experience with ChatGPT from OpenAI: | |
/// 2nd Input: create google sheets script for a year of data by the month of bme280 sensor data | |
function createSheets() { | |
var ss = SpreadsheetApp.getActive(); | |
var data = ss.getDataRange().getValues(); | |
// Create a map to store the sheets by month | |
var sheets = {}; | |
// Iterate through the data, adding a new sheet for each month | |
for (var i = 0; i < data.length; i++) { | |
var date = new Date(data[i][0]); | |
var month = date.getMonth() + 1; | |
var year = date.getFullYear(); | |
var sheetName = month + "/" + year; | |
// Check if the sheet already exists | |
if (!sheets[sheetName]) { | |
var sheet = ss.insertSheet(sheetName); | |
sheets[sheetName] = sheet; | |
// Add the headers | |
sheet.appendRow(["Date", "Temperature", "Humidity", "Pressure"]); | |
} | |
// Add the data to the appropriate sheet | |
sheets[sheetName].appendRow([data[i][0], data[i][1], data[i][2], data[i][3]]); | |
} | |
} | |
/// 3d Input: create google sheets script for a year of data by the month of bme280 sensor data for every 15 minutes | |
// Set up a trigger to run the script every 15 minutes | |
ScriptApp.newTrigger("getBME280Data") | |
.timeBased() | |
.everyMinutes(15) | |
.create(); | |
function getBME280Data() { | |
// Get the current date and time | |
var now = new Date(); | |
// Connect to the BME280 sensor | |
var bme280 = connectToBME280(); | |
// Get the temperature, humidity, and pressure data from the sensor | |
var temp = bme280.getTemperature(); | |
var humidity = bme280.getHumidity(); | |
var pressure = bme280.getPressure(); | |
// Get the current sheet for the month | |
var sheet = getSheetForMonth(now.getMonth()); | |
// Append the data to the sheet | |
sheet.appendRow([now, temp, humidity, pressure]); | |
} | |
function connectToBME280() { | |
// Connect to the BME280 sensor using I2C communication | |
// Replace with actual I2C code | |
return { | |
getTemperature: function(){ | |
return 25; | |
}, | |
getHumidity: function(){ | |
return 50; | |
}, | |
getPressure: function(){ | |
return 1013; | |
} | |
} | |
} | |
function getSheetForMonth(month) { | |
// Get the name of the sheet for the given month | |
var sheetName = getMonthName(month) + " " + now.getFullYear(); | |
// Get the sheet with the specified name, or create a new one if it doesn't exist | |
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName); | |
if (sheet == null) { | |
sheet = SpreadsheetApp.getActive().insertSheet(sheetName); | |
} | |
return sheet; | |
} | |
function getMonthName(month) { | |
// Get the name of the month for the given month number | |
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; | |
return monthNames[month]; | |
} | |
/// 4th Input: change function createNewSheet to add addtional sheet for same month | |
// Set up a trigger to run the script every 15 minutes | |
ScriptApp.newTrigger("getBME280Data") | |
.timeBased() | |
.everyMinutes(15) | |
.create(); | |
function getBME280Data() { | |
// Get the current date and time | |
var now = new Date(); | |
// Connect to the BME280 sensor | |
var bme280 = connectToBME280(); | |
// Get the temperature, humidity, and pressure data from the sensor | |
var temp = bme280.getTemperature(); | |
var humidity = bme280.getHumidity(); | |
var pressure = bme280.getPressure(); | |
// Get the current sheet for the month | |
var sheet = getSheetForMonth(now.getMonth()); | |
try { | |
// Append the data to the sheet | |
sheet.appendRow([now, temp, humidity, pressure]); | |
} catch (error) { | |
// If there is an error, create a new sheet with an additional number to the month name | |
var sheetName = getMonthName(now.getMonth()) + " " + now.getFullYear(); | |
var newSheetName = sheetName; | |
var i = 2; | |
while(SpreadsheetApp.getActive().getSheetByName(newSheetName) != null) { | |
newSheetName = sheetName + " " + i; | |
i++; | |
} | |
sheet = SpreadsheetApp.getActive().insertSheet(newSheetName); | |
// Add headers to the new sheet | |
sheet.appendRow(["Date", "Temperature", "Humidity", "Pressure"]); | |
sheet.appendRow([now, temp, humidity, pressure]); | |
} | |
} | |
function connectToBME280() { | |
// Connect to the BME280 sensor using I2C communication | |
// Replace with actual I2C code | |
return { | |
getTemperature: function(){ | |
return 25; | |
}, | |
getHumidity: function(){ | |
return 50; | |
}, | |
getPressure: function(){ | |
return 1013; | |
} | |
} | |
} | |
function getSheetForMonth(month) { | |
// Get the name of the sheet for the given month | |
var sheetName = getMonthName(month) + " " + now.getFullYear(); | |
// Get the sheet with the specified name, or create a new one if it doesn't exist | |
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName); | |
if (sheet == null) { | |
sheet = SpreadsheetApp.getActive().insertSheet(sheetName); | |
sheet.appendRow(["Date", "Temperature", "Humidity", "Pressure"]); | |
} | |
return sheet; | |
} | |
function getMonthName(month) { | |
// Get the name of the month for the given month number | |
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; | |
return month; | |
} | |
/// 5th Input: change function getBME280Data to use data sent from arduino sketch | |
// Set up a trigger to run the script every 15 minutes | |
ScriptApp.newTrigger("getBME280Data") | |
.timeBased() | |
.everyMinutes(15) | |
.create(); | |
function getBME280Data() { | |
// Get the current date and time | |
var now = new Date(); | |
// Get the temperature, humidity, and pressure data sent from the Arduino sketch | |
var data = getDataFromArduino(); | |
var temp = data.temp; | |
var humidity = data.humidity; | |
var pressure = data.pressure; | |
// Get the current sheet for the month | |
var sheet = getSheetForMonth(now.getMonth()); | |
try { | |
// Append the data to the sheet | |
sheet.appendRow([now, temp, humidity, pressure]); | |
} catch (error) { | |
// If there is an error, create a new sheet with an additional number to the month name | |
var sheetName = getMonthName(now.getMonth()) + " " + now.getFullYear(); | |
var newSheetName = sheetName; | |
var i = 2; | |
while(SpreadsheetApp.getActive().getSheetByName(newSheetName) != null) { | |
newSheetName = sheetName + " " + i; | |
i++; | |
} | |
sheet = SpreadsheetApp.getActive().insertSheet(newSheetName); | |
// Add headers to the new sheet | |
sheet.appendRow(["Date", "Temperature", "Humidity", "Pressure"]); | |
sheet.appendRow([now, temp, humidity, pressure]); | |
} | |
} | |
function getDataFromArduino() { | |
// Replace with the code to receive data from the Arduino sketch | |
// Example: | |
// var data = JSON.parse(Serial.read()); | |
// return data; | |
return {temp:20, humidity:40, pressure:1013} | |
} | |
function getSheetForMonth(month) { | |
// Get the name of the sheet for the given month | |
var sheetName = getMonthName(month) + " " + now.getFullYear(); | |
// Get the sheet with the specified name, or create a new one if it doesn't exist | |
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName); | |
if (sheet == null) { | |
sheet = SpreadsheetApp.getActive().insertSheet(sheetName); | |
sheet.appendRow(["Date", "Temperature", "Humidity", "Pressure"]); | |
} | |
return sheet; | |
} | |
function getMonthName(month) { | |
// Get the name of the month for the given month number | |
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; | |
return monthNames[month]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment