-
Notifications
You must be signed in to change notification settings - Fork 0
/
copydocs.js
160 lines (141 loc) · 6.7 KB
/
copydocs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
function runMe() {
setVars();
}
function setVars() {
var sheetConfig = {'sheetName': '',
'rowStart': '',
'firstNameColumn': '',
'lastNameColumn': '',
'emailColumn': ''};
var scriptConfig = {'srcSheetName': '',
'srcDocName': '',
'dstFolder': '',
'sheetConfig': sheetConfig,
'isTest': '0',
'dryRun': '0',
'share': '0'};
Logger.log('Source Sheet Name: ' + scriptConfig.srcSheetName);
Logger.log('Source Document Name: ' + scriptConfig.srcDocName);
Logger.log('Destination Folder: ' + scriptConfig.dstFolder);
Logger.log('Test Run: ' + scriptConfig.isTest);
Logger.log('Dry Run: ' + scriptConfig.dryRun);
Logger.log('Sheet Configs: ');
Logger.log(' Sheet Name: ' + scriptConfig.sheetConfig.sheetName);
Logger.log(' Row Start: ' + scriptConfig.sheetConfig.rowStart);
Logger.log(' First Name: ' + scriptConfig.sheetConfig.firstNameColumn);
Logger.log(' Last Name: ' + scriptConfig.sheetConfig.lastNameColumn);
Logger.log(' Email: ' + scriptConfig.sheetConfig.emailColumn);
copyDoc(scriptConfig);
}
/**
* Function: copyDoc
* Purpose: Function for creating a new file from the master file.
*
* @param scriptConfig {dict} Master config variable with all pre-set options.
*
*/
function copyDoc(scriptConfig) {
// Creating list of students from createStudentList function
var studentInfo = createStudentList(scriptConfig);
var listLength = studentInfo.length;
// Iterate over student information array to create a new file with format of "firstName lastName srcDocName"
for (var i = 0; i < listLength; i++) {
var dstFileName = studentInfo[i].firstName + ' ' + studentInfo[i].lastName + ' ' + scriptConfig.srcDocName;
var docID = docsIterator(scriptConfig.srcDocName);
if (scriptConfig.dstFolder == '') {
DriveApp.getFileById(docID).makeCopy(dstFileName);
} else {
DriveApp.getFileById(docID).makeCopy(dstFileName, scriptConfig.dstFolder);
}
shareDoc(dstFileName, studentInfo[i].email);
}
}
/**
* Function: shareDoc
* Purpose: Share the newly created file with the student by email address.
*
* @param dstFileName {string} Newly created file name to add the student as an editor.
* @param email {string} Email address of the student with whom the file is being shared.
*/
function shareDoc(dstFileName, email) {
var docID = docsIterator(dstFileName);
DocumentApp.openById(docID).addEditor(email);
}
/**
* createStudentList creates an array of dictionaries containing the student
* information pulleed from student info Google spreadsheet.
*
* @param scriptConfig.isTest {boolean} Flags whether the user wants to use test data provided in-script for testing purposes or bring their own Google sheet.
* @param scriptConfig.srcSheetName {string} The name of the Google sheet containing the student information.
* @param scriptConfig.srcDocName {string} The name of the file that needs to be copied.
* @param scriptCOnfig.sheetCOnfig.sheetName {string} The name of the individual sheet in the Google spreadsheet with the student information required.
* @param student {dict} Dictionary of student information containing firstName, lastName, and email.
* @return studentInfo {array} The compiled list of student information
*
*/
function createStudentList(scriptConfig) {
var studentInfo = [];
// Check if isTest is set properly. If not, bail.
if (scriptConfig.isTest != '0' && scriptConfig.isTest != '1') {
throw ("error: isTest must be 0 or 1 where 0 = False and 1 = True.");
}
// Check to see if the source Google sheet is set. If not, bail.
if (scriptConfig.srcSheetName == '') {
throw ("error: scriptConfig.srcSheetName must not be empty.");
}
// Check to see if the source Google file is set. If not, bail.
if (scriptConfig.srcDocName == '') {
throw ("error: scriptConfig.srcDocName must not be empty.");
}
// Check to see if the Google spreadsheet sheet name is set. If not, bail.
// We can not assume what the sheet name will be, even though the default is "Sheet1".
if (scriptConfig.sheetConfig.sheetName == '') {
throw ("error: scriptConfig.sheetConfig.sheetName must not be empty.");
}
// Returning studentInfo list object with test data if scriptConfig.isTest is True.
if (scriptConfig.isTest == '1') {
studentInfo.push({'firstName': "Student", 'lastName': "One", 'email': "[email protected]"});
studentInfo.push({'firstName': "Student", 'lastName': "Two", 'email': "[email protected]"});
studentInfo.push({'firstName': "Student", 'lastName': "Three", 'email': "[email protected]"});
return studentInfo;
}
// Creating studentInfo from spreadsheet and returning studentInfo list object.
if (scriptConfig.isTest == '0') {
var docID = docsIterator(scriptConfig.srcSheetName);
var ss = SpreadsheetApp.openById(docID);
// Activating the appropriate spreadsheet that houses the student information.
var sheet = ss.getSheetByName(scriptConfig.sheetConfig.sheetName).activate();
// Determining the last row number with data in it.
var lastRow = ss.getLastRow();
var i = scriptConfig.sheetConfig.rowStart;
while (i <= lastRow) {
var firstName = sheet.getRange(scriptConfig.sheetConfig.firstNameColumn + i).getValue();
var lastName = sheet.getRange(scriptConfig.sheetConfig.lastNameColumn + i).getValue();
var email = sheet.getRange(scriptConfig.sheetConfig.emailColumn + i).getValue();
var student = {'firstName': firstName, 'lastName': lastName, 'email': email};
studentInfo.push(student);
i++;
}
return studentInfo;
}
}
/**
* Function: docsIterator
* Purpose: Code re-use to derive ID of the file to be copied and the new copy of the file for processing.
*
* @param docName {string} Name of the document that is being worked on, either the spreadsheet, file to be copied, or the new file.
* @return docID {string} Unique identifier for the file that's being operated on.
*
*/
function docsIterator(docName) {
var files = DriveApp.getFilesByName(docName);
while (files.hasNext()) {
var srcDoc = files.next();
if (docName == srcDoc.getName()) {
var docID = srcDoc.getId();
return docID;
} else {
throw ("error: Did not find document matching name " + scriptConfig.srcDocName);
}
}
}