You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Google Script(非Google Script API)在Google幻灯片中替换文本为图片

Replace Text with Images in Google Slides Using Apps Script

Let’s walk through two approaches to achieve this—one using the Advanced Slides Service (your initial code, adjusted for clarity) and a native SlidesApp method if you prefer avoiding advanced services.

Option 1: Fixing Your Advanced Service Code

Your code uses the Advanced Slides Service (a built-in part of Google Apps Script, not the external REST API). To get it working, first enable the service:

  1. Open your Apps Script project.
  2. Go to Services > Add Service.
  3. Select Google Slides API and click Add.

Here’s your code with explanatory comments:

function insertImageCentered() { 
  var presentation = SlidesApp.getActivePresentation(); 
  var requests = [{ 
    "replaceAllShapesWithImage": { 
      "imageUrl": "https://docs.google.com/drawings/d/e/2PACX-1vR5mi6ujksb_2WtTFmk39IPYBIBlJ6WkzM1nsys9cT4Wquik627DDIRXzoYTgHPKX3fcvJzG9inDmJt/pub?w=960&h=720", 
      "imageReplaceMethod": "CENTER_INSIDE", // Centers image within the original shape's bounds
      "pageObjectIds": [presentation.getSlides()[0].getObjectId()], // Target only the first slide
      "containsText": { 
        "text": "{{CENTERED_SHAPE}}", // Text string to match and replace
        "matchCase": false // Ignore uppercase/lowercase differences
      } 
    } 
  }]; 
  // Send the batch update to replace all matching shapes
  Slides.Presentations.batchUpdate({'requests': requests}, presentation.getId()); 
}

This method is efficient for bulk replacements across slides, as it uses a single API call to handle all matches.

Option 2: Native SlidesApp Method (No Advanced Service)

If you want to avoid enabling the Advanced Slides Service, you can manually iterate through shapes and replace them with images. This gives you more control over individual elements:

function replaceTextWithImageNative() {
  var presentation = SlidesApp.getActivePresentation();
  var targetSlide = presentation.getSlides()[0]; // Focus on the first slide
  var targetText = "{{CENTERED_SHAPE}}";
  var imageUrl = "https://docs.google.com/drawings/d/e/2PACX-1vR5mi6ujksb_2WtTFmk39IPYBIBlJ6WkzM1nsys9cT4Wquik627DDIRXzoYTgHPKX3fcvJzG9inDmJt/pub?w=960&h=720";
  
  // Get all shapes on the target slide
  var shapes = targetSlide.getShapes();
  
  // Loop backwards to avoid index issues when deleting shapes
  for (var i = shapes.length - 1; i >= 0; i--) {
    var shape = shapes[i];
    var shapeText = shape.getText().asString().trim();
    
    // Check if the shape contains the exact target text
    if (shapeText === targetText) {
      // Capture the shape's position and size
      var left = shape.getLeft();
      var top = shape.getTop();
      var width = shape.getWidth();
      var height = shape.getHeight();
      
      // Insert the image at the same location and dimensions
      targetSlide.insertImage(imageUrl, left, top, width, height);
      
      // Remove the original text shape
      shape.remove();
    }
  }
}

Key Notes:

  • Ensure your image URL is publicly accessible (your Google Drawing link works since it’s published).
  • Test both methods on a copy of your presentation first to avoid accidental changes.
  • The Advanced Service method is faster for large presentations, while the native method is better for custom logic (e.g., replacing only specific instances).

内容的提问来源于stack exchange,提问作者Learn Apps Script

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 18:12:32