612Tech
612Tech

Reputation: 1

creating basic Google Script to Copy SS and Save to a particular folder on my drive

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);
}

  1. I would like the copied file to only be saved as Performance + The current Month, Date, Year. (Performance March 16 2020. Tomorrows copy to be saved as Performance March 17 2020, etc)

Its currently being saved as: Performance Mon Mar 16 2020 14:45:09 GMT-0400 (Eastern Daylight Time)

  1. Its currently being saved to the root of my drive. I'd like it to be saved to the folder I created called "Performance"

  2. Im not sure if it will execute tomorrow after the file gets updated. Im assuming so?

Upvotes: 0

Views: 144

Answers (1)

Cooper
Cooper

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

Related Questions