-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
UI.js
233 lines (200 loc) · 8.2 KB
/
UI.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* 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.
*/
// Change application logo here (and in manifest) as desired.
const ADDON_LOGO = 'https://www.gstatic.com/images/branding/product/2x/apps_script_48dp.png';
/**
* Callback function for rendering the main card.
* @return {CardService.Card} The card to show the user.
*/
function onHomepage(e) {
return createSelectionCard(e);
}
/**
* Builds the primary card interface used to collect user inputs.
*
* @param {Object} e - Add-on event object.
* @param {string} sourceScriptId - Script ID of the source project.
* @param {string} targetSpreadsheetUrl - URL of the target spreadsheet.
* @param {string[]} errors - Array of error messages.
*
* @return {CardService.Card} The card to show to the user for inputs.
*/
function createSelectionCard(e, sourceScriptId, targetSpreadsheetUrl, errors) {
// Configures card header.
let cardHeader = CardService.newCardHeader()
.setTitle('Share macros with other spreadheets!')
.setImageUrl(ADDON_LOGO)
.setImageStyle(CardService.ImageStyle.SQUARE);
// If form errors exist, configures section with error messages.
let showErrors = false;
if (errors && errors.length) {
showErrors = true;
let msg = errors.reduce((str, err) => `${str}• ${err}<br>`, '');
msg = `<b>Form submission errors:</b><br><font color="#ba0000">${msg}</font>`;
// Builds error message section.
sectionErrors = CardService.newCardSection()
.addWidget(CardService.newDecoratedText()
.setText(msg)
.setWrapText(true));
}
// Configures source project section.
let sectionSource = CardService.newCardSection()
.addWidget(CardService.newDecoratedText()
.setText('<b>Source macro</b><br>The Apps Script project to copy'))
.addWidget(CardService.newTextInput()
.setFieldName('sourceScriptId')
.setValue(sourceScriptId || '')
.setTitle('Script ID of the source macro')
.setHint('You must have at least edit permission for the source spreadsheet to access its script project'))
.addWidget(CardService.newTextButton()
.setText('Find the script ID')
.setOpenLink(CardService.newOpenLink()
.setUrl('https://developers.google.com/apps-script/api/samples/execute')
.setOpenAs(CardService.OpenAs.FULL_SIZE)
.setOnClose(CardService.OnClose.NOTHING)));
// Configures target spreadsheet section.
let sectionTarget = CardService.newCardSection()
.addWidget(CardService.newDecoratedText()
.setText('<b>Target spreadsheet</b>'))
.addWidget(CardService.newTextInput()
.setFieldName('targetSpreadsheetUrl')
.setValue(targetSpreadsheetUrl || '')
.setHint('You must have at least edit permission for the target spreadsheet')
.setTitle('Target spreadsheet URL'));
// Configures help section.
let sectionHelp = CardService.newCardSection()
.addWidget(CardService.newDecoratedText()
.setText('<b><font color=#c80000>NOTE: </font></b>' +
'The Apps Script API must be turned on.')
.setWrapText(true))
.addWidget(CardService.newTextButton()
.setText('Turn on Apps Script API')
.setOpenLink(CardService.newOpenLink()
.setUrl('https://script.google.com/home/usersettings')
.setOpenAs(CardService.OpenAs.FULL_SIZE)
.setOnClose(CardService.OnClose.NOTHING)));
// Configures card footer with action to copy the macro.
var cardFooter = CardService.newFixedFooter()
.setPrimaryButton(CardService.newTextButton()
.setText('Share macro')
.setOnClickAction(CardService.newAction()
.setFunctionName('onClickFunction_')));
// Begins building the card.
let builder = CardService.newCardBuilder()
.setHeader(cardHeader);
// Adds error section if applicable.
if (showErrors) {
builder.addSection(sectionErrors)
}
// Adds final sections & footer.
builder
.addSection(sectionSource)
.addSection(sectionTarget)
.addSection(sectionHelp)
.setFixedFooter(cardFooter);
return builder.build();
}
/**
* Action handler that validates user inputs and calls shareMacro_
* function to copy Apps Script project to target spreadsheet.
*
* @param {Object} e - Add-on event object.
*
* @return {CardService.Card} Responds with either a success or error card.
*/
function onClickFunction_(e) {
const sourceScriptId = e.formInput.sourceScriptId;
const targetSpreadsheetUrl = e.formInput.targetSpreadsheetUrl;
// Validates inputs for errors.
const errors = [];
// Pushes an error message if the Script ID parameter is missing.
if (!sourceScriptId) {
errors.push('Missing script ID');
} else {
// Gets the Apps Script project if the Script ID parameter is valid.
const sourceProject = APPS_SCRIPT_API.get(sourceScriptId);
if (!sourceProject) {
// Pushes an error message if the Script ID parameter isn't valid.
errors.push('Invalid script ID');
}
}
// Pushes an error message if the spreadsheet URL is missing.
if (!targetSpreadsheetUrl) {
errors.push('Missing Spreadsheet URL');
} else
try {
// Tests for valid spreadsheet URL to get the spreadsheet ID.
const ssId = SpreadsheetApp.openByUrl(targetSpreadsheetUrl).getId();
} catch (err) {
// Pushes an error message if the spreadsheet URL parameter isn't valid.
errors.push('Invalid spreadsheet URL');
}
if (errors && errors.length) {
// Redisplays form if inputs are missing or invalid.
return createSelectionCard(e, sourceScriptId, targetSpreadsheetUrl, errors);
} else {
// Calls shareMacro function to copy the project.
shareMacro_(sourceScriptId, targetSpreadsheetUrl);
// Creates a success card to display to users.
return buildSuccessCard(e, targetSpreadsheetUrl);
}
}
/**
* Builds success card to inform user & let them open the spreadsheet.
*
* @param {Object} e - Add-on event object.
* @param {string} targetSpreadsheetUrl - URL of the target spreadsheet.
*
* @return {CardService.Card} Returns success card.
*/function buildSuccessCard(e, targetSpreadsheetUrl) {
// Configures card header.
let cardHeader = CardService.newCardHeader()
.setTitle('Share macros with other spreadsheets!')
.setImageUrl(ADDON_LOGO)
.setImageStyle(CardService.ImageStyle.SQUARE);
// Configures card body section with success message and open button.
let sectionBody1 = CardService.newCardSection()
.addWidget(CardService.newTextParagraph()
.setText('Sharing process is complete!'))
.addWidget(CardService.newTextButton()
.setText('Open spreadsheet')
.setOpenLink(CardService.newOpenLink()
.setUrl(targetSpreadsheetUrl)
.setOpenAs(CardService.OpenAs.FULL_SIZE)
.setOnClose(CardService.OnClose.RELOAD_ADD_ON)));
let sectionBody2 = CardService.newCardSection()
.addWidget(CardService.newTextParagraph()
.setText('If you don\'t see the copied project in your target spreadsheet,' +
' make sure you turned on the Apps Script API in the Apps Script dashboard.'))
.addWidget(CardService.newTextButton()
.setText("Check API")
.setOpenLink(CardService.newOpenLink()
.setUrl('https://script.google.com/home/usersettings')
.setOpenAs(CardService.OpenAs.FULL_SIZE)
.setOnClose(CardService.OnClose.RELOAD_ADD_ON)));
// Configures the card footer with action to start new process.
let cardFooter = CardService.newFixedFooter()
.setPrimaryButton(CardService.newTextButton()
.setText('Share another')
.setOnClickAction(CardService.newAction()
.setFunctionName('onHomepage')));
return builder = CardService.newCardBuilder()
.setHeader(cardHeader)
.addSection(sectionBody1)
.addSection(sectionBody2)
.setFixedFooter(cardFooter)
.build();
}