复制时将日期添加到文件名

时间:2015-01-06 03:27:06

标签: google-apps-script

我创建了一个脚本来创建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>');
}

如何在运行时将今天的日期dd / mm / yy添加到文件名中?

2 个答案:

答案 0 :(得分:2)

获取旧文件名

我使用 DriveApp 代替 DocsList

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

为新文件名创建变量。

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;

制作副本:

theFileReference.makeCopy(newFileName);

答案 1 :(得分:0)

var newUrl = DocsList.getFileById(fileId).makeCopy('Filename').getUrl();

文件名在上面的'Filename'字符串中设置。

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