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);  
}}
4

1 回答 1

1

你必须自己处理格式化,有很多方法可以做到这一点,我通常用字符串操作来做,因为我发现这更容易。

这是一个用这种固定模式格式化数字的小函数###,###.##

function toFormatedNumbers(val){
  if(val==''){temp='';return temp}
  var temp = val.toString().replace(/[^\d\.-]/g,'').split('.');
  if(temp[0]==''){temp[0]='0'}
  if(temp.length==1){var result = temp[0]+'.00'}
  else{
  var int = temp[0]
  var dec = temp[1]
      if(dec.length==1){var result=int+'.'+dec+'0'}else{var result=int+'.'+dec}
  }
  var out=result;// result is in the form ######.##
  Logger.log(result.length)
  if(result.length>6){out=result.substring(0,result.indexOf('.')-3)+','+result.substring(result.indexOf('.')-3)}
  return out;// out has the comma separator for thousands
}

您可以使用记录器对其进行测试,它会返回34,567.40

function test(){
Logger.log(toFormatedNumbers(34567.4))
}
于 2013-02-06T08:51:30.140 回答