forked from googleworkspace/apps-script-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slides.gs
218 lines (210 loc) · 6.76 KB
/
slides.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Copyright 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
*
* https://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.
*/
// [START apps_script_slides_create_presentation]
/**
* Create a new presentation.
* @return {string} presentation Id.
* @see https://developers.google.com/slides/api/reference/rest/v1/presentations/create
*/
function createPresentation() {
try {
const presentation =
Slides.Presentations.create({'title': 'MyNewPresentation'});
console.log('Created presentation with ID: ' + presentation.presentationId);
return presentation.presentationId;
} catch (e) {
// TODO (developer) - Handle exception
console.log('Failed with error %s', e.message);
}
}
// [END apps_script_slides_create_presentation]
// [START apps_script_slides_create_slide]
/**
* Create a new slide.
* @param {string} presentationId The presentation to add the slide to.
* @return {Object} slide
* @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate
*/
function createSlide(presentationId) {
// You can specify the ID to use for the slide, as long as it's unique.
const pageId = Utilities.getUuid();
const requests = [{
'createSlide': {
'objectId': pageId,
'insertionIndex': 1,
'slideLayoutReference': {
'predefinedLayout': 'TITLE_AND_TWO_COLUMNS'
}
}
}];
try {
const slide =
Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
console.log('Created Slide with ID: ' + slide.replies[0].createSlide.objectId);
return slide;
} catch (e) {
// TODO (developer) - Handle Exception
console.log('Failed with error %s', e.message);
}
}
// [END apps_script_slides_create_slide]
// [START apps_script_slides_read_page]
/**
* Read page element IDs.
* @param {string} presentationId The presentation to read from.
* @param {string} pageId The page to read from.
* @return {Object} response
* @see https://developers.google.com/slides/api/reference/rest/v1/presentations.pages/get
*/
function readPageElementIds(presentationId, pageId) {
// You can use a field mask to limit the data the API retrieves
// in a get request, or what fields are updated in an batchUpdate.
try {
const response = Slides.Presentations.Pages.get(
presentationId, pageId, {'fields': 'pageElements.objectId'});
console.log(response);
return response;
} catch (e) {
// TODO (developer) - Handle Exception
console.log('Failed with error %s', e.message);
}
}
// [END apps_script_slides_read_page]
// [START apps_script_slides_add_text_box]
/**
* Add a new text box with text to a page.
* @param {string} presentationId The presentation ID.
* @param {string} pageId The page ID.
* @return {Object} response
* @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate
*/
function addTextBox(presentationId, pageId) {
// You can specify the ID to use for elements you create,
// as long as the ID is unique.
const pageElementId = Utilities.getUuid();
const requests = [{
'createShape': {
'objectId': pageElementId,
'shapeType': 'TEXT_BOX',
'elementProperties': {
'pageObjectId': pageId,
'size': {
'width': {
'magnitude': 150,
'unit': 'PT'
},
'height': {
'magnitude': 50,
'unit': 'PT'
}
},
'transform': {
'scaleX': 1,
'scaleY': 1,
'translateX': 200,
'translateY': 100,
'unit': 'PT'
}
}
}
}, {
'insertText': {
'objectId': pageElementId,
'text': 'My Added Text Box',
'insertionIndex': 0
}
}];
try {
const response =
Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
console.log('Created Textbox with ID: ' +
response.replies[0].createShape.objectId);
return response;
} catch (e) {
// TODO (developer) - Handle Exception
console.log('Failed with error %s', e.message);
}
}
// [END apps_script_slides_add_text_box]
// [START apps_script_slides_format_shape_text]
/**
* Format the text in a shape.
* @param {string} presentationId The presentation ID.
* @param {string} shapeId The shape ID.
* @return {Object} replies
* @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate
*/
function formatShapeText(presentationId, shapeId) {
const requests = [{
'updateTextStyle': {
'objectId': shapeId,
'fields': 'foregroundColor,bold,italic,fontFamily,fontSize,underline',
'style': {
'foregroundColor': {
'opaqueColor': {
'themeColor': 'ACCENT5'
}
},
'bold': true,
'italic': true,
'underline': true,
'fontFamily': 'Corsiva',
'fontSize': {
'magnitude': 18,
'unit': 'PT'
}
},
'textRange': {
'type': 'ALL'
}
}
}];
try {
const response =
Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
return response.replies;
} catch (e) {
// TODO (developer) - Handle Exception
console.log('Failed with error %s', e.message);
}
}
// [END apps_script_slides_format_shape_text]
// [START apps_script_slides_save_thumbnail]
/**
* Saves a thumbnail image of the current Google Slide presentation in Google Drive.
* Logs the image URL.
* @param {number} i The zero-based slide index. 0 is the first slide.
* @example saveThumbnailImage(0)
* @see https://developers.google.com/slides/api/reference/rest/v1/presentations.pages/getThumbnail
*/
function saveThumbnailImage(i) {
try {
const presentation = SlidesApp.getActivePresentation();
// Get the thumbnail of specified page
const thumbnail = Slides.Presentations.Pages.getThumbnail(
presentation.getId(), presentation.getSlides()[i].getObjectId());
// fetch the URL to the thumbnail image.
const response = UrlFetchApp.fetch(thumbnail.contentUrl);
const image = response.getBlob();
// Creates a file in the root of the user's Drive from a given Blob of arbitrary data.
const file = DriveApp.createFile(image);
console.log(file.getUrl());
} catch (e) {
// TODO (developer) - Handle Exception
console.log('Failed with error %s', e.message);
}
}
// [END apps_script_slides_save_thumbnail]