Created
July 9, 2018 12:16
-
-
Save withakay/be0c22e0e4ce7d8c24d9a1240caff285 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Usage | |
1. Run the setup function (you'll need to do this twice - 1st time to grant acces to Script Properties) | |
2. Share > Publish as service ... set security level and enable service | |
3. Copy the service URL and post this in your form/script action | |
4. Insert column names on the DATA sheet matching the parameter names of the data you are passing | |
call via https://your-script-url/exec?key=YOUR_KEY?headerX=123&headerB=xyz | |
*/ | |
// change to doPost(e) if you are recieving POST data | |
function doGet(e) { | |
var SHEET_NAME = "DOING"; // Needs to match a sheet name | |
var KEY = "doingit"; | |
var ss = SpreadsheetApp.openById(ScriptProperties.getProperty('active')); | |
var sheet = ss.getSheetByName(SHEET_NAME); | |
// read the first row of the sheet as column headers | |
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]; | |
var nextRow = sheet.getLastRow(); // get next row | |
var cell = sheet.getRange('a1'); | |
var col = 0; | |
// quick and dirty access restriction | |
if (e.parameter["key"] == KEY) { | |
// loop through the headers and if a parameter name matches the header name insert the value | |
for (i in headers){ | |
if (headers[i] == "Timestamp"){ | |
val = new Date(); | |
} else { | |
val = e.parameter[headers[i]]; | |
} | |
if(!!val) { | |
cell.offset(nextRow, col).setValue(val); | |
} | |
col++; | |
} | |
} | |
// this dumps the input back to the HTML, useful for debug | |
/* | |
var app = UiApp.createApplication(); | |
var panel = app.createVerticalPanel(); | |
for( p in e.parameters){ | |
panel.add(app.createLabel(p +" "+e.parameters[p])); | |
} | |
app.add(panel); | |
return app; | |
*/ | |
} | |
//http://www.google.sc/support/forum/p/apps-script/thread?tid=345591f349a25cb4&hl=en | |
function setUp() { | |
ScriptProperties.setProperty('active', SpreadsheetApp.getActiveSpreadsheet().getId()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment