Skip to content

Instantly share code, notes, and snippets.

@mhawksey mhawksey/gist:1067246
Last active Sep 26, 2015

Embed
What would you like to do?
IMPORTANT This is a better way of doing this --> https://gist.github.com/4641774 Automatically create new spreadsheet for each user completing a form. Used in http://mashe.hawksey.info/2011/07/apps-script-intro-form-split/
// This code has been developed for Google Apps Script
/*
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.
*/
// onFormSubmit
function onFormSubmit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form_Sheet");
if (ss.getSheetByName("Sheet_Index")){
var destSheet = ss.getSheetByName("Sheet_Index");
} else {
var destSheet = ss.insertSheet("Sheet_Index");
destSheet.getRange(1,1,1,3).setValues([["Student Username", "Spreadsheet ID", "Spreadsheet Url"]]);
}
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][1];
var destData = destSheet.getRange(1, 1, destSheet.getLastRow(), 1).getValues();
if(getUserRow(destData, studentUsername)){
var row = parseInt(getUserRow(destData, studentUsername))+1;
var userSS = SpreadsheetApp.openById(destSheet.getRange(row, 2, 1, 1).getValue());
var userSSSheet = userSS.getActiveSheet();;
//if not make
} else {
var userSS = SpreadsheetApp.create(ss.getName()+"-"+studentUsername);
userSS.addViewer(studentUsername);
// to add student's spreadsheet to a named folder create the folder, insert the name below and uncomment the next 3 lines
// var ssFile = DocsList.getFileById(userSS.getId());
// var folder = DocsList.getFolder("your folder name");
// ssFile.addToFolder(folder);
var userSSSheet = userSS.getActiveSheet();
userSSSheet.getRange(1, 1 , 1, headings[0].length).setValues(headings);
destSheet.getRange(destSheet.getLastRow()+1, 1, 1 , 3).setValues([[studentUsername, userSS.getId(), userSS.getUrl()]]);
}
// copy submitted data to user's sheet
userSSSheet.getRange(userSSSheet.getLastRow()+1, 1, 1, lastRow[0].length).setValues(lastRow);
}
function getUserRow(data, match){
for (var i in data){
if (data[i]==match){
return (i);
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.