0
我有一个脚本,用于从表单提交(电子表格)创建文档(pdf)。这个脚本运行良好。但是,当一个特定的列值从电子表格传输到文档时,它不会保留数字格式。我累了.setNumberFormat,但它的使用非常模糊(在我看来),我没有成功。例如:在我的脚本(第12行)“var Length_ = row [4];”在电子表格中显示为9,651(或任何其他数字)的值为9,651,但是当它将此值放入我的“新”文档中时,它显示为9651,不带逗号分隔数千个位置。有没有办法保持这种格式,所以它在我的文档中显示为9,651而不是9651?从电子表格数据创建文档时,有没有办法保持单元格格式?
function sendDocument() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = sheet.getLastRow(); // First row of data to process
var numRows = 1; // Number of rows to process // Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1,numRows,sheet.getLastColumn()) // Fetch values for each row in the Range. Needs 4 parameters :start row, start column, number of rows, number of columns
var data = dataRange.getValues(); //returns a 2D array with 0 indexed values : data[0] is row nr 1 and data[0][0] is first cell in this first row
for (i in data) {
var row = data[i];
var ID_ = row[1]; // First column is index 0
var facility_name = row[2]; // Second column is index 1
var facility_type = row[3];
var Length_ = row[4];
var Acres_ = row[5];
var Submission_Date = row[6];
var email_address1 = row[7];
var email_address2 = row[8];
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById("162VlFMAMHad4i2FvuzSL5eAT98-j8SFx6TNXaiBe3DQ")
.makeCopy("POD BBC Prickly Pear Contraction"+' for '+ID_)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys/tags,
copyBody.replaceText('keyID', ID_);
copyBody.replaceText('keyFacilityName', facility_name);
copyBody.replaceText('keyFacilityType', facility_type);
copyBody.replaceText('keyLength', Length_);
copyBody.replaceText('keyAcres', Acres_);
copyBody.replaceText('keySubmissionDate', Submission_Date);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF by using the getAs blob conversion
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "POD BBC Prickly Pear Contraction"+' for '+ID_;
var body = "Document for POD BBC Prickly Pear Contraction"+' for '+ID_+" has been created. Please see attached PDF";
MailApp.sendEmail(email_address1, subject, body, {htmlBody: body, attachments: pdf});
MailApp.sendEmail(email_address2, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(false);
}}
@塞尔INSAS谢谢你的快速响应。我仍然有点不确定在哪里使用你所提供的脚本?由于我只需要一列保留为我的文档格式化,我可以这样做,还是将它格式化所有ceels?另外,我在哪里运行这个功能?意思是,在我检索数据之后,在我的文档副本制作完成之前?或者在复制完成之后,在脚本的.copyBody部分之前/之后的某个位置?对不起,我的经验不足给我留下了问题。本周我刚刚开始编写脚本。谢谢您的帮助。 – user2043853
不用担心;-)使用它就像这样:'var Length_ = toFormatedNumbers(row [4]);'例如,为每个需要格式化的变量重复。 –