2

I created a script to create a copy of a Gdoc:

// Script-as-app template.
function doGet(e) {
  //file has to be at least readable by the person running the script
  var fileId = e.parameters.fileId;  
  if(!fileId){
    //have a default fileId for testing. 
    fileId = '1TzLleN93A0ibMhSQ3eenZ1dLC3RLB7T9XoIaRw8-pfk'; 
  }
  var newUrl = DocsList.getFileById(fileId).makeCopy('Filename').getUrl(); 
  return HtmlService.createHtmlOutput('<h1><a href="'+newUrl+'">Open Document</a></h1>');
}

How would I go about adding today's date dd/mm/yy to the filename when running?

screampuff
  • 217
  • 3
  • 11

2 Answers2

3

Get the old file name

I'd use DriveApp instead of DocsList.

var theFileReference = DriveApp.getFileById(fileId);
var oldFileName = theFileReference.getName();

Create a variable for the new file name.

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();

var theDate = curr_date + "-" + curr_month + "-" + curr_year);

var newFileName = oldFileName + theDate;

Make the copy:

theFileReference.makeCopy(newFileName);
jlo
  • 2,157
  • 2
  • 17
  • 23
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
0
var newUrl = DocsList.getFileById(fileId).makeCopy('Filename').getUrl();

The file name is set in the 'Filename' string above.

Turning today's date into a string has been covered heaps of times.

Community
  • 1
  • 1
AshClarke
  • 2,990
  • 5
  • 21
  • 27