-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
135 lines (103 loc) · 3.75 KB
/
Code.gs
File metadata and controls
135 lines (103 loc) · 3.75 KB
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
// This file is part of FormatAsCode add-on
//
// FormatAsCode add-onis free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FormatAsCode add-on is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FormatAsCode add-on. If not, see <http://www.gnu.org/licenses/>.
function onOpen(){
DocumentApp.getUi().createAddonMenu()
.addItem('Format Selection', 'formatSelection')
.addSeparator()
.addItem('Settings', 'showSettings')
.addToUi();
loadSettings();
}
function onInstall(){
onOpen();
}
var allsettings = { fontSize:10 };
function loadSettings(){
Logger.log('Load settings was called!');
//var scriptProperties = PropertiesService.getScriptProperties();
allsettings.fontSize = getScriptSettingsProperty('fontSize', 10);
Logger.log('settings read');
Logger.log(allsettings);
return allsettings;
}
function getScriptSettingsProperty(keyName, defaultValue){
var scriptProperties = PropertiesService.getScriptProperties();
if (scriptProperties.getKeys().indexOf(keyName) != -1){
var propertyValue = scriptProperties.getProperty(keyName);
if (typeof(propertyValue) !== 'undefined'){
return propertyValue;
} else {
return defaultValue;
}
} else {
return defaultValue;
}
}
function saveSettings(settings){
Logger.log('saving settings');
Logger.log(settings);
var allowedKeys = ['fontSize'];
if (typeof(settings) !== 'object' || settings.length == 0){
return false;
}
var scriptProperties = PropertiesService.getScriptProperties();
for(var keyName in settings){
if (allowedKeys.indexOf(keyName) != -1) {
scriptProperties.setProperty(keyName, settings[keyName]);
allsettings[keyName] = settings[keyName];
}
}
Logger.log('Settings saved');
Logger.log(allsettings);
return allsettings[keyName];
}
function formatSelection() {
loadSettings();
var selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
var elements = selection.getRangeElements();
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
// Only modify elements that can be edited as text; skip images and other non-text elements.
if (element.getElement().editAsText) {
var text = element.getElement().editAsText();
// Bold the selected part of the element, or the full element if it's completely selected.
if (element.isPartial()) {
//text.setBold(element.getStartOffset(), element.getEndOffsetInclusive(), true);
text.setFontFamily(element.getStartOffset(), element.getEndOffsetInclusive(),'Courier New');
text.setFontSize(element.getStartOffset(), element.getEndOffsetInclusive(), parseInt(allsettings.fontSize));
} else {
text.setFontFamily('Courier New');
text.setFontSize(parseInt(allsettings.fontSize));
}
}
}
} else {
// Do nothing
}
}
function showSettings(){
var ui = DocumentApp.getUi();
var html = HtmlService.createTemplateFromFile('settings.html')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(200)
.setHeight(100);
ui.showModalDialog(html, 'Settings');
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}