Skip to content

Instantly share code, notes, and snippets.

@n0531m
Created December 14, 2016 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n0531m/fcc6adcf8e07920aedab1662fe4af3a4 to your computer and use it in GitHub Desktop.
Save n0531m/fcc6adcf8e07920aedab1662fe4af3a4 to your computer and use it in GitHub Desktop.
AppsScript to copy doc if updated
//This script should be stored in Drive as a AppScript file and configured to run at intervals
//In my case, run it every two hours and copied the doc if there was an update
//ID of original document
var srcDocId="xxxxxx";
//ID of desitination Folder
var targetFolderId= "xxxxxx";
function copyDoc() {
copyDocIfUpdated(srcDocId,targetFolderId,2);
}
/**
* src : file to poll
* dest : destination folder to create a copy
* intervalhours : how you would define fresh.
*/
function copyDocIfUpdated(src,dest,intervalhours){
var srcFile=DriveApp.getFileById(src);
var lastupdated=srcFile.getLastUpdated();
var now= new Date();
if(((now.getTime()-lastupdated.getTime())/(1000*3600))<intervalhours){
var targetFolder=DriveApp.getFolderById(dest);
//Logger.log(file.getName());
var datestr=Utilities.formatDate(new Date(), "Asia/Singapore", "YYYY/MM/dd HH:mm");
var newFile=srcFile.makeCopy("[copy "+datestr+"] " + srcFile.getName(), targetFolder);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment