forked from googleworkspace/apps-script-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetup.js
119 lines (109 loc) · 3.97 KB
/
Setup.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
/**
* Copyright 2022 Google LLC
*
* 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.
*/
// [START apps_script_upload_files_setup]
// TODO You must run the setUp() function before you start using this sample.
/**
* The setUp() function performs the following:
* - Creates a Google Drive folder named by the APP_FOLDER_NAME
* variable in the Code.gs file.
* - Creates a trigger to handle onFormSubmit events.
*/
function setUp() {
// Ensures the root destination folder exists.
const appFolder = getFolder_(APP_FOLDER_NAME);
if (appFolder !== null) {
console.log(`Application folder setup.
Name: ${appFolder.getName()}
ID: ${appFolder.getId()}
URL: ${appFolder.getUrl()}`)
}
else {
console.log(`Could not setup application folder.`)
}
// Calls the function that creates the Forms onSubmit trigger.
installTrigger_();
}
/**
* Returns a folder to store uploaded files in the same location
* in Drive where the form is located. First, it checks if the folder
* already exists, and creates it if it doesn't.
*
* @param {string} folderName - Name of the Drive folder.
* @return {object} Google Drive Folder
*/
function getFolder_(folderName) {
// Gets the Drive folder where the form is located.
const ssId = FormApp.getActiveForm().getId();
const parentFolder = DriveApp.getFileById(ssId).getParents().next();
// Iterates through the subfolders to check if folder already exists.
// The script checks for the folder name specified in the APP_FOLDER_NAME variable.
const subFolders = parentFolder.getFolders();
while (subFolders.hasNext()) {
let folder = subFolders.next();
// Returns the existing folder if found.
if (folder.getName() === folderName) {
return folder;
}
}
// Creates a new folder if one doesn't already exist.
return parentFolder.createFolder(folderName)
.setDescription(`Created by ${APP_TITLE} application to store uploaded files.`);
}
/**
* Installs trigger to capture onFormSubmit event when a form is submitted.
* Ensures that the trigger is only installed once.
* Called by setup().
*/
function installTrigger_() {
// Ensures existing trigger doesn't already exist.
let propTriggerId = PropertiesService.getScriptProperties().getProperty('triggerUniqueId')
if (propTriggerId !== null) {
const triggers = ScriptApp.getProjectTriggers();
for (let t in triggers) {
if (triggers[t].getUniqueId() === propTriggerId) {
console.log(`Trigger with the following unique ID already exists: ${propTriggerId}`);
return;
}
}
}
// Creates the trigger if one doesn't exist.
let triggerUniqueId = ScriptApp.newTrigger('onFormSubmit')
.forForm(FormApp.getActiveForm())
.onFormSubmit()
.create()
.getUniqueId();
PropertiesService.getScriptProperties().setProperty('triggerUniqueId', triggerUniqueId);
console.log(`Trigger with the following unique ID was created: ${triggerUniqueId}`);
}
/**
* Removes all script properties and triggers for the project.
* Use primarily to test setup routines.
*/
function removeTriggersAndScriptProperties() {
PropertiesService.getScriptProperties().deleteAllProperties();
// Removes all triggers associated with project.
const triggers = ScriptApp.getProjectTriggers();
for (let t in triggers) {
ScriptApp.deleteTrigger(triggers[t]);
}
}
/**
* Removes all form responses to reset the form.
*/
function deleteAllResponses() {
FormApp.getActiveForm().deleteAllResponses();
}
// [END apps_script_upload_files_setup]