// Format a string into text for the HTML response

function out( s ) {
  return ContentService.createTextOutput(s).setMimeType(ContentService.MimeType.TEXT);
}

function doPost(e) { 
//  Logger.clear();
      
  var ssID = ScriptProperties.getProperty('targetSpreadsheetID');
  if (ssID == null ) {
    return( out( "Property targetSpreadsheetID not found. Be sure to run Setup script."));
  }
  
//  Logger.log("Spreadsheet ID=%s", ssID );  
  
  var ss = SpreadsheetApp.openById( ssID );
  
  if (ss == null ) { 
    return( out( "Could not find spreadsheet ID ["+ssID+"]. Aborting."));
  }

  var sheetName = ScriptProperties.getProperty('targetSheetName');
  if (sheetName == null ) {
    return( out( "Property targetSheetName not found. Be sure to run Setup script."));
  }

//  Logger.log( "Target Sheet Name=%s" , sheetName );  
  
  var sheet = ss.getSheetByName(sheetName);
  
  // No such thing as Spreadsheet.getSheetById()? Really?
  
  if (sheet == null ) {
    return( out( "Could not find sheet named  ["+sheetName+"]. Aborting."));
  }

    
  var parameters = e.parameter;   // Grab the  parameters from the request
  
  var headers;
    
  if ( sheet.getLastColumn() == 0 ) {   // Special case of empty sheet...
    headers = [ ];
  } else {
    headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];  //read headers from top row of spreadsheet    
  }

  var newHeadersFlag = false; 
      
  var newRow = [];     // Hold new row to be Added to bottom
  
  // loop through the request parameter names (keys) and 
  // put them in the right column in spreadsheet
  for (var p in parameters) { 
//    Logger.log( " parameter key=%s"  ,  p );
                 
    var col = headers.indexOf( p );    // Find column for the param name 
    if ( col <0 ) {  // if matching col header not found
      
      // add new column at end           
      headers.push(p);
      col = headers.indexOf( p );    // Find column for the param name       
      newHeadersFlag = true;
                  
//      Logger.log( "New col=%s" ,  col );
      
      // Note that it appears if you send multipule params with 
      // the same name that only the first one shows up
      
    }
    
    // Lookup value of the passed param and put it into the new row we are building
    newRow[col]  = parameters[ p ];    
//    Logger.log( " col=%s value=%s", col  , newRow[col] );
    
  }
  
  // Necessary because appending a blank row causes a 
  // Sheet Serivce Error after the script completes
  if (newRow.length==0) {
     return( out( "No parameters found, no data appended."));      
  }   
  
  if (newHeadersFlag) {   // We updated some headers, so reflect in the sheet
    var headersRange = [ headers ]; // Must be 2 dimensional array to set a range
    sheet.getRange( 1, 1, 1 , headers.length ).setValues( headersRange );
  }
    
  sheet.appendRow(newRow);   // Append new row to end of spreadsheet  
  return( out( "Data appended successfully.") );
  
}

function doGet(e) {
  return(doPost(e));
}

function setupLoggingToCurrentSheet() {
  ScriptProperties.setProperty('targetSpreadsheetID', SpreadsheetApp.getActiveSpreadsheet().getId());
  ScriptProperties.setProperty('targetSheetName', SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetName());      
}

function clearLog() {
  var spreadsheet = SpreadsheetApp.getActive();
  var last = SpreadsheetApp.getActiveSheet().getLastRow();
  spreadsheet.deleteRows(2, last - 1);
}

function onOpen() {
  SpreadsheetApp.getActive()
  .addMenu("V9 Script",
           [{name: "Active log", functionName: "setupLoggingToCurrentSheet"},
            {name: "Clear log", functionName: "clearLog"}]);
}