如何使用脚本从谷歌电子表格生成(PDF)时控制日期格式?

时间:2016-02-25 19:24:55

标签: performance date google-apps-script

我正在寻求澄清所提问题here。 Serge insas建议“当您在替换循环中将它们插入文档时,应该使用格式化字符串转换日期。使用的函数是Utilities.formatDate。”

我想做Serge推荐的,但我在实施它时遇到了一些困难。该脚本运行良好,但我可以节省大量时间,因为在将日期粘贴到电子表格之前不必将日期转换为文本字符串。在我的PDF上编码我想要的日期格式更有意义(d mmmm yyyy)。非常感谢任何帮助。

//Creates the custom menu in the spreadsheet "Run Script"
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Scripts')
      .addItem('Create Certs', 'menuItem1')
//      .addItem('Change Cell Format', 'menuItem2')
//      .addItem('Reset Cell Format', 'menuItem3')
      .addToUi();
}
//Runs the menuItem 1 operation (Create Certs)
function menuItem1() {

//Defines the start row and calculates the number of rows to be processed
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = Browser.inputBox("Enter Start Row");
  var endRow = Browser.inputBox("Enter End Row");
  var numRows = (endRow - startRow) + 1;
  var dataRange = sheet.getRange(startRow, 1, numRows, 7);

 var counter =0;   
 var data = dataRange.getValues();   
 var templateDoc = DriveApp.getFileById("1baxSUxfSdzcVheR3Y2qgieWeSAqNybPfWct1913uRIc");   
 var templateCopy = templateDoc.makeCopy();
 var templateCopyId = templateCopy.getId();

//Begin Tew's code
 var dateOld;   
 var courseOld;   
 var fullNameOld;   
  for (var i = 0; i < data.length; ++i) {
        var doc = DocumentApp.openById(templateCopyId);
        var body = doc.getActiveSection();
        var row = data[i];
        var date = row[0]; //like this for testing to get a different value on each pdf
        var nic = row[1];
        var course = row[2];
        var lastname = row[3];
        var firstname = row[4];
        var middle = row[5]
        var email = row[6];
        //    var docname = lastname+" "+nic+" PME Cert"; //not used in this version
        var subjectTxt = "NWC "+ course +" Online PME Course Certificate";
        var fullBody = "PME COURSE COMPLETION CERTIFICATE" + "\n\n";
        fullBody += "Your " + course + " course completion certificate is attached." + "\n\n";
        fullBody += "NOTES:" + "\n";
        fullBody += "1. NWC does NOT mail hardcopy certificates." + "\n";
        fullBody += "2. NWC does not award certificates for the SNCO JPME course." + "\n\n";
        fullBody += "Regards," + "\n\n";
        fullBody += "Professor Steve Pierce" + "\n";
        fullBody += "U.S. Naval War College "+ "\n";
        fullBody += "Online PME Program Team" + "\n\n";
        fullBody += "Learn more about NWC's Online PME Program at the link below:" + "\n";
        fullBody += "http://www.usnwc.edu/Academics/College-of-Distance-Education/PME-(1).aspx" + "\n";

        var row = data[i];
        var fullName = firstname+' '+middle+''+lastname
        var fdate = Utilities.formatDate(new Date(date), "d mmmm yyyy");
        if(counter ==0){
          body.replaceText('fullName',fullName);  
          body.replaceText('course', course);
          body.replaceText('date', fdate); 
        }
        else {   
          body.replaceText(fullNameOld,fullName);
          body.replaceText(courseOld, course);
          body.replaceText(dateOld, fdate); 
        }

        dateOld = fdate; 
        courseOld = course;
        fullNameOld = fullName

        counter ++

          doc.saveAndClose()
          var attachment = doc.getAs("application/pdf")

          MailApp.sendEmail(email, subjectTxt, fullBody, {attachments: attachment});   
 } 
          DriveApp.getFileById(templateCopyId).setTrashed(true);
}

1 个答案:

答案 0 :(得分:0)

第57行存在两个问题。

Utilities.formatDate需要一个日期,一个不存在的时区,然后一种格式。
https://msdn.microsoft.com/en-us/library/bb314749.aspx

另一个问题是格式字符串本身。

mmmm是一种非常长的分钟格式。我假设你想要长的月MMMM

所以要使用的命令是

var fdate = Utilities.formatDate(new Date(date), "UTC", "d MMMM yyyy");

今天将返回26 February 2016 如果你想要一个不同的格式,试试看看这个方法借用格式化选项的Java的See here for the documentation