Malki Mohamed
Malki Mohamed

Reputation: 1688

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

Upvotes: 2

Views: 1977

Answers (3)

Christian
Christian

Reputation: 799

I think what you are trying to do is duplicate a slide and then replaceText. I had the same issue.

1- Use duplicateObject to duplicate a slide, asign a new Object ID. This is very important(check the docs to see the valid values).https://developers.google.com/slides/reference/rest/v1/presentations/request#duplicateobjectrequest

2- Use replaceAllText to replace your placeholder. Be sure to include the pageObjectId of the new duplicate Object. Otherwise, the replace it going to be apply to all the slides in your presentation.

sample:

 {
   duplicateObject: {
     objectId: 'g796f0ce3dc2930a6_1',
     objectIds: { 'g796f0ce3dc2930a6_1': 'NEW_OBJECT_ID' }
   }
 },
 {
   replaceAllText: {
      pageObjectIds: ['NEW_OBJECT_ID'],
      containsText: {
        text: '{{ARTICLE_NAME}}',
        matchCase: true
      },
      replaceText: 'Article_1'
   }
 }

You might need to include more replaceAllText for all your placeholders

Upvotes: 1

Maurice Codik
Maurice Codik

Reputation: 598

Update 5/26: Slides API now supports limiting ReplaceAllTestRequest to individual pages. Updated the answer to reflect.

DuplicateObjectRequest is the right request to use to duplicate a slide. However, the objectIds parameter doesn't do what you think: it maps objectIds from page elements in your existing slide to the objectId you want them to have in your new slide. Things like {{ARTICLE_NAME}} are not objectIds, that's the text in the table cells.

To replace that text in your slides, you can use ReplaceAllTextRequest. You can use the pageObjectIds parameter in that request to limit the replacements to only the duplicate slide you created. Make sure you specify an objectId in DuplicateObjectRequest that will be assigned to the new slide.

Upvotes: 1

Malki Mohamed
Malki Mohamed

Reputation: 1688

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!.

Upvotes: 1

Related Questions