Task guide
Back up a spreadsheet to Drive every week
Keep a dated copy of a spreadsheet in a Drive folder. Run the setup function once, and every Monday morning the script saves a new copy with the date in its name.
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. If the folder is missing, the script stops with an error.
- 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
- See, edit, create, and delete all your Google Sheets spreadsheets. The code only reads the name and file ID of this spreadsheet.
- See, edit, create, and delete all of your Google Drive files. The code finds one folder by name and saves a copy of this spreadsheet in it.
- Allow this application to run when you are not present. This lets the script install its weekly trigger and run the backup on schedule.
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
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
Choose Extensions, then Apps Script. Paste both functions over the starter code and save. Choose backUpSpreadsheetToDrive in the function dropdown above the code, then click Run. Approve access when Google asks, then open the folder in Drive and check that a dated copy is there.
Install the weekly trigger
When the test copy looks right, choose createWeeklyBackupTrigger and click Run 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
Click Triggers, the clock icon in the left sidebar. backUpSpreadsheetToDrive is listed as a week timer. The 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 run stops with "Create a Drive folder named Spreadsheet backups before running this function."
- Create a folder with that exact name, or change BACKUP_FOLDER_NAME in the code to the name of your folder. If two folders share the name, the script uses the first one Drive returns.
- The run stops with "TypeError: Cannot read properties of null" and mentions getActiveSpreadsheet.
- Open the editor from the spreadsheet with Extensions, then Apps Script, and paste the code there. A project started at script.google.com has no active spreadsheet, so getActiveSpreadsheet returns null.
- The backup did not run at 6 AM.
- Open Executions in the left sidebar to see when the run happened, and check the time zone under Project Settings, the gear icon. A time trigger runs some time inside the hour you choose, in the project time zone.
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
Make a function run on its own once a day. Run the setup function once, and from then on Apps Script calls the reminder function each day around the hour you chose.
- Task guide Email yourself a weekly sheet summary
Get a short email every Friday with the numbers from an Orders tab: how many rows it has, the total of column C, and how many rows are not Done. Run the setup function once to install the weekly trigger.
- 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 DriveApp.getFileById and Drive files
DriveApp.getFileById(id) returns a DriveApp.File for a Google Drive file, such as a Doc, Sheet, or PDF. Pass the file ID as a string. The call throws an exception if the file does not exist or the executing account cannot access it.
- 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 kind of project affects how your code opens files, who can see the code, 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 act on each row, such as sending one email per row.
- Google app Google Drive
DriveApp lets a script open files by ID, find a named folder with getFoldersByName, and save copies or PDFs to a folder you choose.
- 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.