Task guide
Back up a spreadsheet to Drive every week
Run the setup function once. Every Monday morning the script copies the spreadsheet into a Drive folder and puts the date in the copy name. The original is never touched.
1 · Prepare
Put these pieces in place
- Open the script from the spreadsheet you want to back up, with Extensions, then Apps Script, so the script belongs to that file.
- Create a Drive folder named Spreadsheet backups. The script stops instead of guessing if it cannot find it.
- Keep both functions in the same Apps Script project.
Change these values
- Backup folder name
Spreadsheet backupsThe Drive folder that receives the copies. It must already exist.
- Backup day
ScriptApp.WeekDay.MONDAYThe weekday the trigger runs. Other values look like ScriptApp.WeekDay.FRIDAY.
- Backup hour
6The hour the trigger runs, in the project time zone. 6 means some time between 6 AM and 7 AM.
- Date format
yyyy-MM-ddHow the date is written in the copy name, which is also what sorts the backups by date.
2 · Review before running
What the script can access and change
Permissions you approve
- Read the active spreadsheet, including its name and its file ID.
- Find a folder in Google Drive and create a copy of the spreadsheet file inside it.
- Create and view triggers in this Apps Script project.
Effects of one run
- Creates one weekly trigger when one is not already installed.
- Saves one dated copy of the whole spreadsheet in the backup folder each week.
- Does not change the original spreadsheet, and does not delete old backups. The folder grows by one file a week, so clear out old copies yourself now and then.
3 · Copy
Paste this into the Apps Script editor
Create or open a script file, replace the example values marked above, then save the project.
const BACKUP_FOLDER_NAME = 'Spreadsheet backups'; // The Drive folder that receives the copies. It must already exist.
const BACKUP_DAY = ScriptApp.WeekDay.MONDAY; // The weekday the trigger runs. Other values look like ScriptApp.WeekDay.FRIDAY.
const BACKUP_HOUR = 6; // The hour the trigger runs, in the project time zone. 6 means some time between 6 AM and 7 AM.
const DATE_FORMAT = 'yyyy-MM-dd'; // How the date is written in the copy name, which is also what sorts the backups by date.
function createWeeklyBackupTrigger() {
const alreadyInstalled = ScriptApp.getProjectTriggers().some((trigger) => {
return trigger.getHandlerFunction() === 'backUpSpreadsheetToDrive';
});
if (alreadyInstalled) return;
ScriptApp.newTrigger('backUpSpreadsheetToDrive')
.timeBased()
.onWeekDay(BACKUP_DAY)
.atHour(BACKUP_HOUR)
.create();
}
function backUpSpreadsheetToDrive() {
const folders = DriveApp.getFoldersByName(BACKUP_FOLDER_NAME);
if (!folders.hasNext()) throw new Error('Create a Drive folder named ' + BACKUP_FOLDER_NAME + ' before running this function.');
const folder = folders.next();
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const stamp = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), DATE_FORMAT);
const copyName = spreadsheet.getName() + ' backup ' + stamp;
DriveApp.getFileById(spreadsheet.getId()).makeCopy(copyName, folder);
Logger.log('Saved a backup named ' + copyName);
} 4 · Run safely
Follow these steps in order
Make the folder
In Drive, create one folder named Spreadsheet backups. Use that exact name, including the capital S and the space.
Run the backup by hand first
Paste both functions and save. Choose backUpSpreadsheetToDrive and press Run. Approve the access when Google asks, then open the folder and check that a dated copy is there.
Install the weekly trigger
Only after the test copy looks right, choose createWeeklyBackupTrigger and run it once. It does nothing when a trigger for backUpSpreadsheetToDrive already exists, so running it twice does not add a second trigger.
Check the trigger list
Open Triggers in the left sidebar of the editor. You should see backUpSpreadsheetToDrive as a week timer. A time trigger runs as the account that created it, and a script attached to a spreadsheet can still reach that file with getActiveSpreadsheet, so there is nothing else to set up.
5 · Confirm
How to tell it worked
The Spreadsheet backups folder holds a copy whose name is the spreadsheet name, the word backup, and the date, such as Orders backup 2026-01-05. A new copy appears each Monday.
If the result is different
Troubleshooting
- The script says Create a Drive folder named Spreadsheet backups before running this function.
- Create a folder with that exact name, or change the name in the code and in the editable values. If two folders share the name, the script uses the first one Drive returns.
- The error mentions getActiveSpreadsheet or a value that is null.
- This script has to belong to a spreadsheet. Open the editor from the file with Extensions, then Apps Script, rather than starting a project at script.google.com.
- The backup did not run at 6 AM.
- A time trigger runs some time inside the hour you choose, and it follows the project time zone. Check the time zone under Project Settings, the gear icon, and open Executions to see when the run happened.
Technical details
Names used in this script
Open these details when you want to adapt the example or look up an exact service or method name.
Show Apps Script symbols and service pages
Apps Script symbols
Service pages
Keep going
Related tasks, basics, and apps
- Task guide Run an Apps Script function every day
Run the setup function once, then Apps Script calls the reminder function each day around your chosen hour.
- Task guide Email yourself a weekly sheet summary
Run the setup function once. Each Friday the script reads the Orders tab, counts the nonblank rows, totals column C, counts the rows that are not Done, and emails the account that installed it.
- Object reference Trigger builder
ScriptApp.newTrigger(functionName) returns a TriggerBuilder that configures a timeBased(), forSpreadsheet(), or forForm() trigger before create() saves it.
- Object reference Spreadsheet
A Spreadsheet is the whole Google Sheets file. Use SpreadsheetApp.getActiveSpreadsheet() or openById() to get it, then call getSheetByName() or getActiveSheet().
- Object reference Drive file
A DriveApp.File is one item in Google Drive, such as a Doc, Sheet, or PDF. Open it with DriveApp.getFileById(id) to read its metadata, copy it, or export a PDF.
- Object reference Drive folder
A DriveApp.Folder is a container in Google Drive. Find one by name with DriveApp.getFoldersByName(name) or by ID with DriveApp.getFolderById(id).
- Basics Bound scripts and standalone scripts
Every Apps Script project is either attached to one Google file or standing on its own. The choice decides how your code reaches your data, who can see it, and where you find it later.
- Google app Google Sheets
SpreadsheetApp lets a Google Sheets script read a Sheet or Range, add a custom menu or formula, and run repeatable actions across rows.
- Google app Google Drive
DriveApp lets a script open files by ID, find a named folder with getFoldersByName, and save copies or PDF exports in one known place.
- Google app Triggers
A trigger is a saved schedule or event rule in ScriptApp that runs your function on time, on edit, or on form submit.