Duplicate Multiple Sheets in Google Sheets: A Step-by-Step Guide
Have you ever needed to duplicate multiple sheets in Google Sheets for different purposes, such as maintaining separate records or comparing data? While Google Sheets doesn't have a built-in feature to copy multiple sheets at once, there's a simple workaround using Google Apps Script.
By using a script, you can make this process more efficient, saving you time and reducing the risk of human error. In this guide, we'll walk you through the step-by-step process of copying multiple sheets in Google Sheets.
Understanding Google Apps Script
Before we dive in, let's briefly understand what Google Apps Script is. It's a JavaScript-based scripting language supported by Google. It allows you to automate tasks across multiple Google products and third-party services.
For our purpose, we'll use Google Apps Script to create a custom function that will copy multiple sheets in Google Sheets.
Setting Up Google Apps Script
First, you need to open your Google Sheets document, click on "Extensions" in the menu, then "Apps Script." This will open the Google Apps Script editor. Here, you can write, save, and run your scripts.
The editor window consists of several panes. You'll primarily work in the "Code.gs" file, which is where you'll write your scripts. On the left sidebar, you'll see your files and their content, and at the bottom, there's a toolbar with useful functions.
Writing the Copy Sheets Script
Now, let's write the script to copy multiple sheets. In the Code.gs file, delete any existing code and replace it with the following:
function copySheets() {
var ss = SpreadsheetApp.getActive();
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++) {
var newSheet = sheets[i].copyTo(ss);
}
}
This script does the following:
- Gets the active spreadsheet (your Google Sheets document),
- Gets all the sheets in the active spreadsheet,
- Loops through each sheet,
- Creates a copy of the sheet and adds it to the active spreadsheet.
Running the Script
With the script written, it's time to run it. Click the "Save" button in the toolbar, name your project (e.g., "CopySheets"), then click the play icon to run the script.
Running the script will copy all the sheets in your Google Sheets document. Once done, you'll see a confirmation message, and your copied sheets will appear at the end of your sheet list.
Modifying the Script for Specific Sheets
If you only want to copy specific sheets, you can modify the script. Replace the script above with the following:
function copySpecificSheets() {
var ss = SpreadsheetApp.getActive();
var sheetNames = ["Sheet1", "Sheet2"]; //replace with the names of the sheets you want to copy
sheetNames.forEach(sheetName => {
var sheet = ss.getSheetByName(sheetName);
var newSheet = sheet.copyTo(ss);
});
}
Replace "Sheet1" and "Sheet2" with the names of the sheets you want to copy. Running this script will only copy the specified sheets.
Remember, the script's performance depends on the size, complexity, and number of your sheets. Always be careful when running scripts, especially on large or complex documents, to avoid any unintended consequences.
In the world of automation and efficiency, Google Apps Script empowers us to perform tasks in Google Sheets seamlessly. Next time you need to duplicate multiple sheets, knowing this simple trick can save you valuable time and effort.