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

如何用Google App Script自动化处理Google Doc中缺失手绘的绘图?

解决Google Doc中Word文档手绘图形丢失的自动化方案

问题核心

Google Doc导入.docx文件时,组合绘图中的手绘图形无法被解析,导致丢失。以下是针对你提出的两种方案的自动化实现思路及代码示例,适配每日重复10次的批量需求。


方案1:批量将组合绘图替换为预存图片

实现思路

  • 遍历指定Drive文件夹中的所有.docx文件
  • 自动转换为Google Doc格式(或直接处理已转换的文档)
  • 基于文件名匹配预存的对应图片(需保证命名规则统一,如doc1.docx对应doc1.png)
  • 替换文档中的所有目标绘图(或精准定位特定绘图)

代码示例

function replaceDrawingsWithSavedImages() {
  const targetFolderId = "你的目标文件夹ID"; // 替换为实际文件夹ID
  const targetFolder = DriveApp.getFolderById(targetFolderId);
  
  // 建立文件名与对应图片Blob的映射
  const imageMapping = {};
  targetFolder.getFilesByType(MimeType.PNG).forEach(imgFile => {
    const baseName = imgFile.getName().replace(".png", "");
    imageMapping[baseName] = imgFile.getBlob();
  });

  // 遍历处理所有docx文件
  const docxFiles = targetFolder.getFilesByType(MimeType.MICROSOFT_WORD);
  while (docxFiles.hasNext()) {
    const docxFile = docxFiles.next();
    const docBaseName = docxFile.getName().replace(".docx", "");
    
    // 跳过无对应图片的文件
    if (!imageMapping[docBaseName]) continue;

    // 转换docx为Google Doc
    const processedDoc = DriveApp.createFile(docxFile.getBlob())
      .setName(`${docBaseName}_processed`);
    
    // 打开文档并替换绘图
    const docBody = DocumentApp.openById(processedDoc.getId()).getBody();
    const allDrawings = docBody.getImages();

    allDrawings.forEach(drawing => {
      // 可根据绘图的位置、尺寸等属性精准筛选目标绘图,避免误替换
      drawing.replaceImage(imageMapping[docBaseName]);
    });

    DocumentApp.getActiveDocument().saveAndClose();
  }
}

方案2:编辑现有绘图并插入图片后保存

实现思路

  • 针对已转换的Google Doc,定位其中的内嵌绘图元素
  • 打开对应的Google Drawing文件,删除原有手绘图形
  • 插入预存图片并保存绘图,同步更新到原文档

代码示例(需启用Drive API)

function editDrawingsAndInsertImages() {
  const targetDocId = "你的Google Doc ID"; // 替换为实际文档ID
  const targetImageId = "你的目标图片ID"; // 替换为实际图片ID
  const imageBlob = DriveApp.getFileById(targetImageId).getBlob();

  const doc = DocumentApp.openById(targetDocId);
  const docBody = doc.getBody();
  const allParagraphs = docBody.getParagraphs();

  allParagraphs.forEach(para => {
    const firstChild = para.getChild(0);
    // 识别内嵌绘图元素
    if (firstChild?.getType() === DocumentApp.ElementType.INLINE_DRAWING) {
      const drawingId = firstChild.asInlineDrawing().getId();
      const drawing = DrawingApp.openById(drawingId);

      // 删除所有手绘自由形状
      const allShapes = drawing.getShapes();
      allShapes.forEach(shape => {
        if (shape.getShapeType() === DrawingApp.ShapeType.FREEFORM) {
          shape.remove();
        }
      });

      // 插入图片到绘图左上角
      drawing.insertImage(0, 0, imageBlob);
      drawing.saveAndClose();
    }
  });

  doc.saveAndClose();
}

关键优化建议

  • 命名规则统一:确保.docx文件、对应图片的文件名保持一致,简化映射逻辑
  • 定时触发:通过Google Apps Script的「当前项目的触发器」设置每日定时执行,无需手动运行
  • 权限配置:首次运行脚本时需授权Drive、Document、Drawing的编辑权限
  • 精准筛选:如果文档包含多个绘图,可通过绘图的位置、描述文本等属性添加筛选条件,避免误操作

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 03:05:22