makeCopy in Google Apps Script- See My Automations in Action!

The makeCopy function is part of the DriveApp service, and it’s very straightforward to use.

makeCopy Method Variations

The makeCopy method has several variations that allow you to control how and where the copies are created. Here’s an overview:

  1. makeCopy():
    This version creates a copy of the file in the root of your Google Drive.

    var copy = file.makeCopy();  // Creates a copy in the root of Drive
  2. makeCopy(destination):
    Creates a copy of the file in a specified folder (destination).

    var folder = DriveApp.getFolderById('YOUR_FOLDER_ID');
    var copy = file.makeCopy(folder); // Copy saved to the specified folder
  3. makeCopy(name):
    Creates a copy of the file and renames it with the provided name.

    var copy = file.makeCopy('New File Name');  // Copy with a custom name
  4. makeCopy(name, destination):
    This variation creates a copy of the file in a specified folder and names it with the provided name.

    var folder = DriveApp.getFolderById('YOUR_FOLDER_ID');
    var copy = file.makeCopy('New File Name', folder); // Custom name and folder

makeCopy method stands out as an essential tool for managing and duplicating files.

I will show you how to use makeCopy in Google Apps Script, including my practical examples and best practices. Whether you are a beginner or an experienced user, you’ll find my guide helpful in automating file copying in Google Drive.

What is makeCopy in Google Apps Script?

The makeCopy method is a built-in function in Google Apps Script that allows you to duplicate Google Drive files such as Sheets, Docs, and Slides. This function is particularly useful when you need to create backups or template copies of documents, enabling collaboration or streamlining processes.

Syntax

DriveApp.getFileById(fileId).makeCopy(destinationFolder);

Parameters

  • fileId: The unique ID of the file you want to copy.
  • destinationFolder (optional): The folder where you want to save the new copy. If not provided, the file will be copied to the root of the user's Google Drive.

Practical Use Cases

  1. Create a Template Copy of a Google Sheet
    Imagine you have a Google Sheet template that you want to duplicate for multiple users. With makeCopy, you can automate this process, ensuring each user receives their own copy.

    function makeSheetCopy() {
    var templateId = 'YOUR_TEMPLATE_FILE_ID'; // Replace with your template's file ID
    var file = DriveApp.getFileById(templateId);
    var newCopy = file.makeCopy('Copy of Template');
    Logger.log('New copy created with ID: ' + newCopy.getId());
    }
  2. Backup Files Automatically
    You can automate the backup process by copying critical files into a specific backup folder.

    function backupFile() {
    var fileId = 'YOUR_FILE_ID'; // Replace with your file's ID
    var backupFolder = DriveApp.getFolderById('YOUR_BACKUP_FOLDER_ID'); // Replace with your folder's ID
    var file = DriveApp.getFileById(fileId);
    file.makeCopy('Backup of ' + file.getName(), backupFolder);
    Logger.log('Backup created in folder');
    }
  3. Mass File Duplication for Teams or Projects
    If you’re managing a team or working on multiple projects, you may need to create multiple copies of the same file for different folders.

    function copyFileForTeams() {
    var fileId = 'YOUR_FILE_ID'; // Replace with the file's ID
    var folders = ['FOLDER_ID_1', 'FOLDER_ID_2', 'FOLDER_ID_3']; // List of folder IDs where the file should be copied

    folders.forEach(function(folderId) {
    var folder = DriveApp.getFolderById(folderId);
    var file = DriveApp.getFileById(fileId);
    file.makeCopy('Copy for Team', folder);
    });
    Logger.log('File copied for all teams.');
    }

Advanced: Automating Naming and Organization

Using makeCopy, you can also customize the names of the copied files to include dates, user names, or other dynamic values. Here’s an example of how you can add a timestamp to each copy:

function makeTimedBackup() {
var fileId = 'YOUR_FILE_ID'; // Replace with your file's ID
var backupFolder = DriveApp.getFolderById('YOUR_BACKUP_FOLDER_ID'); // Replace with your folder's ID
var file = DriveApp.getFileById(fileId);
var timestamp = new Date().toISOString();
file.makeCopy('Backup_' + timestamp, backupFolder);
Logger.log('Timed backup created with name: Backup_' + timestamp);
}

