能否通过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:
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.
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(); } }
- Most common animation effects are available via the
SlidesApp.AnimationEntryEffectenum—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 aTextRangeobject.
内容的提问来源于stack exchange,提问作者user76652

