📋 Duplicate a Sheet in Google Spreadsheets
1. Overview
\[ \begin{array}{l} \textbf{Google Apps Script allows you to programmatically duplicate sheets} \\ \text{within the same spreadsheet or across different spreadsheets.} \\ \textbf{This is useful for creating templates, backups, or auto-generating tabs} \\ \text{based on user input or recurring schedules.} \end{array} \]
2. Use Cases
- Create a new sheet from a template for each client, project, or day
- Backup an existing sheet before edits
- Generate recurring reports or logs based on a master layout
- Programmatically copy layouts across files
Sheet duplication helps automate templating, data separation, and repetitive tasks.
3. Script to Duplicate a Sheet in the Same Spreadsheet
function duplicateSheet() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName("Template"); // source sheet name
const newName = "Copy - " + new Date().toLocaleDateString();
if (ss.getSheetByName(newName)) {
SpreadsheetApp.getUi().alert("Sheet already exists.");
return;
}
sheet.copyTo(ss).setName(newName);
}
This script copies the "Template" sheet and names it with todayβs date (or any logic).
4. Script to Duplicate Sheet to Another Spreadsheet
function duplicateToAnotherSpreadsheet() {
const sourceSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sourceSheet = sourceSpreadsheet.getSheetByName("Template");
const targetSpreadsheet = SpreadsheetApp.openById("TARGET_SPREADSHEET_ID");
const copy = sourceSheet.copyTo(targetSpreadsheet);
copy.setName("Imported Template");
targetSpreadsheet.setActiveSheet(copy);
}
This version allows you to copy a sheet between two spreadsheets.
Replace `"TARGET_SPREADSHEET_ID"` with the actual destination ID.
5. Optional: Duplicate and Clear Data
function duplicateAndClear() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const original = ss.getSheetByName("Template");
const newSheet = original.copyTo(ss).setName("New Blank Sheet");
newSheet.clearContents();
newSheet.getRange(1, 1, 1, original.getLastColumn())
.setValues(original.getRange(1, 1, 1, original.getLastColumn()).getValues());
}
This creates a clean duplicate with only headers (row 1) copied β useful for empty forms/logs.
6. Automate Duplication with Triggers
function setupDailyCopy() {
ScriptApp.newTrigger('duplicateSheet')
.timeBased()
.everyDays(1)
.atHour(7)
.create();
}
You can schedule automatic sheet duplication every day at a specific time.
7. Conclusion
Duplicating sheets via Google Apps Script is an easy way to create structured templates,
backups, or automated content. You can integrate it with triggers, forms, and buttons
to fully streamline your spreadsheet workflow.
With just a few lines of code, your Sheets can replicate themselves on command or schedule.