/
format_multiple_sheets.gs
199 lines (166 loc) · 6.38 KB
/
format_multiple_sheets.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* @OnlyCurrentDoc
*
* The above comment directs Apps Script to limit the scope of file
* access for this add-on. It specifies that this add-on will only
* attempt to read or modify the files in which the add-on is used,
* and not all of the user's files. The authorization request message
* presented to users will reflect this limited scope.
*/
'use strict'
/**
* Creates a menu entry in the Google Docs UI when the document is opened.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @param {object} e The event parameter for a simple onOpen trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode.
*/
function onOpen (e) {
SpreadsheetApp.getUi().createAddonMenu()
.addItem('Start', 'showSidebar')
.addToUi()
}
/**
* @returns {array.<string>} list of names of the sheets in active Spreadsheet.
*/
function getSheetNames () {
var sheetNames = []
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets()
for (var i = 0; i < sheets.length; i++) {
sheetNames.push(sheets[i].getName())
}
return sheetNames;
}
/**
* propagateFormat takes a template sheet and copies its format
* to every other non-hidden sheet. Including col widths, row heights,
* frozen cols/rows and even tab color.
*
* TODO1: Copy also charts, protections and images, how?
* TODO2: Checkboxes to select whether to copy chart, protections, tab colors, etc.
* TODO3: Option to make the script non-destructive
* -> YAGNY if user duplicates spreadsheet before running script.
* Todo3B: Change "tip" to a "tips carrousel"?
*
* FIX: At input for contentOnly range, HTML5 RegExp for input validation FAILs (b/c no submit button?).
* @param {string} Name of the template sheet.
* @returns {string|Error} Message or error.
*/
function propagateFormat (template, options) {
var opts = options || {}
var ss = SpreadsheetApp.getActiveSpreadsheet()
var source = ss.getSheetByName(template)
if (source === null)
throw new ReferenceError('Sheet with name: "' + template + '" no longer exists!', 'format_multiple_sheets.gs')
var destinations = ss.getSheets()
var destination = destinations[0]
var data = prepareData(source, opts)
var tasks = [{ 'execute': unhideCells, 'skip': false },
{ 'execute': freezeCells, 'skip': false },
{ 'execute': copyTabColor, 'skip': false },
{ 'execute': cleanUp, 'skip': true },
{ 'execute': copyFormatsOnly, 'skip': false },
{ 'execute': copyHeights, 'skip': false },
{ 'execute': copyWidths, 'skip': false },
{ 'execute': copyContentsOnly, 'skip': false }]
for (var i = 0; i < destinations.length; i++) {
destination = destinations[i]
if (destination.isSheetHidden() || isSameSheet(destination, data.source)) continue
for (var j = 0; j < tasks.length; j++) {
if (!tasks[j].skip) tasks[j].execute(destination, data)
}
}
return 'Done!'
}
function prepareData (source, opts) {
var data = { 'range': source.getDataRange() }
if (opts.contentsOnlyRange && isValidA1Notation(opts.contentsOnlyRange))
data.contentsOnlyRange = source.getRange(opts.contentsOnlyRange)
data.values = data.range.getValues()
data.rangeInA1Notation = data.range.getA1Notation()
data.numCols = data.range.getNumColumns()
data.numRows = data.range.getNumRows()
data.firstCol = data.range.getColumn()
data.firstRow = data.range.getRow()
data.frozenCols = source.getFrozenColumns()
data.frozenRows = source.getFrozenRows()
data.tabColor = source.getTabColor()
data.source = source
return data
}
function freezeCells (target, data) {
target.setFrozenColumns(data.frozenCols)
target.setFrozenRows( data.frozenRows)
}
function unhideCells (target, data) {
target.showColumns(data.firstCol, data.numCols)
target.showRows( data.firstRow, data.numRows)
}
function copyTabColor (target, data) {
target.setTabColor(data.tabColor)
}
function cleanUp (target, _) {
target.clearFormats()
}
function copyFormatsOnly (target, data) {
// TODO: Refactor using range.copyTo(destRange, { formatOnly: true })
data.range.copyFormatToRange(target,
data.firstCol,
data.numCols,
data.firstRow,
data.numRows)
}
function copyWidths (target, data) {
var colPosition = 0
var desiredWidth = 0
var currentWidth = 0
for (var i = 0; i <= data.numCols; i++) {
colPosition = data.firstCol + i
desiredWidth = data.source.getColumnWidth(colPosition)
currentWidth = target.getColumnWidth(colPosition)
if (currentWidth !== desiredWidth) target.setColumnWidth(colPosition, desiredWidth)
}
}
function copyHeights (target, data) {
var rowPosition = 0
var desiredHeight = 0
var currentHeight = 0
for (var i = 0; i <= data.numRows; i++) {
rowPosition = data.firstRow + i
desiredHeight = data.source.getRowHeight(rowPosition)
currentHeight = target.getRowHeight(rowPosition)
if (currentHeight !== desiredHeight) target.setRowHeight(rowPosition, desiredHeight)
}
}
function copyContentsOnly (target, data) {
var destination
if (!data.contentsOnlyRange) return
destination = target.getRange(data.contentsOnlyRange.getA1Notation())
data.contentsOnlyRange.copyTo(destination, { contentsOnly: true })
}
/**
* Runs when the add-on is installed.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @param {object} e The event parameter for a simple onInstall trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode. (In practice, onInstall triggers always
* run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or
* AuthMode.NONE.)
*/
function onInstall (e) {
onOpen(e)
}
/**
* Opens a sidebar in the document containing the add-on's user interface.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*/
function showSidebar () {
var ui = HtmlService.createHtmlOutputFromFile('sidebar')
.setTitle('Format Multiple Sheets')
SpreadsheetApp.getUi().showSidebar(ui)
}