[GAS Beginner Series #44] Duplicate Files Automatically: How to Copy Templates with File.makeCopy()

When traveling across the vast expanse of Hokkaido, whether for a business trip or sightseeing, rental cars are an absolute essential. Arriving at New Chitose Airport, picking up your car, and enjoying the drive into downtown Sapporo or out to various regional hubs—in scenarios like these, generating documents such as reservation confirmations, estimates, and invitation letters happens constantly.

Duplicating a standardized template file, updating the customer name and dates, and saving it—doing this manually only takes a few minutes per file. However, repeating it day in and day out often leads to careless mistakes, such as “I forgot to make a copy and accidentally overwrote the master template!” or “Where did that copied file go in Google Drive?”

That’s why today we’re tackling File.makeCopy(), a Google Apps Script method that lets you programmatically duplicate (copy) any specified file on Google Drive in an instant!

Once you master this method, you will be able to automatically generate individual confirmation documents from a template Google Doc or Google Sheet in a single step. Combining it with Folder.getFiles(), which we covered in Lesson #43, will drastically speed up your file management automation!

1. What Is File.makeCopy() in Google Apps Script?

File.makeCopy() is a method that takes an existing file in Google Drive (such as a Google Doc, Spreadsheet, Slides presentation, or PDF) and creates an exact duplicate as a new file while preserving all of its original content and formatting.

Let’s review where this fits into “Phase 5: Google Drive & File Management” in this tutorial series:

  • Lesson #41: DriveApp.getFolderById() ➡ Identify and access a target destination folder
  • Lesson #42: Folder.createFile() ➡ Create a brand-new empty text file inside a folder
  • Lesson #43: Folder.getFiles() ➡ Retrieve the list of files (contents) inside a folder
  • Lesson #44: File.makeCopy() [Today’s Star!] ➡ Duplicate an existing template file

Unlike createFile() (Lesson #42), which creates a file from scratch, makeCopy() lets you replicate a fully pre-formatted master file containing completed layouts, styles, and calculation formulas. In the real world, it’s just like placing your original master document on an office copy machine and pressing print to get a fresh duplicate.

2. The 4 Syntax Patterns of makeCopy()

Depending on the arguments you pass, File.makeCopy() offers four different syntax patterns. Let’s organize them so you can pick the right one for your specific use case!

Pattern 1: No Arguments (Simplest)

var newFile = templateFile.makeCopy();
  • Behavior: Creates a copy in the same folder as the original file, named “Copy of [Original File Name]”.
  • Use Case: Best when you just need a quick, simple backup.

Pattern 2: Specifying Only the New File Name (String)

var newFile = templateFile.makeCopy("Car_Rental_Confirmation_Mr_Sato");
  • Behavior: Creates a duplicate in the same folder as the original file with the specified new file name.
  • Use Case: Handy when keeping copies in the same directory while assigning new, distinct names.

Pattern 3: Specifying Only the Destination Folder (Folder Object)

var newFile = templateFile.makeCopy(targetFolder);
  • Behavior: Creates a duplicate inside the specified destination folder, named “Copy of [Original File Name]”.
  • Use Case: Useful when you want to route copies directly into a different directory (e.g., an “Issued_2026” folder).

Pattern 4: Specifying Both the New File Name and the Destination Folder (The Best Practice!)

var newFile = templateFile.makeCopy("Car_Rental_Confirmation_Mr_Sato", targetFolder);
  • Behavior: Neatly generates a duplicate inside the target folder with your specified file name.
  • Use Case: The safest, most standard approach for building production automation workflows!

💡 Comparison Table of the 4 Patterns

Pattern Argument 1 Argument 2 Created Location Created File Name
1. No Args None None Same folder as original “Copy of [Original Name]”
2. Name Only New file name (String) None Same folder as original Specified file name
3. Folder Only Destination folder (Folder) None Specified folder “Copy of [Original Name]”
4. Name + Folder New file name (String) Destination folder (Folder) Specified folder Specified file name

3. STEP 1: Preparation! Create the Template File and Output Folder

Let’s set up the environment in Google Drive to run our file duplication test!

Instructions

  1. Open Google Drive
    Navigate to Google Drive in your web browser.
  2. Create Two Folders
    In any location (such as My Drive), create the following two folders:
    • 01_Template_Storage (Where the master template file lives)
    • 02_Confirmation_Output (Where duplicated files will be saved)
  3. Create the Template Document
    Inside the “01_Template_Storage” folder, create a new Google Doc and title it Rental Car Confirmation Template. Enter some sample text like the following:
[Hokkaido Rental Car - Reservation Confirmation]

Customer Name: {Customer Name}
Rental Start Date: October 01, 2025
Pickup Location: New Chitose Airport Branch (Drive Counter)
Return Location: Sapporo Station Branch
Reserved Vehicle: Compact Car (4WD, Standard Snow Tires Included)

We look forward to serving you!
  1. Retrieve File ID and Folder ID
    Copy and note down the unique IDs from the browser address bar (URL) for each item:
    • Template File ID:
      https://docs.google.com/document/d/[THIS_ID_STRING]/edit
    • Destination Folder ID (02_Confirmation_Output):
      https://drive.google.com/drive/folders/[THIS_ID_STRING]

4. STEP 2: Hands-On Code: How to Duplicate a Template into a Target Folder

Now that your files are ready, let’s open the Apps Script editor, write the script, and run it!

1. Enter the Code

Create a new Google Sheet or open an existing GAS project, then paste in the following code.

* Be sure to replace YOUR_TEMPLATE_FILE_ID and YOUR_TARGET_FOLDER_ID with the actual IDs you recorded in Step 1.

function copyRentalConfirmation() {
  // 1. Set the template file ID and destination folder ID
  var templateFileId = "YOUR_TEMPLATE_FILE_ID";
  var targetFolderId = "YOUR_TARGET_FOLDER_ID";

  // 2. Retrieve both objects using DriveApp (reviewing Lesson #41!)
  var templateFile = DriveApp.getFileById(templateFileId);
  var targetFolder = DriveApp.getFolderById(targetFolderId);

  // 3. Prepare the new file name
  var newFileName = "Car_Rental_Confirmation_Mr_Sato";

  // 4. Use makeCopy() to duplicate the file into the target folder with the new name!
  var copiedFile = templateFile.makeCopy(newFileName, targetFolder);

  // 5. Output results to the log (using Logger.log from Lesson #1!)
  Logger.log("■ File duplication completed successfully!");
  Logger.log("- New file name: " + copiedFile.getName());
  Logger.log("- New file ID: " + copiedFile.getId());
  Logger.log("- Access URL: " + copiedFile.getUrl());
}

2. Save and Run

  1. Click the Save icon (floppy disk) at the top of the editor.
  2. Verify that the selected function is set to copyRentalConfirmation, then click Run.
  3. (If this is your first time running it, grant the required Google Drive access permissions in the authorization dialog.)

3. Check Execution Results

If you see output like this in the Execution Log area at the bottom of the editor, you’re all set!

Info    ■ File duplication completed successfully!
Info    - New file name: Car_Rental_Confirmation_Mr_Sato
Info    - New file ID: 1abc12345xyz...
Info    - Access URL: https://docs.google.com/document/d/1abc12345xyz.../edit

Open the “02_Confirmation_Output” folder in Google Drive. You should see a brand-new file named “Car_Rental_Confirmation_Mr_Sato” sitting neatly inside, completely independent of your master template!

5. STEP 3: Real-World Workflow: Generating Dynamic File Names with Dates and Customer Data

In practical business environments, rather than manually typing names into script code each time, you’ll almost always want to automatically generate file names based on current system timestamps or customer records.

Here is practical code combining Utilities.formatDate() from Lesson #29 to generate an organized dynamic file name format: “YYYYMMDD_ReservationNo_Confirmation_CustomerName”.

function copyRentalConfirmationDynamic() {
  var templateFileId = "YOUR_TEMPLATE_FILE_ID";
  var targetFolderId = "YOUR_TARGET_FOLDER_ID";

  var templateFile = DriveApp.getFileById(templateFileId);
  var targetFolder = DriveApp.getFolderById(targetFolderId);

  // Format today's date as "yyyyMMdd" (applying Utilities.formatDate from Lesson #29)
  var today = new Date();
  var dateString = Utilities.formatDate(today, "JST", "yyyyMMdd");

  // Dynamic values normally retrieved from a spreadsheet or form submission
  var customerName = "Takahashi";
  var reservationNo = "R-20251001";

  // Construct the dynamic file name (e.g., 20250923_R-20251001_Car_Rental_Confirmation_Takahashi)
  var dynamicFileName = dateString + "_" + reservationNo + "_Car_Rental_Confirmation_" + customerName;

  // Duplicate the template into the destination folder with the generated name!
  var newFile = templateFile.makeCopy(dynamicFileName, targetFolder);

  Logger.log("Created with dynamic file name: " + newFile.getName());
}

When executed, this script saves the duplicated document with a clean title such as “20250923_R-20251001_Car_Rental_Confirmation_Takahashi“. Placing dates at the start keeps your Drive folder chronologically sorted whenever you sort files alphabetically—a huge productivity win!

6. 4 Common Traps Beginners Face and How to Avoid Them

Here are four frequent pitfalls beginners run into when working with File.makeCopy(). Keeping these in mind will save you hours of debugging!

Trap 1: makeCopy() Returns the “Newly Created File Object”!

This is the single most common misconception:

// ✕ Common mistake
templateFile.makeCopy("NewFileName", targetFolder);
// Trying to modify templateFile after this line... (This ruins your original template!)

When executed, makeCopy() returns the newly duplicated File object as its return value.

When we perform text replacements inside documents in upcoming lessons, remember that you must manipulate the copied file received via var newFile = templateFile.makeCopy(...), not the original templateFile!

Trap 2: Passing a “Folder ID String” Directly into the Second Argument

Many beginners mistakenly pass a folder ID string directly into the second argument:

// ✕ Error: The second argument is a raw string
templateFile.makeCopy("NewFileName", "YOUR_TARGET_FOLDER_ID");

Running this triggers an error: Exception: Cannot find method makeCopy(string,string).

Always remember that the second argument of makeCopy() requires a Folder object retrieved via DriveApp.getFolderById(), not a plain text string!

Trap 3: Omitting the Destination Folder Clutters Root “My Drive”

If you call templateFile.makeCopy("NewFileName") with only a name, the duplicate is placed in whatever parent folder the original template lives in.

However, if the template is located in a shared folder or a deep folder structure, omitting the destination folder (Pattern 4) can result in files accidentally being dumped into root “My Drive”.

In real-world automation, adopting Pattern 4—passing both the new file name and destination Folder object—is the safest best practice.

Trap 4: Handling Comments and Permissions from the Original File

When duplicating files via makeCopy(), all text body contents, embedded images, spreadsheet formulas, and styles are copied over flawlessly.

However, the new file receives a completely unique File ID. Specific granular sharing permissions and comment histories tied to the original template file might not carry over. If you need to share the new copy automatically, you can chain methods like File.addViewer() (covered in Lesson #50).

7. Summary & What’s Next

Today, we took a deep dive into using File.makeCopy() to automatically duplicate templates into fresh, customized business documents.

Key Takeaways

  • File.makeCopy() generates an exact duplicate of a template file
  • The two-argument format makeCopy(newFileName, targetFolder) is the industry standard best practice
  • It returns the newly duplicated File object as its return value
  • Pairing it with Utilities.formatDate() lets you generate cleanly structured, dynamic file names

Once you know how to clone template files, you can automate invoices, estimates, service agreements, and all sorts of repetitive documentation!

Now that you know how to copy files, questions like “How do I rename files after copying them?” or “How can I batch-rename existing files on a schedule?” will naturally pop up.

That brings us to Lesson #45: File.setName()!

We’ll walk through techniques for renaming and formatting file names dynamically, so stay tuned!