Last active
November 28, 2021 20:59
-
-
Save horacioibrahim/d7a8e4146a57e81f812d615d2403e112 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* `googleAppScriptAutoBackup.js` creates copy of files | |
* when is opened. This script check if file already exists | |
* so creates the backup file. | |
*/ | |
var prefixBackupName = "MyFileXYZ"; // we're gonna to use as MyFileXYZ- | |
var folderId = "hashFolderIdFoundInURL"; // backup folder: where file to be placed it | |
var principalFileId = "hashFileIdFoundInURL" // principal file that it'll be copied | |
var workDir = DriveApp.getFolderById(folderId); | |
var myGMT = "GMT-3:00" | |
/** | |
* The event handler triggered when opening the spreadsheet. | |
* @param {Event} e The onOpen event. | |
*/ | |
function onOpen(e) { | |
backupOntime(); | |
} | |
/* `backupOntime` creates backup of the spreadsheet | |
* if not exists file with same name into specific | |
* directory. | |
*/ | |
function backupOntime() { | |
// e.g MyFileXYZ-20211201 | |
let newFileName = prefixBackupName + "-" + mygetDate(); | |
if ( !checkIfFileExists(newFileName) ) { | |
// If false so create backup... | |
DriveApp.getFileById(principalFileId).makeCopy(newFileName, workDir); | |
} | |
} | |
/* `getDate` gets date as YYYYMMDD | |
*/ | |
function mygetDate() { | |
let d = Utilities.formatDate(new Date, myGMT, "yyyyMMdd"); | |
return d | |
} | |
/* `checkIfFileExists` checks if file exists into | |
* specific folder in Driver | |
*/ | |
function checkIfFileExists(filename) { | |
let workFiles = workDir.getFilesByName(filename); | |
while (workFiles.hasNext()) { | |
let workFile = workFiles.next(); | |
let fileId = workFile.getId() || null; | |
if (fileId) { | |
return true | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment