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

如何在Google Apps Script中批量生成对应JPG文件的快捷方式?

批量替换Google Drive中TIF快捷方式为JPG快捷方式

背景

我原本有权限访问共享Google Drive文件夹TIF_Images,其中包含Image01.tif至Image1000.tif的图片。我花费大量时间通过创建快捷方式将这些图片整理到不同的快捷方式文件夹中,例如Pictures_of_trees、Pictures_of_dogs。

由于机构归档政策,我现在失去了对TIF_Images的读取权限,但获得了JPG_Images的访问权限,其中包含所有相同图片的低分辨率.jpg副本。为避免手动重新整理所有图片,我希望使用Google Drive脚本读取每个包含.tif快捷方式的文件夹,并重新创建对应.jpg版本图片的快捷方式。

需求说明

给定共享Google Drive中一个包含Image01.tif和Image02.tif快捷方式的文件夹,代码需在同一文件夹中创建指向Image01.jpg和Image02.jpg的新快捷方式。预期代码应在Pictures_of_trees文件夹中创建两个新快捷方式,分别指向这两个JPG文件。

我已完成获取指定文件夹中所有.tif图片对应.jpg版本的部分,但在从.jpg文件列表生成快捷方式时遇到问题。这是我首次使用Google Apps Script或Javascript,代码可能存在问题,寻求帮助。

现有代码及问题

function myFunction() {
 // 获取存放JPG的文件夹
 var jpgFolder = DriveApp.getFolderById("JPG_Images文件夹ID");
 
 // 获取原快捷方式文件夹(即要创建新快捷方式的目标文件夹)
 var origFolder = DriveApp.getFolderById("如Pictures_of_trees这类快捷方式文件夹ID");
 var targetFolder = origFolder;


 // 取出原文件夹中所有.tif快捷方式
 var origFiles = origFolder.getFiles();
 while(origFiles.hasNext()){
   var origFile = origFiles.next();
   var origName = origFile.getName()
   // 将文件名中的tif替换为jpg
   const regex = new RegExp("(?:tif)", 'gm')
   var str = origName;
   const subst = `jpg`;
   
   // 得到替换后的JPG文件名
   var newName = str.replace(regex, subst);
   console.log("newName", newName);
   
   // 获取对应JPG文件
   var newFiles = jpgFolder.getFilesByName(newName)
   // 这里应该是出了问题——我需要的是单个File对象,但不知道怎么从FileIterator里获取

   // 这行代码应该在文件夹中创建指向对应JPG的快捷方式,但运行失败
   newFiles.createShortcut(targetFolder)
 }
}

代码核心问题:

  • jpgFolder.getFilesByName(newName)返回的是FileIterator(文件迭代器)对象,而非单个File对象,无法直接调用createShortcut()方法
  • Google Apps Script中,createShortcut()需要传入目标文件的ID,不能直接对迭代器操作

修复后代码

function replaceTifShortcutsWithJpg() {
  // 替换为你的JPG文件夹ID
  const jpgFolderId = "JPG_Images文件夹ID";
  // 替换为你的目标快捷方式文件夹ID(如Pictures_of_trees)
  const shortcutFolderId = "快捷方式文件夹ID";
  
  const jpgFolder = DriveApp.getFolderById(jpgFolderId);
  const targetFolder = DriveApp.getFolderById(shortcutFolderId);
  
  const tifShortcuts = targetFolder.getFiles();
  while (tifShortcuts.hasNext()) {
    const tifShortcut = tifShortcuts.next();
    // 跳过非快捷方式文件
    if (tifShortcut.getMimeType() !== "application/vnd.google-apps.shortcut") continue;
    
    const tifName = tifShortcut.getName();
    // 精准替换文件后缀为jpg,忽略大小写
    const jpgName = tifName.replace(/\.tif$/i, ".jpg");
    
    // 获取对应JPG文件
    const jpgFiles = jpgFolder.getFilesByName(jpgName);
    if (jpgFiles.hasNext()) {
      const jpgFile = jpgFiles.next();
      // 创建快捷方式并移动到目标文件夹
      DriveApp.createShortcut(jpgFile.getId())
        .setName(jpgName)
        .moveTo(targetFolder);
      console.log(`已创建快捷方式:${jpgName}`);
    } else {
      console.log(`未找到对应JPG文件:${jpgName}`);
    }
  }
}

关键修复点

  • 增加快捷方式类型判断,避免处理非快捷方式文件
  • 使用精准正则替换后缀,防止误替换文件名中包含"tif"的其他部分
  • 正确从FileIterator中提取单个JPG文件,获取其ID后调用createShortcut()
  • 创建快捷方式后设置名称并移动到目标文件夹

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 23:42:51