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

能否通过Google Apps Script对Google Slides对象动画进行增删改查?

Absolutely! Google Apps Script fully supports working with animations in Google Slides—you can add animations to objects like text boxes, shapes, images, and even access, modify, or delete existing animation data. Let’s walk through how this works:

1. Adding Animations to Slides Objects

You can attach animations to almost any interactive object in a slide. Here’s a quick example that adds a "Fly In" animation to a shape:

function addShapeAnimation() {
  const presentation = SlidesApp.getActivePresentation();
  const targetSlide = presentation.getSlides()[0]; // Grab the first slide
  const targetShape = targetSlide.getShapes()[0]; // Target the first shape on the slide

  // Create and configure the animation
  const newAnimation = targetShape.createAnimation()
    .setEntryEffect(SlidesApp.AnimationEntryEffect.FLY_IN_LEFT)
    .setDuration(1.2) // Animation runs for 1.2 seconds
    .setStartOffset(0.3); // 0.3-second delay before it starts
}

This works similarly for text boxes, images, or tables—just replace getShapes() with getTextBoxes(), getImages(), etc., to target the right object type.

2. Accessing, Modifying, and Deleting Animations

You’re not limited to just adding animations; you can also inspect and edit existing ones:

Accessing Animations

To get all animations on a slide (or a specific object), use getAnimations():

function inspectAnimations() {
  const presentation = SlidesApp.getActivePresentation();
  const targetSlide = presentation.getSlides()[0];
  
  // Get all animations on the slide
  const slideAnimations = targetSlide.getAnimations();
  
  slideAnimations.forEach((anim, idx) => {
    const targetObj = anim.getTarget();
    console.log(`Animation ${idx+1} targets a ${targetObj.getType()} with effect: ${anim.getEntryEffect()}`);
  });
}

Modifying Animations

You can tweak properties like entry effect, duration, or start offset after an animation is created:

function updateAnimation() {
  const presentation = SlidesApp.getActivePresentation();
  const targetSlide = presentation.getSlides()[0];
  const firstAnimation = targetSlide.getAnimations()[0];
  
  // Change the animation to a fade-in effect
  firstAnimation.setEntryEffect(SlidesApp.AnimationEntryEffect.FADE_IN);
  // Adjust duration to 2 seconds
  firstAnimation.setDuration(2);
}

Deleting Animations

To remove an animation, call the remove() method on the animation object:

function deleteAnimation() {
  const presentation = SlidesApp.getActivePresentation();
  const targetSlide = presentation.getSlides()[0];
  const animations = targetSlide.getAnimations();
  
  // Delete the first animation on the slide
  if (animations.length > 0) {
    animations[0].remove();
  }
}
Key Notes
  • Most common animation effects are available via the SlidesApp.AnimationEntryEffect enum—you can explore the full list of supported effects in the Apps Script documentation.
  • You can control the playback order of animations using setOrder() to determine which effect plays first.
  • For text boxes, you can even animate specific text ranges (not just the entire box) by calling createAnimation() on a TextRange object.

内容的提问来源于stack exchange,提问作者user76652

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:04:13