Skip to content

Instantly share code, notes, and snippets.

@rubenhorn
Created November 26, 2019 21:28
Show Gist options
  • Save rubenhorn/9e4d43e7abfaee45e6de79abded0fdaf to your computer and use it in GitHub Desktop.
Save rubenhorn/9e4d43e7abfaee45e6de79abded0fdaf to your computer and use it in GitHub Desktop.
Google App Script for logging temperature values in Google Sheet file
// From https://github.com/varul29/SHT25_GoogleSheets_GoogleScript?source=post_page-----27912740e74a----------------------
var filename = 'temp_log_sheet';
var sheetName = 'values';
var columns = ['time', 'celsius', 'location'];
function getSheet() {
var files = DriveApp.getFilesByName(filename);
var file;
var didCreateFile = false;
if(files.hasNext()) {
file = files.next();
}
else {
file = SpreadsheetApp.create(filename, 1, columns.length);
didCreateFile = true;
}
var spreadsheet = SpreadsheetApp.openById(file.getId());
var sheet = spreadsheet.getSheets()[0];
if(didCreateFile) {
var range = sheet.getRange(1, 1, 1, columns.length);
range.setValues([columns]);
sheet.setName(sheetName);
}
return sheet;
}
function doGet(request) {
var output = ContentService.createTextOutput();
var sheet = getSheet();
var row = sheet.getLastRow();
var json;
if(row == 1) {
json = JSON.stringify({
'error' : 'No data available!'
});
}
else {
var range = sheet.getRange(row, 1, 1, columns.length);
var values = range.getValues()[0];
var data = {};
for(var i = 0; i < columns.length; i++) {
data[columns[i]] = values[i];
}
json = JSON.stringify({ 'data' : data });
}
output.append(json);
output.setMimeType(ContentService.MimeType.JSON);
return output;
}
function doPost(request) {
Logger.log(JSON.stringify(request));
var output = ContentService.createTextOutput();
var celsius = request.parameter[columns[1]];
var location = request.parameter[columns[2]];
var json;
if(celsius == null || String(Number(celsius)) == 'NaN' || typeof location != 'string') {
json = JSON.stringify({
'error' : 'Bad request!'
});
}
else {
var values = [
new Date().toISOString(),
celsius,
location
];
var sheet = getSheet();
var row = sheet.getLastRow() + 1;
var range = sheet.getRange(row, 1, 1, columns.length);
range.setValues([values]);
sheet.setFrozenRows(1);
json = JSON.stringify({
'success' : 'New record created!'
});
}
output.append(json);
output.setMimeType(ContentService.MimeType.JSON);
return output;
}
function testGET() {
var url = ScriptApp.getService().getUrl();
Logger.log(url);
var options = {
'method' : 'GET',
'followRedirects' : true,
'muteHttpExceptions': true
};
Logger.log(JSON.stringify(options));
var result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
Logger.log(result.getContentText());
}
else {
Logger.log('Error ' + result.getResponseCode());
}
}
function testPOST() {
var url = ScriptApp.getService().getUrl();
Logger.log(url);
var payload = {
'celsius' : 0,
'location' : 'test'
};
var options = {
'method' : 'POST',
'payload' : payload,
'followRedirects' : true,
'muteHttpExceptions': true
};
Logger.log(JSON.stringify(options));
var result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
Logger.log(result.getContentText());
}
else {
Logger.log('Error ' + result.getResponseCode());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment