Skip to content

Commit 85c853c

Browse files
committed
t push origin masterMerge branch 'RajeshGogo-slides_snippets_2'
2 parents fa65ef6 + 9cf2ba6 commit 85c853c

File tree

4 files changed

+372
-0
lines changed

4 files changed

+372
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START slides_image_merging]
18+
/**
19+
* Add an image to a template presentation.
20+
* @param {string} templatePresentationId The template presentation ID.
21+
* @param {string} imageUrl The image URL
22+
* @param {string} customerName A customer name used for the title
23+
*/
24+
async function imageMerging(templatePresentationId, imageUrl, customerName) {
25+
const {GoogleAuth} = require('google-auth-library');
26+
const {google} = require('googleapis');
27+
28+
const auth = new GoogleAuth(
29+
{scopes: ['https://www.googleapis.com/auth/presentations',
30+
'https://www.googleapis.com/auth/drive']});
31+
32+
const slidesService = google.slides({version: 'v1', auth});
33+
const driveService = google.drive({version: 'v2', auth});
34+
const logoUrl = imageUrl;
35+
const customerGraphicUrl = imageUrl;
36+
37+
// Duplicate the template presentation using the Drive API.
38+
const copyTitle = customerName + ' presentation';
39+
try {
40+
const driveResponse = await driveService.files.copy({
41+
fileId: templatePresentationId,
42+
resource: {
43+
name: copyTitle,
44+
},
45+
});
46+
const presentationCopyId = driveResponse.data.id;
47+
48+
// Create the image merge (replaceAllShapesWithImage) requests.
49+
const requests = [{
50+
replaceAllShapesWithImage: {
51+
imageUrl: logoUrl,
52+
replaceMethod: 'CENTER_INSIDE',
53+
containsText: {
54+
text: '{{company-logo}}',
55+
matchCase: true,
56+
},
57+
},
58+
}, {
59+
replaceAllShapesWithImage: {
60+
imageUrl: customerGraphicUrl,
61+
replaceMethod: 'CENTER_INSIDE',
62+
containsText: {
63+
text: '{{customer-graphic}}',
64+
matchCase: true,
65+
},
66+
},
67+
}];
68+
69+
// Execute the requests for this presentation.
70+
const batchUpdateResponse = await slidesService.presentations.batchUpdate({
71+
presentationId: presentationCopyId,
72+
resource: {
73+
requests,
74+
},
75+
});
76+
let numReplacements = 0;
77+
for (let i = 0; i < batchUpdateResponse.data.replies.length; ++i) {
78+
numReplacements += batchUpdateResponse.data.replies[i]
79+
.replaceAllShapesWithImage.occurrencesChanged;
80+
}
81+
console.log(`Created merged presentation with ID: ${presentationCopyId}`);
82+
console.log(`Replaced ${numReplacements} shapes with images.`);
83+
} catch (err) {
84+
// TODO (developer) - Handle exception
85+
throw err;
86+
}
87+
// [END slides_image_merging]
88+
}
89+
90+
imageMerging('12zc4QWOtsJZ0weX3zFHj5O_IVRhXQYqOXjbia4hoXw4',
91+
'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png');
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START slides_simple_text_replace]
18+
/**
19+
* Replaces text in the provided shape ID.
20+
* @param {string} presentationId The presentation ID.
21+
* @param {string} shapeId The shape ID to delete existing text and insert new text into.
22+
* @param {string} replacementText The new replacement text.
23+
*/
24+
async function simpleTextReplace(presentationId, shapeId, replacementText) {
25+
const {GoogleAuth} = require('google-auth-library');
26+
const {google} = require('googleapis');
27+
28+
const auth = new GoogleAuth(
29+
{scopes: 'https://www.googleapis.com/auth/presentations'});
30+
31+
const service = google.slides({version: 'v1', auth});
32+
// Remove existing text in the shape, then insert new text.
33+
const requests = [{
34+
deleteText: {
35+
objectId: shapeId,
36+
textRange: {
37+
type: 'ALL',
38+
},
39+
},
40+
}, {
41+
insertText: {
42+
objectId: shapeId,
43+
insertionIndex: 0,
44+
text: replacementText,
45+
},
46+
}];
47+
// Execute the requests.
48+
try {
49+
const batchUpdateResponse = await service.presentations.batchUpdate({
50+
presentationId,
51+
resource: {
52+
requests,
53+
},
54+
});
55+
console.log(`Replaced text in shape with ID: ${shapeId}`);
56+
console.log('in presentation with ID: ' +
57+
batchUpdateResponse.data.presentationId);
58+
} catch (err) {
59+
// TODO (developer) - Handle exception
60+
throw err;
61+
}
62+
}
63+
// [END slides_simple_text_replace]
64+
65+
simpleTextReplace('12zc4QWOtsJZ0weX3zFHj5O_IVRhXQYqOXjbia4hoXw4',
66+
'MyTextBox_01', 'My_text_which_is_new');
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START slides_text_merging]
18+
/**
19+
* Adds data from a spreadsheet to a template presentation.
20+
* @param {string} templatePresentationId The template presentation ID.
21+
* @param {string} dataSpreadsheetId The data spreadsheet ID.
22+
*/
23+
async function textMerging(templatePresentationId, dataSpreadsheetId) {
24+
const {GoogleAuth} = require('google-auth-library');
25+
const {google} = require('googleapis');
26+
27+
const auth = new GoogleAuth(
28+
{scopes: ['https://www.googleapis.com/auth/presentations',
29+
'https://www.googleapis.com/auth/drive',
30+
'https://www.googleapis.com/auth/spreadsheet']});
31+
32+
const slidesService = google.slides({version: 'v1', auth});
33+
const sheetsService = google.sheets({version: 'v4', auth});
34+
const driveService = google.drive({version: 'v2', auth});
35+
36+
// Use the Sheets API to load data, one record per row.
37+
const responses = [];
38+
const dataRangeNotation = 'Customers!A2:M6';
39+
40+
try {
41+
const sheetsResponse = await sheetsService.spreadsheets.values.get({
42+
spreadsheetId: dataSpreadsheetId,
43+
range: dataRangeNotation,
44+
});
45+
const values = sheetsResponse.data.values;
46+
47+
// For each record, create a new merged presentation.
48+
for (let i = 0; i < values.length; ++i) {
49+
const row = values[i];
50+
const customerName = row[2]; // name in column 3
51+
const caseDescription = row[5]; // case description in column 6
52+
const totalPortfolio = row[11]; // total portfolio in column 12
53+
54+
// Duplicate the template presentation using the Drive API.
55+
const copyTitle = customerName + ' presentation';
56+
let requests = {
57+
name: copyTitle,
58+
};
59+
60+
const driveResponse = await driveService.files.copy({
61+
fileId: templatePresentationId,
62+
requests,
63+
});
64+
65+
const presentationCopyId = driveResponse.data.id;
66+
// Create the text merge (replaceAllText) requests for this presentation.
67+
requests = [{
68+
replaceAllText: {
69+
containsText: {
70+
text: '{{customer-name}}',
71+
matchCase: true,
72+
},
73+
replaceText: customerName,
74+
},
75+
}, {
76+
replaceAllText: {
77+
containsText: {
78+
text: '{{case-description}}',
79+
matchCase: true,
80+
},
81+
replaceText: caseDescription,
82+
},
83+
}, {
84+
replaceAllText: {
85+
containsText: {
86+
text: '{{total-portfolio}}',
87+
matchCase: true,
88+
},
89+
replaceText: totalPortfolio,
90+
},
91+
}];
92+
// Execute the requests for this presentation.
93+
const batchUpdateResponse = await slidesService.presentations.batchUpdate({
94+
presentationId: presentationCopyId,
95+
resource: {
96+
requests,
97+
},
98+
});
99+
const result = batchUpdateResponse.data;
100+
// [START_EXCLUDE silent]
101+
responses.push(result.replies);
102+
// [END_EXCLUDE]
103+
// Count the total number of replacements made.
104+
let numReplacements = 0;
105+
for (let i = 0; i < result.replies.length; ++i) {
106+
numReplacements += result.replies[i].replaceAllText.occurrencesChanged;
107+
}
108+
console.log(`Created presentation for ${customerName} with ID: ` +
109+
presentationCopyId);
110+
console.log(`Replaced ${numReplacements} text instances`);
111+
}
112+
} catch (err) {
113+
// TODO (developer) - Handle exception
114+
throw err;
115+
}
116+
}
117+
// [END slides_text_merging]
118+
119+
textMerging('12zc4QWOtsJZ0weX3zFHj5O_IVRhXQYqOXjbia4hoXw4',
120+
'1uSTAkV11mnou78uRdTYcy36owjZR2mWMDAeRhXEImjE');
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// [START slides_text_style_update]
2+
/**
3+
* Updates text style for a specific presentation's shape ID.
4+
* @param {string} presentationId The presentation ID.
5+
* @param {string} shapeId The shape ID.
6+
*/
7+
async function textStyleUpdate(presentationId, shapeId) {
8+
// Update the text style so that the first 5 characters are bolded
9+
// and italicized, the next 5 are displayed in blue 14 pt Times
10+
// New Roman font, and the next 5 are hyperlinked.
11+
const {GoogleAuth} = require('google-auth-library');
12+
const {google} = require('googleapis');
13+
14+
const auth = new GoogleAuth(
15+
{scopes: 'https://www.googleapis.com/auth/presentations'});
16+
17+
const service = google.slides({version: 'v1', auth});
18+
19+
const requests = [{
20+
updateTextStyle: {
21+
objectId: shapeId,
22+
textRange: {
23+
type: 'FIXED_RANGE',
24+
startIndex: 0,
25+
endIndex: 5,
26+
},
27+
style: {
28+
bold: true,
29+
italic: true,
30+
},
31+
fields: 'bold,italic',
32+
},
33+
}, {
34+
updateTextStyle: {
35+
objectId: shapeId,
36+
textRange: {
37+
type: 'FIXED_RANGE',
38+
startIndex: 5,
39+
endIndex: 10,
40+
},
41+
style: {
42+
fontFamily: 'Times New Roman',
43+
fontSize: {
44+
magnitude: 14,
45+
unit: 'PT',
46+
},
47+
foregroundColor: {
48+
opaqueColor: {
49+
rgbColor: {
50+
blue: 1.0,
51+
green: 0.0,
52+
red: 0.0,
53+
},
54+
},
55+
},
56+
},
57+
fields: 'foregroundColor,fontFamily,fontSize',
58+
},
59+
}, {
60+
updateTextStyle: {
61+
objectId: shapeId,
62+
textRange: {
63+
type: 'FIXED_RANGE',
64+
startIndex: 10,
65+
endIndex: 15,
66+
},
67+
style: {
68+
link: {
69+
url: 'www.example.com',
70+
},
71+
},
72+
fields: 'link',
73+
},
74+
}];
75+
76+
// Execute the requests.
77+
try {
78+
const batchUpdateResponse = await service.presentations.batchUpdate({
79+
presentationId,
80+
resource: {
81+
requests,
82+
},
83+
});
84+
console.log(`Updated the text style for shape with ID: ${shapeId}`);
85+
console.log('in presentation with ID: ' +
86+
batchUpdateResponse.data.presentationId);
87+
} catch (err) {
88+
// TODO (developer) - Handle exceptions
89+
throw err;
90+
}
91+
}
92+
// [END slides_text_style_update]
93+
94+
textStyleUpdate('12zc4QWOtsJZ0weX3zFHj5O_IVRhXQYqOXjbia4hoXw4',
95+
'MyTextBox_01');

0 commit comments

Comments
 (0)