2/26/2016 - 1:47 PM

Creating automatic scheduled backup copies of your Google Sheets using Google Apps Script

Creating automatic scheduled backup copies of your Google Sheets using Google Apps Script

// Abhijeet Chopra
// 26 February 2016
// Google Apps Script to make copies of Google Sheet in specified destination folder

function makeCopy() {

// generates the timestamp and stores in variable formattedDate as year-month-date hour-minute-second
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");

// gets the name of the original file and appends the word "copy" followed by the timestamp stored in formattedDate
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Copy " + formattedDate;

// gets the destination folder by their ID. REPLACE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx with your folder's ID that you can get by opening the folder in Google Drive and checking the URL in the browser's address bar
var destination = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

// gets the current Google Sheet file
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())

// makes copy of "file" with "name" at the "destination"
file.makeCopy(name, destination);
  1. Open your Google Drive and create a new folder right where your Google Sheet is. You may name it anything you like.

  2. Open the folder you just created. You will be able to see its ID in the URL displayed in your browser's address bar. We will need this later.

  3. Open your Google Sheet and select menu item "Tools" --> "Script Editor"

  4. Once the script editor opens, copy and paste the code from the 2-MakeCopy.gs file (see file below) into the "Code.gs" file, replacing the code that's already included.

  5. Copy the ID of the folder you created earlier and paste in place of the xxxxxxxxxxxxxxxxxxxxxxx in the code.

  6. Go to menu "File" --> "Save".

  7. Click on the clocked shaped "Trigger" button.

  8. Click on the blue link to add a trigger.

  9. Select the time interval as per your desire.

  10. Click on "Continue" to authorize running the script you just made.

  11. Click on "Allow" to let the script run on your Google Sheet.

  12. After the set interval(which was every minute in my case), you can see the automatic scheduled backup copies of your Google Sheet saved in the folder you created.

How to "Schedule Automatic Backups" of your Google Sheets

This tutorial demonstrates how to use Google Apps Script to:

  • Create copies of the Google Sheet in the desired destination folder automatically at set intervals.

  • Append the time stamp with each backup file's name.

  • Adjust time trigger for backing up every day/hour/minute.