Skip to content

Commit cde744e

Browse files
authored
Merge pull request #101 from RajeshGogo/slides-snippets
Slides snippets
2 parents c2d2f98 + 906f46e commit cde744e

13 files changed

+885
-0
lines changed

slides/snippets/src/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"google/apiclient": "^2.2"
4+
}
5+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// [START slides_copy_presentation]
19+
20+
use Google\Service\Drive;
21+
use Google\Client;
22+
use Google\Service\Drive\DriveFile;
23+
24+
25+
26+
function copyPresentation($presentationId, $copyTitle)
27+
{
28+
/* Load pre-authorized user credentials from the environment.
29+
TODO(developer) - See https://developers.google.com/identity for
30+
guides on implementing OAuth2 for your application. */
31+
$client = new Google\Client();
32+
$client->useApplicationDefaultCredentials();
33+
$client->addScope(Google\Service\Drive::DRIVE);
34+
$driveService = new Google_Service_Drive($client);
35+
try {
36+
37+
$copy = new Google_Service_Drive_DriveFile(array(
38+
'name' => $copyTitle
39+
));
40+
$driveResponse = $driveService->files->copy($presentationId, $copy);
41+
$presentationCopyId = $driveResponse->id;
42+
printf("copyCreated at:%s\n ", $presentationCopyId);
43+
return $presentationCopyId;
44+
} catch (Exception $e) {
45+
echo 'Message: ' . $e->getMessage();
46+
}
47+
}
48+
49+
// [END slides_copy_presentation]
50+
51+
require_once 'vendor/autoload.php';
52+
copyPresentation('12ZqIbNsOdfGr99FQJi9mQ0zDq-Q9pdf6T3ReVBz0Lms', 'New File');
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// [START slides_create_bulleted_text]
19+
use Google\Client;
20+
use Google\Service\Drive;
21+
use Google\Service\Slides;
22+
use Google\Service\Slides\Request;
23+
24+
25+
function createBulletedText($presentationId, $shapeId)
26+
{
27+
/* Load pre-authorized user credentials from the environment.
28+
TODO(developer) - See https://developers.google.com/identity for
29+
guides on implementing OAuth2 for your application. */
30+
$client = new Google\Client();
31+
$client->useApplicationDefaultCredentials();
32+
$client->addScope(Google\Service\Drive::DRIVE);
33+
$slidesService = new Google_Service_Slides($client);
34+
// Add arrow-diamond-disc bullets to all text in the shape.
35+
$requests = array();
36+
$requests[] = new Google_Service_Slides_Request(array(
37+
'createParagraphBullets' => array(
38+
'objectId' => $shapeId,
39+
'textRange' => array(
40+
'type' => 'ALL'
41+
),
42+
'bulletPreset' => 'BULLET_ARROW_DIAMOND_DISC'
43+
)
44+
));
45+
46+
// Execute the request.
47+
$batchUpdateRequest = new Google_Service_Slides_BatchUpdatePresentationRequest(array(
48+
'requests' => $requests
49+
));
50+
$response = $slidesService->presentations->batchUpdate($presentationId, $batchUpdateRequest);
51+
printf("Added bullets to text in shape with ID: %s", $shapeId);
52+
return $response;
53+
}
54+
55+
// [END slides_create_bulleted_text]
56+
require 'vendor/autoload.php';
57+
createBulletedText('12ZqIbNsOdfGr99FQJi9mQ0zDq-Q9pdf6T3ReVBz0Lms', 'MyTextBox_01');
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// [START slides_create_image]
19+
use Google\Client;
20+
use Google\Service\Drive;
21+
use Google\Service\Slides;
22+
use Google\Service\Slides\Request;
23+
24+
25+
function createImage($presentationId, $pageId)
26+
{
27+
/* Load pre-authorized user credentials from the environment.
28+
TODO(developer) - See https://developers.google.com/identity for
29+
guides on implementing OAuth2 for your application. */
30+
$client = new Google\Client();
31+
$client->useApplicationDefaultCredentials();
32+
$client->addScope(Google\Service\Drive::DRIVE);
33+
$slidesService = new Google_Service_Slides($client);
34+
35+
try {
36+
37+
$imageUrl = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png';
38+
// Create a new image, using the supplied object ID, with content downloaded from imageUrl.
39+
$imageId = 'MyImage_01asdfsadfasdf';
40+
$emu4M = array('magnitude' => 4000000, 'unit' => 'EMU');
41+
$requests[] = new Google_Service_Slides_Request(array(
42+
'createImage' => array(
43+
'objectId' => $imageId,
44+
'url' => $imageUrl,
45+
'elementProperties' => array(
46+
'pageObjectId' => $pageId,
47+
'size' => array(
48+
'height' => $emu4M,
49+
'width' => $emu4M
50+
),
51+
'transform' => array(
52+
'scaleX' => 1,
53+
'scaleY' => 1,
54+
'translateX' => 100000,
55+
'translateY' => 100000,
56+
'unit' => 'EMU'
57+
)
58+
)
59+
)
60+
));
61+
62+
// Execute the request.
63+
$batchUpdateRequest = new Google_Service_Slides_BatchUpdatePresentationRequest(array(
64+
'requests' => $requests
65+
));
66+
$response = $slidesService->presentations->batchUpdate($presentationId, $batchUpdateRequest);
67+
$createImageResponse = $response->getReplies()[0]->getCreateImage();
68+
printf("Created image with ID: %s\n", $createImageResponse->getObjectId());
69+
70+
return $response;
71+
} catch (Exception $e) {
72+
echo 'Message: ' . $e->getMessage();
73+
}
74+
}
75+
76+
// [END slides_create_image]
77+
require 'vendor/autoload.php';
78+
createImage('12ZqIbNsOdfGr99FQJi9mQ0zDq-Q9pdf6T3ReVBz0Lms', 'abcd1234');
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// [START slides_create_presentation]
19+
use Google\Client;
20+
use Google\Service\Drive;
21+
use Google\Service\Slides;
22+
use Google\Service\Slides\Request;
23+
24+
function createPresentation($title)
25+
{
26+
/* Load pre-authorized user credentials from the environment.
27+
TODO(developer) - See https://developers.google.com/identity for
28+
guides on implementing OAuth2 for your application. */
29+
$client = new Google\Client();
30+
$client->useApplicationDefaultCredentials();
31+
$client->addScope(Google\Service\Drive::DRIVE);
32+
$service = new Google_Service_Slides($client);
33+
try {
34+
$presentation = new Google_Service_Slides_Presentation($title);
35+
//creating a presentation
36+
$presentation = $service->presentations->create($presentation);
37+
printf("Created presentation with ID: %s\n", $presentation->presentationId);
38+
return $presentation;
39+
} catch (Exception $e) {
40+
echo 'Message: ' . $e->getMessage();
41+
}
42+
}
43+
44+
// [END slides_create_presentation]
45+
require 'vendor/autoload.php';
46+
createPresentation("sample presentation");
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// [START slides_create_sheets_chart]
19+
use Google\Client;
20+
use Google\Service\Drive;
21+
use Google\Service\Slides;
22+
use Google\Service\Slides\Request;
23+
24+
25+
function createSheetsChart($presentationId, $pageId, $spreadsheetId, $sheetChartId)
26+
{
27+
/* Load pre-authorized user credentials from the environment.
28+
TODO(developer) - See https://developers.google.com/identity for
29+
guides on implementing OAuth2 for your application. */
30+
$client = new Google\Client();
31+
$client->useApplicationDefaultCredentials();
32+
$client->addScope(Google\Service\Drive::DRIVE);
33+
$slidesService = new Google_Service_Slides($client);
34+
// Embed a Sheets chart (indicated by the spreadsheet_id and sheet_chart_id) onto
35+
// a page in the presentation. Setting the linking mode as "LINKED" allows the
36+
// chart to be refreshed if the Sheets version is updated.
37+
try {
38+
//creating new presentaion chart
39+
$presentationChartId = 'MyEmbeddedChart';
40+
$emu4M = array('magnitude' => 4000000, 'unit' => 'EMU');
41+
$requests = array();
42+
$requests[] = new Google_Service_Slides_Request(
43+
array(
44+
'createSheetsChart' => array(
45+
'spreadsheetId' => $spreadsheetId,
46+
'chartId' => $sheetChartId,
47+
'linkingMode' => 'LINKED',
48+
'elementProperties' => array(
49+
'pageObjectId' => $pageId,
50+
'size' => array(
51+
'height' => $emu4M,
52+
'width' => $emu4M
53+
),
54+
'transform' => array(
55+
'scaleX' => 1,
56+
'scaleY' => 1,
57+
'translateX' => 100000,
58+
'translateY' => 100000,
59+
'unit' => 'EMU'
60+
)
61+
)
62+
)
63+
));
64+
65+
// Execute the request.
66+
$batchUpdateRequest = new Google_Service_Slides_BatchUpdatePresentationRequest(array(
67+
'requests' => $requests
68+
));
69+
$response = $slidesService->presentations->batchUpdate($presentationId, $batchUpdateRequest);
70+
printf("Added a linked Sheets chart with ID: %s\n", $response->getPresentationId());
71+
return $response;
72+
} catch (Exception $e) {
73+
echo 'Message: ' . $e->getMessage();
74+
}
75+
}
76+
77+
// [END slides_create_sheets_chart]
78+
require 'vendor/autoload.php';
79+
createSheetsChart('12ZqIbNsOdfGr99FQJi9mQ0zDq-Q9pdf6T3ReVBz0Lms', 'abcd1234', '1sN_EOj0aYp5hn9DeqSY72G7sKaFRg82CsMGnK_Tooa8', 122);
80+
?>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// [START slides_create_slide]
19+
use Google\Client;
20+
use Google\Service\Drive;
21+
use Google\Service\Slides\Request;
22+
use Google\Service\Slides\BatchUpdatePresentationRequest;
23+
24+
function createSlide($presentationId, $pageId)
25+
{
26+
/* Load pre-authorized user credentials from the environment.
27+
TODO(developer) - See https://developers.google.com/identity for
28+
guides on implementing OAuth2 for your application. */
29+
$client = new Google\Client();
30+
$client->useApplicationDefaultCredentials();
31+
$client->addScope(Google\Service\Drive::DRIVE);
32+
$slidesService = new Google_Service_Slides($client);
33+
try {
34+
$requests = array();
35+
$requests[] = new Google_Service_Slides_Request(array(
36+
'createSlide' => array(
37+
'objectId' => $pageId,
38+
'insertionIndex' => 1,
39+
'slideLayoutReference' => array(
40+
'predefinedLayout' => 'TITLE_AND_TWO_COLUMNS'
41+
)
42+
)
43+
));
44+
$batchUpdateRequest = new Google_Service_Slides_BatchUpdatePresentationRequest(array(
45+
'requests' => $requests
46+
));
47+
48+
//execute the request
49+
$response = $slidesService->presentations->batchUpdate($presentationId, $batchUpdateRequest);
50+
$createSlideResponse = $response->getReplies()[0]->getCreateSlide();
51+
printf("Created slide with ID: %s\n", $createSlideResponse->getObjectId());
52+
return $response;
53+
} catch (Exception $e) {
54+
echo 'Message: ' . $e->getMessage();
55+
}
56+
}
57+
58+
// [END slides_create_slide]
59+
require 'vendor/autoload.php';
60+
createSlide('12ZqIbNsOdfGr99FQJi9mQ0zDq-Q9pdf6T3ReVBz0Lms', 'ss1234');

0 commit comments

Comments
 (0)