Skip to content

Instantly share code, notes, and snippets.

@cattaka
Created January 23, 2020 13:15
Show Gist options
  • Save cattaka/f369c412261e147700ebbac3e39b857d to your computer and use it in GitHub Desktop.
Save cattaka/f369c412261e147700ebbac3e39b857d to your computer and use it in GitHub Desktop.
function doGet(e) {
var now = new Date();
var sheet = getSheetOfDate(now);
var rawWeightFrom = e && e.parameter && e.parameter.raw_weight_from;
var rawWeightTo = e && e.parameter && e.parameter.raw_weight_to;
var etc = e && e.parameter && e.parameter.etc;
var response;
if (rawWeightTo) {
// https://stackoverflow.com/a/17637159
var avals = sheet.getRange("A1:A").getValues();
var nextRow = avals.filter(String).length + 1;
sheet.getRange(nextRow, 1).setValue(now);
sheet.getRange(nextRow, 2).setValue(rawWeightFrom);
sheet.getRange(nextRow, 3).setValue(rawWeightTo);
sheet.getRange(nextRow, 4).setValue(etc);
response = { status: 'success' };
} else {
response = { status: 'failed', message: 'Parameter raw_weight is missing' };
}
var output = ContentService.createTextOutput();
output.setMimeType(ContentService.MimeType.JSON);
output.setContent(JSON.stringify(response));
return output;
}
function getSheetOfDate(date) {
var ymd = getNowYMD(date);
var spreadsheet = SpreadsheetApp.getActive();
var sheets = spreadsheet.getSheets();
var sheet = null;
for (var i=0;i<sheets.length;i++) {
if (sheets[i].getName() == ymd) {
sheet = sheets[i];
break;
}
}
if (sheet == null) {
sheet = spreadsheet.insertSheet();
sheet.setName(ymd);
sheet.getRange(1, 1).setValue('timestamp');
sheet.getRange(1, 2).setValue('raw_weight_from');
sheet.getRange(1, 3).setValue('raw_weight_to');
sheet.getRange(1, 4).setValue('etc');
var columns = sheet.getMaxColumns();
if (columns > 10) {
sheet.deleteColumns(10, columns - 10)
}
}
return sheet;
}
function getNowYMD(date) {
var y = date.getFullYear();
var m = ("00" + (date.getMonth()+1)).slice(-2);
var d = ("00" + date.getDate()).slice(-2);
var result = y + "-" + m;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment