2013-02-06 26 views
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); 
}} 

回答

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)) 
} 
+0

@塞爾INSAS謝謝你的快速響應。我仍然有點不確定在哪裏使用你所提供的腳本?由於我只需要一列保留爲我的文檔格式化,我可以這樣做,還是將它格式化所有ceels?另外,我在哪裏運行這個功能?意思是,在我檢索數據之後,在我的文檔副本製作完成之前?或者在複製完成之後,在腳本的.copyBody部分之前/之後的某個位置?對不起,我的經驗不足給我留下了問題。本週我剛剛開始編寫腳本。謝謝您的幫助。 – user2043853

+0

不用擔心;-)使用它就像這樣:'var Length_ = toFormatedNumbers(row [4]);'例如,爲每個需要格式化的變量重複。 –

相關問題