Search code examples
phpgoogle-slides-api

How to duplicate slide using google slide api


I have installed google slide API and it works perfectly. I can add and modify slide, but I need to duplicate a specific slide by id. I follow docmumentation GOOGLE SLIDE API and I found this function Google slide api duplicate object but it give me error. this is my code function :

'duplicateObject'=>array(
    'objectId'=>'g796f0ce3dc2930a6_1',
    'objectIds'=>array(
        '{{ARTICLE_NAME}}'=>'Article_1',
        '{{ARTICLE_PRICE}}'=> '100',
        '{{ARTICLE_QT}}'=>'5',
        '{{ARTICLE_HT}}'=>'500',
    ),
),

And this is the error :

Invalid requests[0].duplicateObject: The object with objectId ARTICLE_NAME could not be found

My slide with id g796f0ce3dc2930a6_1 looks like :

enter image description here


Solution

  • This is my solution to resolve that problem:

    1. step 1- create one presentation that has a slide table
    2. step 2- create a blank presentation
    3. step 3- call presentation that had the slide to duplicate by using this function that I created:

    NB: The $external_page has all slides of your presentation, so you can loop on it like that

        public function createSingleSlide(array $slide_elements,string         $presentationId,string $id_slide_multiple){
        $external_page = $this->getPresentation($presentationId);
        $index = 0;
        foreach ($external_page['slides'] as $key => $value) {
    
            if($value['objectId'] != $id_slide_multiple ){
    
                // create a blank slide
                $slideId = 'slide_'.rand();
                $requests_slide = $this->createSlide($slideId,$index);
                $response = $this->executeRequest($requests_slide);
    
                foreach ($value['pageElements'] as $key_pe => $value_pe) {
    
                    // we have to do test here if shape or table
                    if(isset($value_pe['shape'])){
    
                        $shapeType  = $value_pe['shape']['shapeType'];
                        $elementId  = $shapeType.'_'.rand();
                        $textElements =  $value_pe['shape']['text']['textElements'];
    
                        $requests = $this->createShape($elementId,$shapeType,$slideId,$value_pe['size'],$value_pe['transform']);
    
                        $response = $this->executeRequest($requests);
    
                        //insert all text of shape, table...etcs
                        foreach ($textElements as $key_text => $value_text) {
    
                            $text = $value_text['textRun']['content'];
    
                            if(isset($text) && !empty($text && $text !="\n") ){
    
                                $requests_text = $this->insertText($elementId,$text);
                                $response = $this->executeRequest($requests_text);
    
                                $requests_style = $this->updateTextStyle($elementId,$value_text['textRun']['style']);
                                $response = $this->executeRequest($requests_style);
                            }
                        }
                    }elseif (isset($value_pe['table'])) {
    
                        $rows       = $value_pe['table']['rows'];
                        $columns    = $value_pe['table']['columns'];
                        $elementId  = 'Table_'.rand();
    
                        $requests_table = $this->createTable($elementId,$slideId,$value_pe['size'],$value_pe['transform'],$rows,$columns);
                        $response = $this->executeRequest($requests_table);
    
                        // insert all rows in table
                        foreach ($value_pe['table']['tableRows'] as $key_rows => $value_rows) {
                            foreach ($value_rows['tableCells'] as $key_cells => $value_cells) {
    
                                $textElements           = $value_cells['text']['textElements'];
                                $tableCellProperties    = $value_cells['tableCellProperties'];
                                $location               = array();
                                $rowSpan                = $value_cells['rowSpan'];
                                $columnSpan             = $value_cells['columnSpan'];
    
                                if(isset($value_cells['location']['rowIndex'])){
                                    $location['rowIndex'] = $value_cells['location']['rowIndex'];
                                }
                                if(isset($value_cells['location']['columnIndex'])){
                                    $location['columnIndex'] = $value_cells['location']['columnIndex'];
                                }
    
                                //insert all text of shape, table...etcs
                                foreach ($textElements as $key_text => $value_text) {
    
                                    $text = $value_text['textRun']['content'];
                                    $requests_text_and_style =array();
    
                                    if(isset($text) && !empty($text && $text !="\n") ){
    
                                        $requests_text_and_style[] = $this->insertTableText($elementId,$text,$location);
    
                                        $requests_text_and_style[] = $this->updateTableCellProperties($elementId,$tableCellProperties,$location,$rowSpan,$columnSpan);
    
                                        $requests_text_and_style[] = $this->updateTextStyleTable($elementId,$value_text['textRun']['style'],$location);
    
                                        $response = $this->executeRequest($requests_text_and_style);
                                    }
                                }
                            }
                        }
    
                    }
                }
    
                // replace varaibales in slide $slideId
                $requests_texts = array(); 
    
                if(isset($slide_elements['replaceText'])){
                    $requests_texts[] = $this->replaceText($slide_elements['replaceText']);
                }
                if(isset($slide_elements['replaceAllShapesWithImage'])){
                    $requests_texts[] = $this->replaceAllShapesWithImage($slide_elements['replaceAllShapesWithImage']);
                }
    
                $response = $this->executeRequest($requests_texts);
                $index++;
            }
    
        }
    }
    

    You can get $presentationId on your link presentation G-slide https://docs.google.com/presentation/d/{{presentationId}}/edit#slide=id.p All of this function :

    • createSlide()
    • executeRequest
    • createShape
    • insertText
    • updateTextStyle
    • replaceText
    • replaceAllShapesWithImage
    • createTable
    • ...

    You can found how to create it on Google slide API Doc.it very simple. Good look!.