如何使用google slide api复制幻灯片

时间:2017-02-15 09:45:20

标签: php google-slides-api

我安装了google slide API,效果很好。我可以添加和修改幻灯片,但我需要通过id复制特定的幻灯片。 我按照doclmentation GOOGLE SLIDE API进行了操作,我找到了这个函数Google slide api duplicate object,但它给了我错误。 这是我的代码函数:

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

这就是错误:

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

我的ID为g796f0ce3dc2930a6_1的幻灯片如下:

enter image description here

3 个答案:

答案 0 :(得分:1)

更新5/26:幻灯片API现在支持将ReplaceAllTestRequest限制为单个页面。更新了要反映的答案。

DuplicateObjectRequest是用于复制幻灯片的正确请求。但是,objectIds参数不符合您的想法:它将objectIds从现有幻灯片中的页面元素映射到您希望它们在新幻灯片中具有的objectId。 {{ARTICLE_NAME}}之类的东西不是objectIds,而是表格单元格中的文本。

要替换幻灯片中的该文字,您可以使用ReplaceAllTextRequest。您可以使用该请求中的pageObjectIds参数将替换限制为仅创建的重复幻灯片。确保在DuplicateObjectRequest中指定将分配给新幻灯片的objectId。

答案 1 :(得分:1)

这是解决该问题的解决方案:

  1. 步骤1-创建一个具有幻灯片表​​的演示文稿
  2. 第2步 - 创建空白演示文稿
  3. 第3步调用使用我创建的函数复制幻灯片的演示文稿:
  4. 注意:$external_page包含演示文稿的所有幻灯片,因此您可以像这样循环播放

        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++;
            }
    
        }
    }
    

    您可以在链接演示文稿G-slide $presentationId上获得https://docs.google.com/presentation/d/{{presentationId}}/edit#slide=id.p 所有这些功能:

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

    您可以在Google幻灯片API Doc.it上找到如何创建它非常简单。 好看!。

答案 2 :(得分:1)

我认为您要尝试的是复制一张幻灯片,然后替换文本。我遇到过同样的问题。

1-使用 duplicateObject 复制幻灯片,并分配新的对象ID。这非常重要(请检查文档以查看有效值)。https://developers.google.com/slides/reference/rest/v1/presentations/request#duplicateobjectrequest

2-使用 replaceAllText 替换您的占位符。确保包括新的重复对象的pageObjectId。否则,替换将适用于演示文稿中的所有幻灯片。

样本:

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

您可能需要为所有占位符添加更多的 replaceAllText