如何在Google幻灯片中添加标题与正文?PHP实现方法咨询
Got it, let's cover both the manual way to add content to your slides and how to extend your existing PHP code to populate the title and description programmatically.
Manual Steps for Google Slides
If you're working directly in the Google Slides interface:
- Open your presentation and navigate to the slide (or create a new one with the
TITLE_AND_BODYlayout, which matches what you're using in code). - You'll see two pre-made placeholders: one at the top for the title, and a larger box below for body content.
- Click the title placeholder and type your desired title text. Use the top toolbar to format it (font size, color, bold, etc.) if needed.
- Click the body placeholder to add your description or main content. You can add bullet points, line breaks, or formatted text here too.
Extending Your PHP Code to Add Title and Description
You already have the code to create a slide with the TITLE_AND_BODY layout—great start! Now we just need to add additional requests to the batch update to insert text into those placeholders.
Here's how to update your code:
We'll add two InsertText requests: one for the title, one for the body. We can target the placeholders using their predefined types (TITLE and BODY) which works reliably for standard layouts like TITLE_AND_BODY.
// Your existing createSlide request (unchanged) $requests = array(); $requests[] = new \Google_Service_Slides_Request( array( 'createSlide' => array( 'objectId' => $nameSlide, 'insertionIndex' => 0, 'slideLayoutReference' => array( 'predefinedLayout' => 'TITLE_AND_BODY' ) ) ) ); // Add request to insert the slide title $requests[] = new \Google_Service_Slides_Request( array( 'insertText' => array( 'objectId' => $nameSlide, // Target the slide we just created 'text' => 'My Custom Slide Title', 'location' => array( 'placeholder' => array( 'type' => 'TITLE', 'index' => 0 // Only one title placeholder exists in this layout ) ) ) ) ); // Add request to insert the body/description text $requests[] = new \Google_Service_Slides_Request( array( 'insertText' => array( 'objectId' => $nameSlide, 'text' => 'This is the body content for my slide. You can add line breaks with \n like this!\n- Bullet point 1\n- Bullet point 2', 'location' => array( 'placeholder' => array( 'type' => 'BODY', 'index' => 0 ) ) ) ) ); // Execute the batch update (don't forget to replace your presentation ID!) $batchUpdateRequest = new \Google_Service_Slides_BatchUpdatePresentationRequest(array( 'requests' => $requests )); $response = self::$serviceSlide->presentations->batchUpdate('YOUR_PRESENTATION_ID', $batchUpdateRequest);
Important Notes:
- Replace
'YOUR_PRESENTATION_ID'with the actual ID from your Google Slides URL (it's the long string between/d/and/edit). - The
objectIdin theinsertTextrequests matches$nameSlide, so we're sure to target the exact slide we just created. - For multi-line text or bullet points in the body, use
\nto create line breaks in thetextstring. - If you're working with a custom layout and need more precision, you can first fetch the slide's elements to get the placeholder object IDs. Here's a quick snippet to do that:
// Fetch the slide to get placeholder IDs (optional) $slide = self::$serviceSlide->presentations_pages->get('YOUR_PRESENTATION_ID', $nameSlide); foreach ($slide->getPageElements() as $element) { if ($element->getShape() && $element->getShape()->getPlaceholder()) { $placeholder = $element->getShape()->getPlaceholder(); echo "Placeholder Type: " . $placeholder->getType() . ", Object ID: " . $element->getObjectId() . "\n"; } }
Once you have the placeholder's object ID, you can use it in the insertText request's location.objectId field instead of the placeholder type.
内容的提问来源于stack exchange,提问作者David

