有没有办法在从电子表格数据创建文档时保持单元格格式?

时间:2013-02-06 08:05:01

标签: google-apps-script

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

1 个答案:

答案 0 :(得分: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))
}