-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_preso_adwordsapp.js
465 lines (438 loc) · 16 KB
/
update_preso_adwordsapp.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
* Creating dynamic visualisations using the Slides API within Apps Script.
*
* See: http://plemont.github.io/javascript/slides-api/2016/12/08/dynamic-dashboarding-with-slides-api.html
*/
var CLIENT_ID = 'INSERT_CLIENT_ID';
var CLIENT_SECRET = 'INSERT_CLIENT_SECRET';
var REFRESH_TOKEN = 'INSERT_REFRESH_TOKEN';
var SLIDES_ID = 'INSERT_SLIDES_ID';
var TOKEN_URL = 'https://accounts.google.com/o/oauth2/token';
var SCOPE = 'https://www.googleapis.com/auth/presentations ' +
'https://www.googleapis.com/auth/drive';
var oauthUrlFetchApp;
// Example use - consult this function for how to use the updatePresentation
// function in your own projects.
function main() {
if (typeof OAuth2 === 'undefined') {
var libUrl = 'https://developers.google.com/adwords/scripts/docs/examples/oauth20-library';
throw Error('OAuth2 library not found. Please take a copy of the OAuth2 ' +
'library from ' + libUrl + ' and append to the bottom of this script.');
}
oauthUrlFetchApp = OAuth2.withRefreshToken(TOKEN_URL, CLIENT_ID,
CLIENT_SECRET, REFRESH_TOKEN, SCOPE);
// Text mappings to change in the presentation
// For example, a text label created with "${heading1}" when creating the
// slide, will have text replaced with "My presentation".
var mappings = {
'heading1': 'My presentation ',
'heading2': 'Last updated: ' +
Utilities.formatDate(new Date(), AdWordsApp.currentAccount().getTimeZone(),
'yyyy-MM-dd hh:mm')
};
// Tables in the presentation to update from Sheets data.
// For example, if a 3x3 table is created in the document, and "${testtable}"
// placed in the top left cell, then with every subsequent execution, 3x3 of
// data will be taken from the Sheets document with id as per below, and
// Sheetname "TableData". Add further entries to the tables object for each
// table in the presentation
var tables = {
'testtable': {
id: '<...Sheets ID...>',
sheetName: 'TableData'
}
};
updatePresentation(SLIDES_ID, mappings, tables);
}
// Prefix used in Slides objectIds to indicate that this object has been renamed
// and is the target for content substitution.
var OBJ_PREFIX = '__plemont';
/**
* Updates a given presentation, performing:
* (0) Object renaming to facilitate repeated updating of objects from data.
* (1) Updates to any Sheets-linked charts.
* (2) Text updates for any text entities, using the mapping.
* (3) Table updates for any table entities, using Sheets as a source.
* @param {string} id The ID of the presentation.
* @param {!Object.<string>} mappings Dictionary of text entries to substitute.
* @param {!Object} tables A dictionary of Sheets to update tables from.
*/
function updatePresentation(id, mappings, tables) {
// DriveApp.createFile(blob);
var presentation = getPresentation(id);
// Create requests for one-time changes to object IDs
var renameObjectRequests = createTextAndTableRenameRequests(presentation);
Array.prototype.push.apply(renameObjectRequests,
createSlideRenameRequests(presentation));
if (renameObjectRequests.length) {
batchUpdate(presentation, renameObjectRequests);
presentation = getPresentation(id);
}
// Create requests for changing / refreshing the contents of elements
var requests = createTextReplacementRequests(presentation, mappings, tables);
Array.prototype.push.apply(requests,
createRefreshSheetsChartsRequests(presentation));
batchUpdate(presentation, requests);
}
/**
* Creates requests to rename any slides in the deck if they do not conform to
* the naming convention required for the Chrome extension autoplay hack.
* @param {!Object} presentation The Slides presentation object.
* @return {!Array.<Request>} rename requests.
*/
function createSlideRenameRequests(presentation) {
var newIdRequests = [];
var slides = presentation.slides;
var totalSlides = slides.length;
for (var i = 0; i < slides.length; i++) {
var slide = slides[i];
var newObjectId = [OBJ_PREFIX, i, totalSlides].join('_');
if (slide.objectId !== newObjectId) {
Array.prototype.push.apply(newIdRequests,
createRenameObjectRequests(slide, newObjectId));
}
}
return newIdRequests;
}
/**
* Retrieves a presentation.
* @param {string} presentationId The ID of the presentation to retrieve.
* @return {!Object} The object representing the presentation.
*/
function getPresentation(presentationId) {
var url = 'https://slides.googleapis.com/v1/presentations/' + presentationId;
var response = oauthUrlFetchApp.fetch(url);
return JSON.parse(response);
}
/**
* Replaces text in text components or tables, where the objectId indicates that
* a substitution should take place.
* @param {!Object} presentation The presentation object.
* @param {!Object.<string>} textMappings Dictionary of text mappings.
* @param {!Object} tableMappings Dictionary of table mappings.
* @return {!Array.<Request>}
*/
function createTextReplacementRequests(presentation, textMappings,
tableMappings) {
var requests = [];
var slides = presentation.slides;
for (var i = 0; i < slides.length; i++) {
var slide = slides[i];
var pageElements = slide.pageElements;
for (var j = 0; j < pageElements.length; j++) {
var pageElement = pageElements[j];
// Determine first whether the objectId indicates that this object
// requires text or tables to be updates
if (isObjectForTextSub(pageElement)) {
// The objectId contains the key for either the text or table mapping
var key = getKeyFromPageElement(pageElement);
// Determine whether the object is a text object
if (pageElement.shape && pageElement.shape.text) {
// Replacing text consists of deleting the old text object and
// inserting a new.
if (textMappings[key]) {
requests.push(createDeleteTextRequest(pageElement));
requests.push(createInsertTextRequest(pageElement,
textMappings[key]));
}
} else if (pageElement.table) {
// If instead the object is a table, then replace each cell of the
// table with data from a spreadsheet, if a mapping exists.
var spreadsheetInfo = tableMappings[key];
if (spreadsheetInfo) {
// It is necessary to obtain a 2d array, showing both the dimensions
// of the target table, to determine the dimensions to be requested
// from the Sheet, and also the array shows whether any cell is
// empty, as attempting to delete all existing text from an empty
// cell causes an error.
var tableDimensions = getTableDimensions(pageElement);
var newTable = loadTableFromSpreadsheet(spreadsheetInfo,
tableDimensions);
for (var m = 0; m < newTable.length; m++) {
var row = newTable[m];
for (var n = 0; n < row.length; n++) {
if (tableDimensions[m][n]) {
requests.push(
createDeleteTableTextRequests(pageElement, m, n));
}
requests.push(createInsertTableTextRequests(
pageElement, m, n, row[n]));
}
}
}
}
}
}
}
return requests;
}
/**
* Loads data from a specific Sheet in a spreadsheet, of the dimensions
* specified.
* @param {!Object} spreadsheetInfo An object containing the ID of the
* Spreadsheet and the name of the Sheet.
* @param {!Array.<!Array.<*>>} tableDimensions A 2D array, of the same
* dimensions as required for data to be retrieved.
* @return {!Array.<!Array.<!Object>>} 2d array of data from spreadsheet.
*/
function loadTableFromSpreadsheet(spreadsheetInfo, tableDimensions) {
return SpreadsheetApp
.openById(spreadsheetInfo.id)
.getSheetByName(spreadsheetInfo.sheetName)
.getRange(1, 1, tableDimensions.length, tableDimensions[0].length)
.getValues();
}
/**
* Builds a 2d array representing the dimensions of a table for a given element
* on the Slides page. Each array element contains true if the corresponding
* cell has content, and false if it is empty.
* @param {!Object} pageElement The table object from the page.
* @return {!Array.<!Array.<boolean>>}
*/
function getTableDimensions(pageElement) {
var data = [];
var rows = pageElement.table.tableRows;
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
var newRow = [];
for (var j = 0; j < row.tableCells.length; j++) {
var cell = row.tableCells[j];
newRow.push(cell.text ? true : false);
}
data.push(newRow);
}
return data;
}
/**
* Creates a request for use with batchUpdate to delete all the text in a given
* element on the page.
* @param {!Object} pageElement The element on the page to delete text from.
* @return {!Object} The request object.
*/
function createDeleteTextRequest(pageElement) {
return {
deleteText: {
objectId: pageElement.objectId,
textRange: {
type: 'ALL'
},
}
};
}
/**
* Creates a request for use with batchUpdate to insert text into a given
* element on the page.
* @param {!Object} pageElement The element on the page to delete text from.
* @param {string} The text to insert.
* @return {!Object} The request object.
*/
function createInsertTextRequest(pageElement, text) {
return {
insertText: {
objectId: pageElement.objectId,
text: text,
insertionIndex: 0
}
};
}
/**
* Creates a request for use with batchUpdate to delete text from a given cell
* in a table
* @param {!Object} pageElement The table on the page to delete text from.
* @param {number} rowIndex The row index.
* @param {number} colIndex The column index.
* @return {!Object} The request object.
*/
function createDeleteTableTextRequests(pageElement, rowIndex, colIndex) {
return {
deleteText: {
objectId: pageElement.objectId,
cellLocation: {
rowIndex: rowIndex,
columnIndex: colIndex
},
textRange: {
type: 'ALL',
}
}
};
}
/**
* Creates a request for use with batchUpdate to insert text into a given cell
* in a table
* @param {!Object} pageElement The table on the page to delete text from.
* @param {number} rowIndex The row index.
* @param {number} colIndex The column index.
* @return {!Object} The request object.
*/
function createInsertTableTextRequests(pageElement, rowIndex, colIndex, text) {
return {
insertText: {
objectId: pageElement.objectId,
cellLocation: {
rowIndex: rowIndex,
columnIndex: colIndex
},
text: text,
insertionIndex: 0
}
};
}
/**
* Extracts the key from a pageElement object ID. For example, object ID:
* __plemont_1_2_3_headingText returns a key of 'headingText'
* @param {!Object} pageElement The object from which to extract the key.
* @return {?string} The key, or null if none was found.
*/
function getKeyFromPageElement(pageElement) {
// var r = /\_([^_]+)$/;
var r = new RegExp(OBJ_PREFIX + '_\\d+_\\d+_\\d+_(.*)$');
var matches = r.exec(pageElement.objectId);
if (matches && matches.length) {
return matches[1];
}
}
/**
* Determines whether the object on the page expects text replacement.
* @param {!Object} pageElement The element to test.
* @return {boolean}
*/
function isObjectForTextSub(pageElement) {
return pageElement.objectId.substring(0, OBJ_PREFIX.length) === OBJ_PREFIX;
}
/**
* Retrieves an array of TextElements from the specified object, if it is a
* table.
* @param {!Object} pageElement The table element.
* @return {?Array.<!TextElement>}
*/
function extractTableTopLeftTextElements(pageElement) {
if (pageElement.table && pageElement.table.tableRows &&
pageElement.table.tableRows[0].tableCells &&
pageElement.table.tableRows[0].tableCells[0] &&
pageElement.table.tableRows[0].tableCells[0].text &&
pageElement.table.tableRows[0].tableCells[0].text.textElements) {
return pageElement.table.tableRows[0].tableCells[0].text.textElements;
}
}
/**
* Retrieves an array of TextElements from the specified object, if it is a
* shape containing text.
* @param {!Object} pageElement The shape element.
* @return {?Array.<!TextElement>}
*/
function extractLabelTextElements(pageElement) {
if (pageElement.shape && pageElement.shape.text &&
pageElement.shape.text.textElements) {
return pageElement.shape.text.textElements;
}
}
/**
* Creates the necessary requests for use with batchUpdate, to rename elements
* in the presentation, where text substitution markers e.g. ${name} are found.
* For example, a shape with text "${name}" will have its objectId changes to
* something like "<prefix>_name".
* @param {!Object} presentation The presentation object.
* @return {!Array.<Request>}
*/
function createTextAndTableRenameRequests(presentation) {
var regex = /^\$\{.*\}\n$/;
var slides = presentation.slides;
var requests = [];
for (var i = 0; i < slides.length; i++) {
var slide = slides[i];
var pageElements = slide.pageElements;
for (var j = 0; j < pageElements.length; j++) {
var pageElement = pageElements[j];
// Test to see whether the element is already named with the prefix.
if (!isObjectForTextSub(pageElement)) {
// Extract textElements from either table or text.
var textElements = extractTableTopLeftTextElements(pageElement) ||
extractLabelTextElements(pageElement);
if (textElements) {
for (var k = 0; k < textElements.length; k++) {
var textElement = textElements[k];
// Test to see whether the text is of the form ${...}
if (textElement.textRun && textElement.textRun.content &&
regex.test(textElement.textRun.content)) {
var content = textElement.textRun.content;
// Create the new object and remove the old
var newObjId = [OBJ_PREFIX, i, j, k,
content.substring(2, content.length - 2)].join('_');
var pair = createRenameObjectRequests(pageElement, newObjId);
Array.prototype.push.apply(requests, pair);
}
}
}
}
}
}
return requests;
}
/**
* Creates a pair of request objects needed to effectively rename an object.
* This is achieved by duplicating the required object with a new name, and
* deleting the original object
* @param {!Object} pageElement The object to be renamed.
* @param {string} newObjId The ID to rename to.
* @return {!Array.<Request>} A pair of requests.
*/
function createRenameObjectRequests(pageElement, newObjId) {
var duplicateRequest = {
duplicateObject: {
objectId: pageElement.objectId,
objectIds: {}
}
};
duplicateRequest.duplicateObject.objectIds[pageElement.objectId] = newObjId;
var deleteRequest = {
deleteObject: {
objectId: pageElement.objectId
}
};
return [duplicateRequest, deleteRequest];
}
/**
* Creates a list of request objects for refreshing any Sheets-linked charts
* in the presentation.
* @param {!Object} presentation The presentation.
* @return {!Array.<Request>}
*/
function createRefreshSheetsChartsRequests(presentation) {
var objectIds = [];
var slides = presentation.slides;
for (var i = 0; i < slides.length; i++) {
var slide = slides[i];
var pageElements = slide.pageElements;
for (var j = 0; j < pageElements.length; j++) {
var pageElement = pageElements[j];
if (pageElement.sheetsChart) {
objectIds.push(pageElement.objectId);
}
}
}
return objectIds.map(function(objectId) {
return {refreshSheetsChart: {objectId: objectId}};
});
}
/**
* Sends modification requests to Slides API
* @param {!Object} presentation The presentation to update.
* @param {!Array.<Request>} The requests to send.
*/
function batchUpdate(presentation, requests) {
if (!requests.length) {
return;
}
var options = {
method: 'POST',
payload: JSON.stringify({
requests: requests
}),
contentType: 'application/json'
};
var url = 'https://slides.googleapis.com/v1/presentations/' +
presentation.presentationId + ':batchUpdate';
oauthUrlFetchApp.fetch(url, options);
}
// Paste OAuth2 library