Skip to content

Instantly share code, notes, and snippets.

@fmarais
Last active May 12, 2022 13:56
Show Gist options
  • Save fmarais/a962a8b54ce3f53f0ed57100112b453c to your computer and use it in GitHub Desktop.
Save fmarais/a962a8b54ce3f53f0ed57100112b453c to your computer and use it in GitHub Desktop.
PUBLIC_google_drive_sheets_backup_files
// Sheet > Extensions > Apps Script
// Add Script
// Add trigger for script
// ------------------------------
// Backup script
function archiveCopy() {
var file = DriveApp.getFileById("original_file_id_to_backup");
var destination = DriveApp.getFolderById("backup_folder_name");
var timeZone = Session.getScriptTimeZone();
var formattedDate = Utilities.formatDate(new Date(),timeZone,"dd"); // 1 month backup, one per day
var name = SpreadsheetApp.getActiveSpreadsheet().getName()+"_"+formattedDate;
// remove old backup
var allFiles = destination.getFilesByName(name);
while (allFiles.hasNext()) {
var thisFile = allFiles.next();
thisFile.setTrashed(true);
};
// make new backup
file.makeCopy(name,destination);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment