Skip to content

Instantly share code, notes, and snippets.

Last active April 4, 2019 05:01
Show Gist options
  • Save noahbass/12373d4b9b28f494f789 to your computer and use it in GitHub Desktop.
Save noahbass/12373d4b9b28f494f789 to your computer and use it in GitHub Desktop.
google docs - insert data from an external form to a spreadsheet
// forked from
// function to recieve the data from the form
function doPost(e) {
// get the correct spreadsheet
var ss = SpreadsheetApp.openById(ScriptProperties.getProperty('active'));
// get the sheet with a name of "DATA"
var sheet = ss.getSheetByName("DATA");
// find the headers of the sheet and their values
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
// get the row below the headers
var nextRow = sheet.getLastRow();
// get the first cell
var cell = sheet.getRange('a1');
// the first column in the sheet
var col = 0;
// insert the data into the spreadsheet in a loop only if the header is the same as the html name=""
for (i in headers){
if (headers[i] == "Timestamp"){
val = new Date();
} else {
val = e.parameter[headers[i]];
cell.offset(nextRow, col).setValue(val);
// debugging
var app = UiApp.createApplication();
var panel = app.createVerticalPanel();
for(p in e.parameters){
panel.add(app.createLabel(p +" "+e.parameters[p]));
return app;
// function to init the spreadsheet for the first time
function setUp() {
ScriptProperties.setProperty('active', SpreadsheetApp.getActiveSpreadsheet().getId());
<input type="text" placeholder="name" name="Name">
<input type="text" placeholder="comment" name="Comment">
<input type="submit" value="submit">
$('form').submit(function() {
// post the form data to the spreadsheet
$.post('', $('form').serialize());
// reset the value on the input
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment