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

如何通过Apps Script清除Google Slides选中幻灯片的格式?

解决Google Slides导入后格式残留问题的脚本

我完全懂你这种手动清除格式的痛苦——导入幻灯片后明明取消了原主题,却还是残留一堆格式,重新应用布局也不管用,只能挨个元素去清。下面我把你的初始代码完善,让它能处理选中的幻灯片(批量清除所有占位符格式)或者选中的单个/多个占位符,一键让内容适配当前演示文稿的母版样式。

完整脚本代码

function clearFormattingOfSlidesPlaceholders() {
  const presentation = SlidesApp.getActivePresentation();
  const selection = presentation.getSelection();
  const selectionType = selection.getSelectionType();

  switch (selectionType) {
    // 处理选中的幻灯片(批量操作)
    case SlidesApp.SelectionType.PAGE:
      const pageRange = selection.getPageRange();
      const pages = pageRange.getPages();
      Logger.log(`Clearing format for ${pages.length} selected pages`);
      
      pages.forEach(page => {
        const slide = page.asSlide();
        // 获取幻灯片上的所有占位符
        const placeholders = slide.getPlaceholders();
        clearPlaceholderFormatting(placeholders);
      });
      break;

    // 处理选中的占位符(单个或多个)
    case SlidesApp.SelectionType.SHAPE:
      const shapeRange = selection.getShapeRange();
      const shapes = shapeRange.getShapes();
      Logger.log(`Clearing format for ${shapes.length} selected shapes`);
      
      // 筛选出占位符类型的形状
      const placeholders = shapes.filter(shape => shape.getPlaceholderType() !== null);
      clearPlaceholderFormatting(placeholders);
      break;

    default:
      Logger.log("Please select either slides or placeholders to clear formatting");
  }
}

// 通用函数:清除占位符的格式
function clearPlaceholderFormatting(placeholders) {
  placeholders.forEach(placeholder => {
    // 清除占位符本身的格式(填充、边框等)
    placeholder.clearFormatting();
    
    // 清除占位符内文本的格式(字体、颜色、行距等)
    const textRange = placeholder.getText();
    textRange.clearFormatting();
    
    // 额外:确保占位符应用当前布局的样式(增强适配性)
    const slide = placeholder.getParentPage().asSlide();
    const layout = slide.getLayout();
    const layoutPlaceholder = layout.getPlaceholder(placeholder.getPlaceholderType());
    if (layoutPlaceholder) {
      placeholder.copyAttributes(layoutPlaceholder);
    }
  });
}

关键功能说明

  • 批量处理选中幻灯片:当你选中一张或多张幻灯片时,脚本会遍历每张幻灯片的所有占位符,清除它们的自定义格式,恢复到当前母版布局的默认样式。
  • 处理选中的占位符:如果只需要清理特定的占位符(比如某页的标题),直接选中这些元素,脚本会只处理选中的占位符,忽略普通形状。
  • 双重格式清除:不仅清除占位符的视觉样式(填充、边框),还会清除文本的所有自定义格式(字体、大小、颜色、行距等),确保完全适配母版。
  • 布局属性同步:额外加入了copyAttributes方法,把当前布局中对应占位符的属性同步过来,进一步保证样式和母版完全一致。

使用方法

  1. 打开你的Google Slides演示文稿
  2. 点击顶部菜单的「扩展程序」→「Apps Script」
  3. 把上面的代码粘贴进去,保存项目(随便起个名字,比如「Clear Slide Formatting」)
  4. 返回Slides,选中你要处理的幻灯片或占位符
  5. 点击「扩展程序」→「Clear Slide Formatting」→「clearFormattingOfSlidesPlaceholders」
  6. 授权脚本(第一次运行需要授权,按照提示操作即可)

这样就能一键搞定格式残留问题,不用再手动逐个清理啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:38:38