2

I have a slide file called 'Slide A'. The file ID is simply 'A00001'.

There is another slide file called 'Slide B'. The file ID is simply 'B00002'.

I want to add the pages from Slide B into Slide A.

I have written the following code, trying to achieve this using the batchUpdate method, but it seems that instead of adding pages from Slide B, the pages from Slide A are being duplicated.

$slide_01_id = 'A00001';
$slide_02_id = 'B00002';

$slide_02 = $this->slidesService->presentations->get($slide_02_id);
$requests = [];

foreach ($slide_02->getSlides() as $slide) {
    $requests[] = new \Google_Service_Slides_Request([
        'duplicateObject' => [
            'objectId' => $slide->getObjectId(),
        ]
    ]);
}
$batchUpdateRequest = new \Google_Service_Slides_BatchUpdatePresentationRequest();
$batchUpdateRequest->setRequests($requests);
$this->slidesService->presentations->batchUpdate($slide_01_id, $batchUpdateRequest);

I would like to know how to add pages from one slide file to another slide file.

Alternatively, if it's possible to add pages from another file, I would be fine with using Microsoft PowerPoint as well.

0

1 Answer 1

1

Use the createSlide method, - instead of duplicating, you should create new slides in Slide A based on the slides from Slide B.

$slide_01_id = 'A00001';
$slide_02_id = 'B00002';

$slide_02 = $this->slidesService->presentations->get($slide_02_id);
$requests = [];


foreach ($slide_02->getSlides() as $slide) {
    $requests[] = new \Google_Service_Slides_Request([
        'createSlide' => [
            'objectId' => $slide->getObjectId() . '_copy', // Unique ID for the new slide
            'insertionIndex' => '1', // Position to insert the new slide
            'slideLayoutReference' => [
                'predefinedLayout' => 'TITLE_AND_BODY' // Choose a layout
            ]
        ]
    ]);

    // Copy the content from the original slide to the new slide
    foreach ($slide->getPageElements() as $element) {
        $requests[] = new \\Google_Service_Slides_Request([
            'duplicateObject' => [
                'objectId' => $element->getObjectId(),
                'objectIds' => [
                    $element->getObjectId() => $slide->getObjectId() . '_copy'
                ]
            ]
        ]);
    }
}


$batchUpdateRequest = new \Google_Service_Slides_BatchUpdatePresentationRequest();
$batchUpdateRequest->setRequests($requests);


$this->slidesService->presentations->batchUpdate($slide_01_id, $batchUpdateRequest);

Avoid copy pasting.

Sign up to request clarification or add additional context in comments.

4 Comments

I would like the content of the slides in File B to be added as-is, but currently, blank pages corresponding to the number of pages in File B are being created instead. I want the final output to consist of the content of File A + File B. Is additional processing, such as copying the content, required to achieve this?
@Devatim see update, and please accept the answer
"status": "INVALID_ARGUMENT", "message": "Invalid requests[1].duplicateObject: The object (p1_i15) could not be found.", Unfortunately, it wasn't possible to copy. Based on the error message, I suspect that it might be trying to search for objects within the A slide file. It seems that the object from slide B (p1_i15) is not being copied.
it works, but fails for some particular objects, - then you just need to catch such cases by try-catch, so they do not interrupt other iterations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.