我有一个脚本,用于从表单提交(电子表格)创建文档(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);
}}