Skip to content

Instantly share code, notes, and snippets.

@Tech500
Last active February 1, 2023 11:34
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/296acfd008c67ea111a97056aaf4419e to your computer and use it in GitHub Desktop.
Save Tech500/296acfd008c67ea111a97056aaf4419e to your computer and use it in GitHub Desktop.
// BME280 ChatGPT6 Google App Script project
// This Google App Script collects data from a web service and appends it to a Google Sheet.
// It uses the doGet() function to parse the data and append it to the sheet.
// The getSheetForMonth() function is used to get the current sheet for the month or create
// a new one if it doesn't exist.
// The getMonthName() function is used to get the name of the month for the given month number.
// The addNewSheetAtEndOfMonth() function is used to create a new sheet at the end of each month,
// allowing the script to collect data for a full year.
// Google App Script was produced using OpenAI, ChatGPT and ChatGPT-3. Code modified by William Lucid
var sheet_id = "replace with sheet_id";
const now = new Date();
// Get the current sheet for the month
var sheet = getSheetForMonth(now.getMonth());
function doGet(e){
var ss = SpreadsheetApp.openById(sheet_id);
var dtstamp = String(e.parameter.dtstamp);
var temp = Number(e.parameter.temp);
var heatindex = Number(e.parameter.heatindex);
var humidity = Number(e.parameter.humidity);
var dewpoint = Number(e.parameter.dewpoint);
var pressure = Number(e.parameter.pressure);
var diff = Number(e.parameter.diff);
// Call the addNewSheetAtEndOfMonth() function
addNewSheetAtEndOfMonth();
try {
// Append the data to the sheet
sheet.appendRow([dtstamp,temp,heatindex,humidity,dewpoint,pressure,diff]);
} 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([dtstamp,temp,heatindex,humidity,dewpoint,pressure,diff]);
sheet.appendRow([dtstamp,temp,heatindex,humidity,dewpoint,pressure,diff]);
}
}
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(["dtstamp","temp","heatindex","humidity","dewpoint","pressure","diff"]);
}
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];
}
function addNewSheetAtEndOfMonth() {
var now = new Date();
if (now.getDate() === new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate()) {
var sheetName = getMonthName(now.getMonth() + 1) + " " + now.getFullYear();
var sheet = SpreadsheetApp.getActive().insertSheet(sheetName);
sheet.appendRow(["dtstamp","temp","heatindex","humidity","dewpoint","pressure","diff"]);
}
}
// OpenAI, ChatGPT-3 input: "Test google app script will it collect data monthly for a year."
// "Yes, this Google App Script will collect data monthly for a year. The script uses the getSheetForMonth() function
// to get the current sheet for the month or create a new one if it doesn't exist. Additionally, the addNewSheetAtEndOfMonth()
// function is used to create a new sheet at the end of each month, allowing the script to collect data for a full year."
// Data for this GS Script gets inputed from this Arduino function:
void googleSheet()
{
char fahr[7];
dtostrf(temperature, 6, 1, fahr);
char heatindex[7];
dtostrf(heat, 6, 1, heatindex);
char humid[7];
dtostrf(hum, 6, 1, humid);
char dewpoint[7];
dtostrf(dew,6, 1, dewpoint);
char barometric[8];
dtostrf(currentPressure, 7, 3, barometric);
char diff[7];
dtostrf(difference, 6, 3, diff);
String data = "&dtstamp=" + dtStamp
+ "&temp=" + fahr
+ "&heatindex=" + heatindex
+ "&humidity=" + humid
+ "&dewpoint=" + dewpoint
+ "&pressure=" + barometric
+ "&diff=" + diff;
//The first column are the headers of the Google Sheet.
//Second column are the matching data values to the header in first column.
String urlFinal = "https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+data;
//GOOGLE_SCRIPT_ID = Google Sheet Script deployment id.
Serial.print("POST data to spreadsheet:");
Serial.println(urlFinal);
urlFinal.replace(" ", "%20"); //remove spaces in urlFinal URL.
HTTPClient http;
http.begin(urlFinal.c_str());
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
//getting response from google sheet
String payload;
if (httpCode > 0) {
payload = http.getString();
Serial.println("Payload: "+payload);
}
http.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment