-
Notifications
You must be signed in to change notification settings - Fork 22
/
preconfig extractor.gs
64 lines (64 loc) · 3.99 KB
/
preconfig extractor.gs
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
function autoCrat_extractorWindow () {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setHeight(500).setWidth(600).setTitle("Export preconfig() settings");
var panel = app.createVerticalPanel().setWidth("100%").setHeight("100%");
var labelText = "Copying a Google Spreadsheet copies scripts along with it, but without any of the script settings saved. This normally makes it hard to share full, script-enabled Spreadsheet systems. ";
labelText += " You can solve this problem by pasting the code below into a script file called \"paste preconfig here\" (go to Script Editor and look in left sidebar of the autoCrat script) prior to publishing your Spreadsheet for others to copy. \n";
labelText += " After a user copies your spreadsheet, they will select \"Run initial installation.\" This will preconfigure all needed script settings. If you copied this system from someone as a spreadsheet, this has probably already been done for you.";
var label = app.createLabel(labelText);
var window = app.createTextArea();
var codeString = "//This section sets all script properties associated with this autoCrat profile \n";
codeString += "var preconfigStatus = ScriptProperties.getProperty('preconfigStatus');\n";
codeString += "if (preconfigStatus!='true') {\n";
var properties = ScriptProperties.getProperties();
var excludeProperties = ['autocrat_sid', 'preconfigStatus', 'ssId', 'ssKey', 'destinationFolderId', 'fileId', 'formulaTriggerSet'];
var propertyString = '';
for (var key in properties) {
if (excludeProperties.indexOf(key)==-1) {
var keyProperty = properties[key]; //.replace(/[/\\*]/g, "\\\\");
propertyString += " ScriptProperties.setProperty('" + key + "','" + keyProperty + "');\n";
}
}
//generate msgbox warning code if automated email or calendar is enabled in template
codeString += "\n \n";
codeString += " //Custom code to copy document template and create destination folder \n";
codeString += " //Note that your template must be set as visible to the user that will be copying this system \n";
codeString += " var ss = SpreadsheetApp.getActiveSpreadsheet();\n";
codeString += " var ssId = SpreadsheetApp.getActiveSpreadsheet().getId();\n";
codeString += " var parent = DocsList.getFileById(ssId).getParents()[0];\n";
codeString += " var files = parent.getFiles();\n";
codeString += " for (var i=0; i<files.length; i++) {\n";
codeString += " if (files[i].getName()=='"+properties.fileName+"') {\n";
codeString += " var fileId = files[i].getId();\n";
codeString += " ScriptProperties.setProperty('fileId',fileId);\n";
codeString += " break;\n";
codeString += " }\n";
codeString += " }\n";
codeString += " var newFolder = parent.createFolder('"+ properties.destinationFolderName+"');\n";
codeString += " var newFolderId = newFolder.getId();\n";
codeString += " ScriptProperties.setProperty('destinationFolderId',newFolderId);\n";
codeString += " if (!(fileId)) {\n";
codeString += " var originalTemplate = DocsList.getFileById('"+properties.fileId+"');\n";
codeString += " var copy = originalTemplate.makeCopy('"+properties.fileName+"')\n";
codeString += " var root = copy.getParents()[0];\n";
codeString += " copy.removeFromFolder(root);\n";
codeString += " copy.addToFolder(parent);\n";
codeString += " var copyId = copy.getId();\n";
codeString += " ScriptProperties.setProperty('fileId',copyId);\n";
codeString += " }\n";
if (properties.formulaTriggerSet == "true") {
codeString += " setCopyDownTrigger(); \n";
}
if (properties.formTrigger == "true") {
codeString += " autoCrat_setFormTrigger(); \n";
}
codeString += propertyString;
codeString += " ss.toast('Custom autoCrat preconfiguration ran successfully.');\n";
codeString += "}\n";
window.setText(codeString).setWidth("100%").setHeight("400px");
app.add(label);
panel.add(window);
app.add(panel);
ss.show(app);
return app;
}