Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
56 lines (52 sloc) 2.88 KB
function doPost(e) {
var lock = LockService.getPublicLock();
try {
var database = SpreadsheetApp.openById(""); //insert your database ID between the quotation marks
var splicedDataset = SpreadsheetApp.openById(""); //insert your spliced database id between the quotation marks
var compliance = SpreadsheetApp.openById(""); //insert your compliance database id between the quotation marks
var PID = e.parameters['participant_id'];
var sheet = database.getSheetByName(PID);
var splicedSheet = splicedDataset.getSheetByName(PID);
if (database.getSheetByName(PID) == null){
//inserts sheet at the end
var sheetIndex = database.getNumSheets()+1;
sheet = database.insertSheet(PID, sheetIndex).setName(PID);
splicedSheet = splicedDataset.insertSheet(PID, sheetIndex).setName(PID);
var complianceSheet = compliance.getSheets()[0];
var complianceNewRow = complianceSheet.getLastRow()+1;
var today = new Date();
var endYear = today.getFullYear(), endMonth = today.getMonth();
//the number of days you add is equal to the number of days you collect data. If you choose to use the compliance checker, add 2 to the number of
//data colsection days
var endDate = today.getDate() + 9;
var daysAfterDataCollection = new Date(endYear, endMonth, endDate);
//this code will delete the extra columns in each sheet so can write more data to each spreadsheet
//Each spreadsheet can only have a maximum of 2 million cells
sheet.deleteColumns(3, 24);
splicedSheet.deleteColumns(6, 21);
var newRow = sheet.getLastRow()+1;
for (var key = 0; key < Object.keys(e.parameters).length; key++){
sheet.getRange(newRow, 1).setValue(Object.keys(e.parameters)[key]);
splicedSheet.getRange(newRow, 1).setValue(Object.keys(e.parameters)[key]);
sheet.getRange(newRow, 2).setValue(e.parameters[Object.keys(e.parameters)[key]]);
splicedSheet.getRange(newRow, 4).setValue(e.parameters[Object.keys(e.parameters)[key]]);
newRow = newRow + 1;
//create a log of what is going on
//var doc = DocumentApp.openById("1bGnqx4Iw8yoXSn8z6hSfiuxFQE9Rq16rHYUfr1_5fpM");
//var body = doc.getBody();
//return HtmlService.createHtmlOutput(Logger.getLog());
return ContentService.createTextOutput(JSON.stringify({"result":"success", "row": nextRow})).setMimeType(ContentService.MimeType.JSON);
} catch(e) {
//if error return this
return ContentService.createTextOutput(JSON.stringify({"result":"error", "error": e})).setMimeType(ContentService.MimeType.JSON);
} finally {