Error Handling

When working with the makeCopy method, ensure that you handle potential errors gracefully. For instance, the script may fail if the file or folder ID is incorrect or if the user doesn't have the necessary permissions.

function makeCopyWithCheck() {
try {
var fileId = 'YOUR_FILE_ID'; // Replace with your file's ID
var backupFolder = DriveApp.getFolderById('YOUR_BACKUP_FOLDER_ID'); // Replace with your folder's ID
var file = DriveApp.getFileById(fileId);
file.makeCopy('Backup_' + new Date().toISOString(), backupFolder);
Logger.log('Copy created successfully.');
} catch (e) {
Logger.log('Error: ' + e.message);
}
}

Look at my best automation scripts using makeCopy

Having worked extensively with Google Apps Script, I’ve successfully automated various file management processes using the makeCopy method. Here are some of them.

1. Automate Weekly Report Creation from a Template

This script automatically creates a weekly report from a Google Sheets template and renames the copy with the current week's date.

function createWeeklyReport() {
var templateId = 'YOUR_TEMPLATE_ID'; // Replace with the template's ID
var folderId = 'YOUR_FOLDER_ID'; // Replace with the destination folder ID
var folder = DriveApp.getFolderById(folderId);
var file = DriveApp.getFileById(templateId);

var date = new Date();
var weekNumber = Math.ceil(date.getDate() / 7);
var reportName = 'Weekly Report - Week ' + weekNumber;

file.makeCopy(reportName, folder);
Logger.log('Weekly report created: ' + reportName);
}

2. Create a Custom Copy for Each Team Member

This script copies a Google Doc template and renames it for each team member listed in a predefined array. It also saves each copy to a designated folder.

function createTeamDocs() {
var templateId = 'YOUR_TEMPLATE_ID'; // Replace with your Doc template ID
var folderId = 'YOUR_FOLDER_ID'; // Replace with the destination folder ID
var folder = DriveApp.getFolderById(folderId);
var teamMembers = ['John Doe', 'Jane Smith', 'Alex Johnson']; // Team member names

var file = DriveApp.getFileById(templateId);

teamMembers.forEach(function(member) {
var newFileName = 'Document for ' + member;
file.makeCopy(newFileName, folder);
Logger.log('Created copy for: ' + member);
});
}

3. Automated Monthly Invoicing from a Template

This script automates monthly invoice generation by creating a copy of an invoice template for each client, renaming the copy with the current month, and saving it in a specific folder.

function createMonthlyInvoices() {
var templateId = 'YOUR_TEMPLATE_ID'; // Replace with your invoice template ID
var folderId = 'YOUR_FOLDER_ID'; // Replace with your folder ID
var folder = DriveApp.getFolderById(folderId);
var clients = ['Client A', 'Client B', 'Client C']; // Client names

var file = DriveApp.getFileById(templateId);
var month = new Date().toLocaleString('default', { month: 'long' });

clients.forEach(function(client) {
var newFileName = client + ' Invoice - ' + month;
file.makeCopy(newFileName, folder);
Logger.log('Invoice created for: ' + client);
});
}

4. Daily Backup of Critical Files

This script automatically backs up a set of critical files by copying them to a designated backup folder daily, appending the date to each filename.

function dailyFileBackup() {
var fileIds = ['FILE_ID_1', 'FILE_ID_2', 'FILE_ID_3']; // List of file IDs to back up
var backupFolderId = 'YOUR_BACKUP_FOLDER_ID'; // Backup folder ID
var backupFolder = DriveApp.getFolderById(backupFolderId);

var date = new Date().toISOString().split('T')[0]; // Get current date in YYYY-MM-DD format

fileIds.forEach(function(fileId) {
var file = DriveApp.getFileById(fileId);
var newFileName = file.getName() + '_Backup_' + date;
file.makeCopy(newFileName, backupFolder);
Logger.log('Backup created for: ' + file.getName());
});
}

Conclusion

These examples showcase how you can use makeCopy in Google Apps Script for various automation tasks, such as creating reports, invoicing, file backups, and more. By customizing the scripts to your needs, you can streamline repetitive file management processes and save valuable time.

Published