如何在使用脚本从Google电子表格生成Google文档(PDF)时控制日期格式? [英] How can control the date format when generating a google docs (PDF) from a google spreadsheet with script?

查看:141
本文介绍了如何在使用脚本从Google电子表格生成Google文档(PDF)时控制日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google文档的脚本和模板从一张Google(使用Google表单填充)生成PDF文件。
但是这样做的结果是,我在谷歌表格中获得日期格式为dd / mm / yyyy格式,而在最终的PDF格式中,就像这样= Sat Feb 27 2016 00:00:00 GMT + 0100(CET)



脚本的核心(对于Google表格的每一行)

  var copyFile = DriveApp.getFileById(TEMPLATE_ID).makeCopy(FileName); 
var copyId = copyFile.getId();
var copyDoc = DocumentApp.openById(copyId);
var copyBody = copyDoc.getActiveSection(); $;

for(; columnIndex< headerRow [0] .length; columnIndex ++){
copyBody.replaceText('%'+ headerRow [0] [columnIndex] +'%',RowData [ i] [columnIndex])
}

columnIndex = 0;

//创建PDF文件,根据需要重命名并删除文档副本

copyDoc.saveAndClose();

var dir = DriveApp.getFoldersByName(PDF FOLDER)。next();
var newFile = dir.createFile(copyFile.getAs('application / pdf'));
$ b $ if if(PDF_FILE_NAME!==''){
newFile.setName(FileName)
copyFile.setTrashed(true)
}

为了在doc / PDF中获得正确的格式,我可以在哪里(以及如何)控制RowData [i]文件? (在模板中,有效值的替换是用%日期名称%指令进行的)

感谢
Vanni



解决方案

Utilities.formatDate 并且这里记录



使用它非常简单,例如:

  var formattedDate = Utilities.formatDate(新日期(),GMT,yyyy-MM-dd); 

请注意,不要使用GMT或任何GMT + XX值,最好使用 Session.getScriptTimeZone(),它会根据你的时区自动给出正确的值(即使在夏令时的情况下)。



编辑:

您也可以尝试 getDisplayValue(),它会以单元格中显示内容的字符串表示形式

I generate a PDF file from a sheet of Google (that is populated with Google form) using a script and a template with Google Docs. But doing this I obtain that in google sheets date are in dd/mm/yyyy format while in the final PDF it's like this = Sat Feb 27 2016 00:00:00 GMT+0100 (CET)

The "core" of the script is (for each row of the google sheet)

var copyFile = DriveApp.getFileById(TEMPLATE_ID).makeCopy(FileName);
var copyId = copyFile.getId();
var copyDoc = DocumentApp.openById(copyId);
var copyBody = copyDoc.getActiveSection();

for (;columnIndex < headerRow[0].length; columnIndex++) {
    copyBody.replaceText('%' + headerRow[0][columnIndex] + '%', RowData[i][columnIndex])
}

columnIndex = 0;

// Create the PDF file, rename it if required and delete the doc copy

copyDoc.saveAndClose();

var dir = DriveApp.getFoldersByName("PDF FOLDER").next();  
var newFile = dir.createFile(copyFile.getAs('application/pdf'));  

if (PDF_FILE_NAME !== '') {
    newFile.setName(FileName)
    copyFile.setTrashed(true)
}  

Where (and how) can I control the RowData[i] that are date type in order to obtain the correct format in doc/PDF file? (In the template the substitution with effective value is made with the %date name% instruction)

Thanks Vanni

解决方案

You should convert your dates in formatted strings when you insert them in your doc in the replace loop.

The function to use is Utilities.formatDate and is documented here.

It is quite straightforward to use, example :

var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");

Note, instead of using GMT or whatever GMT+XX value, use preferably the Session.getScriptTimeZone() that will automatically give the right value according to your timezone (even in case of daylight savings).

EDIT :

you could try getDisplayValue() too , it returns the value as a string representation of what is shown in a cell.

这篇关于如何在使用脚本从Google电子表格生成Google文档(PDF)时控制日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