Reputation: 1
I have a basic script that I found works pretty good for my needs. The file I want to copy gets updated once a day. The person that updates it just overwrites the file every day so I'm trying to store a historical copy of the file, into a folder on my drive. The script below does copy the file and creates the file for me. However I am trying to polish up a few things.
function DailyPerformanceCopy() {
ScriptApp.newTrigger('DailyPerformanceTrigger')
.forSpreadsheet('ENTERSPREADSHEETIDHERE')
.onEdit()
.create();
var date = new Date();
Logger.log(Utilities.formatDate(date,'America/Chicago', 'MMMM dd, yyyy'));
var ss = SpreadsheetApp.openById("ENTERSPREADSHEETIDHERE");
//Make a copy of the template file
var documentId = DriveApp.getFileById('ENTERSPREADSHEETIDHERE').makeCopy().getId();
//Rename the copied file
DriveApp.getFileById(documentId).setName('Performance ' + date);
}
Its currently being saved as: Performance Mon Mar 16 2020 14:45:09 GMT-0400 (Eastern Daylight Time)
Its currently being saved to the root of my drive. I'd like it to be saved to the folder I created called "Performance"
Im not sure if it will execute tomorrow after the file gets updated. Im assuming so?
Upvotes: 0
Views: 144
Reputation: 64140
function DailyPerformanceCopy() {
const ss=SpreadsheetApp.openById("**************ENTERSPREADSHEETIDHERE******************");
const dfldr=DriveApp.getFolderById('************Enter Folder Id***************')
if(notTrigger('DailyPerformanceTrigger')) {ScriptApp.newTrigger('DailyPerformanceTrigger').forSpreadsheet(ss.getId()).onEdit().create();}
const ts=Utilities.formatDate(new Date(),'America/Chicago', 'MMMM dd yyyy');
const file=DriveApp.getFileById(ss.getId());
const name=Utilities.formatString('Performance %s', ts);
file.makeCopy(name, dfldr);
}
function notTrigger(funcname) {
const tA=ScriptApp.getProjectTriggers();
for(var i=0;i<tA.length;i++) {if(tA[i].getHandlerFunction()=='funcname') {return true;}}
return false;
}
Upvotes: 1