var ss = SpreadsheetApp.getActiveSpreadsheet(); var Date,Systolic,Diastolic,history,user,msg,sheet; function doGet(e){ if (e.parameter.history == "DisplayData") { return ReadSheetData(e); }} function doPost(e) { addUser(e, sheet); msg = " "; if (e.parameter.action == 'register') { user = e.parameter.user; if (ss.getSheetByName(user) == null) { sheet = ss.insertSheet(user); sheet.appendRow(["Date", "Systolic", "Diastolic"]); msg = "New User Added"; } else { msg = "User already exists"; } } else if(e.parameter.action == 'login') { user = e.parameter.user; sheet = ss.getSheetByName(user); if (sheet != null){ msg = "Sheet accessed"; } else { msg = "User not found"; } } addUser(e, user); return ContentService.createTextOutput(msg); } function addUser(e,user) { sheet = ss.getSheetByName(user); if (sheet) { Date = e.parameter.Date; Systolic = e.parameter.Systolic; Diastolic = e.parameter.Diastolic; sheet.appendRow([Date, Systolic, Diastolic]); } else { Logger.log("Sheet not found for user: " + user);} } function ReadSheetData() { sheet = ss.getSheetByName(user); var lastRow = sheet.getLastRow(); var valueRange = sheet.getRange(lastRow, 2, 1, 3).getValues()[0]; //start row, start column, end row, end column var outData = []; Date = valueRange[0]; Systolic = valueRange[1]; Diastolic = valueRange[2]; outData.push([Date.toDateString(),Systolic,Diastolic]); var outString = ContentService.createTextOutput(JSON.stringify(outData)); Logger.log(outData); return outString; }