Skip to content

Instantly share code, notes, and snippets.

@mhawksey
Created July 6, 2011 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhawksey/1067301 to your computer and use it in GitHub Desktop.
Save mhawksey/1067301 to your computer and use it in GitHub Desktop.
Automatically create new sheet for each user completing a form. Used in http://mashe.hawksey.info/2011/07/apps-script-intro-form-split/
/*
Copyright 2011 Martin Hawksey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var usernameColNumber = 1; // this is the column number of where usernames are stored from form submissions (A=1, B=2 etc)
function onFormSubmit() {
// onFormSubmit
// get submitted data
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form_Sheet");
var headings = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var lastRow = sheet.getRange(sheet.getLastRow(), 1, 1, sheet.getLastColumn()).getValues();
var studentUsername = lastRow[0][usernameColNumber-1];
// check if username has sheet
if(ss.getSheetByName(studentUsername)){
var userSheet = ss.getSheetByName(studentUsername);
// if not make
} else {
var userSheet = ss.insertSheet(studentUsername);
userSheet.getRange(1, 1 , 1, headings[0].length).setValues(headings);
}
// copy submitted data to user's sheet
userSheet.getRange(userSheet.getLastRow()+1, 1, 1, lastRow[0].length).setValues(lastRow);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment