0

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?

7
  • If you have been running that for very long then I would recommend that you go into your current project triggers and delete some of the onEdit() triggers and then only create a trigger if one doesn't already exist. Commented Mar 16, 2020 at 20:16
  • I have not been running this for any amount of time. I am putting this together starting today. I have not been running this at all. I did run it from my editor shell ,and it does indeed copy and save a file to my drive. Commented Mar 16, 2020 at 20:28
  • Well, you should always check to see if you already have a trigger with that name before creating another. Commented Mar 16, 2020 at 20:35
  • Understood. This was the first script I've ever created so I can't imagine I have other triggers Commented Mar 17, 2020 at 1:20
  • Thanks for your help! I'm getting an error when this tries to run on change: Your script, Daily Performance Copy, has recently failed to finish successfully. A summary of the failure(s) is shown below. To configure the triggers for this script, or change your setting for receiving future failure notifications, click here. The script is used by the document Performance. Start Function Error Message Trigger End 3/18/20 9:56 AM DailyPerformanceCopy Authorization is required to perform that action. change 3/18/20 9:56 AM Commented Mar 18, 2020 at 16:31

1 Answer 1

1
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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help! I dont have edit access to the original file so I will see tomorrow after it updates. Thank you again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.