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

如何修改Gmail2GDrive脚本实现Google Drive文件覆盖?

How to Modify Gmail2GDrive to Overwrite Existing Files in Google Drive

Hey there! Glad to hear Gmail2GDrive is working well for you—let's tweak the script so it overwrites existing files in your Drive instead of skipping or duplicating them. This way you can maintain a single shareable link without having to update it every time a new version comes in.

Key Modification Logic

The core change is replacing the script's default "skip if file exists" behavior with "overwrite existing file if found, create new if not". Here's how to implement it:

  1. Open your Gmail2GDrive script in the Google Apps Script editor.
  2. Locate the file-saving function—this is usually named something like saveAttachment or processAttachment, where the script checks for existing files and creates new ones.

Replace Existing File-Check Logic

Find the section of code that looks similar to this (the default logic that skips existing files):

// Default Gmail2GDrive logic (example)
var existingFile = DriveApp.getFilesByName(attachment.getName());
if (existingFile.hasNext()) {
  Logger.log("File already exists, skipping: " + attachment.getName());
  return; // Skips saving the attachment
}
// Creates a new file if no existing file is found
var newFile = targetFolder.createFile(attachment);

Replace that with this overwrite-friendly code:

// Modified logic to overwrite existing files
var fileName = attachment.getName();
var targetFolder = DriveApp.getFolderById("YOUR_FOLDER_ID"); // Or your existing folder reference
var existingFiles = targetFolder.getFilesByName(fileName);

var finalFile;
if (existingFiles.hasNext()) {
  // Grab the existing file and overwrite its content
  finalFile = existingFiles.next();
  finalFile.setBlob(attachment); // Overwrites with the new attachment blob
  Logger.log("Successfully overwrote existing file: " + fileName);
} else {
  // Create a new file if no match is found in the target folder
  finalFile = targetFolder.createFile(attachment);
  Logger.log("Created new file: " + fileName);
}

Important Notes

  • Target Folder Scope: Using targetFolder.getFilesByName() instead of DriveApp.getFilesByName() ensures you only overwrite files in your specific target folder, avoiding accidental changes to other files with the same name elsewhere in Drive.
  • File Permissions: Make sure the Google account running the script has edit access to the target folder and existing files—otherwise, the overwrite will fail.
  • Multiple Same-Named Files: If there are multiple files with the exact same name in the target folder, the script will overwrite the first one it finds. If you need more precise control (e.g., by file ID), you can adjust the logic to search by file ID instead of name.

Test the Script

After making the changes:

  • Save the script and re-authorize if prompted (you may need to grant additional permissions for modifying existing files).
  • Run a test with an email attachment that matches an existing file name in your target Drive folder.
  • Check the Drive folder to confirm the existing file was updated with the new attachment content.

Hope this helps you maintain that single, shareable link without any extra hassle!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:24:08