2013-02-05 32 views
-2

我已經修改了「周獎的員工」,不能讓它準確的數據在從提交拉我的文件。員工周最佳劇本修改的工作不

我的目標是:

1)填寫表格所需的細節

2)有腳本來收集這些細節,並把它們插入到我的文檔模板在正確的密鑰持有者

3)在標題變化的PDF創建文檔的一個「新」的副本

4)通過電子郵件將是巨大的,但不是必需的,只要它保存新文檔

我可以讓它創建一個新文檔並運行腳本,但它似乎並沒有從表單或電子表格中提取正確的(或任何)數據。它一直顯示'未定義',它應該顯示輸入到表單中的值。請幫我看看我的錯誤。我花了數小時試圖弄清楚並重新編寫代碼。提前致謝!

這裏是鏈接到我的文檔和電子表格,表單和腳本我使用。

https://docs.google.com/document/d/1A34uyNyMzp3o8XBzmIGqqfvCH6ocpQWN4HaZ5f4AiDk/edit

https://docs.google.com/spreadsheet/ccc?key=0AkKN7xCpxU54dENoZ0RoSnF1QXhQNnAyX3ZiNmVwRGc#gid=0

這裏是我當前的腳本:

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,2) // Fetch values for each row in the Range. 
var data = dataRange.getValues(); 
for (i in data) {  
var row = data[i];  
var ID = row[1]; // First column  
var facility_name = row[2];  // Second column 
// Get document template, copy it as a new temp doc, and save the Doc’s id 
var copyId = DocsList.getFileById("1A34uyNyMzp3o8XBzmIGqqfvCH6ocpQWN4HaZ5f4AiDk") 
      .makeCopy("Copy of Mail Merge Doc template"+' for '+facility_name) 
      .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('keyFacilityName', facility_name); 
copyBody.replaceText('keyID', ID); 
var todaysDate = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy"); 
copyBody.replaceText('keyTodaysDate', todaysDate); 
// 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"); 
// Delete temp file 
DocsList.getFileById(copyId).setTrashed(false); 
}} 

我也曾嘗試以下兩個選項在我的劇本的開始「拉」從表單中的數據創建'新'文檔沒有成功...

// Global variables 
docTemplate = 「1A34uyNyMzp3o8XBzmIGqqfvCH6ocpQWN4HaZ5f4AiDk」; 
docName = 「Copy of Mail Merge Doc template」; 

function sendDocument() { 
// Full name and email address values come from the spreadsheet form 
    var ID = from-spreadsheet-form 
    var facility_name = from-spreadsheet-form 

...繼續腳本如上...

// Global variables 
docTemplate = 「1A34uyNyMzp3o8XBzmIGqqfvCH6ocpQWN4HaZ5f4AiDk」; 
docName = 「Copy of Mail Merge Doc template」; 

function onFormSubmit(e) { 
// Full name and email address values come from the spreadsheet form 
    var ID = e.values[1]; 
    var facility_name = e.values[2]; 

...繼續上面的腳本...

如果可能的話請幫忙。

回答

0

你的腳本是相當....怎麼說呢......近似?

我沒有測試它,看不到你的電子表格,因爲你忘了讓他們公開可見的,但我可以建議與意見一些更正和解釋腳本的第一部分。

希望它能幫助你,使工作。

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[0]; // First column is index 0 
var facility_name = row[1];  // Second column is index 1 

既然你說的文檔創建正在有機會的話,這些變化可以使它;-)

+0

謝謝你的答覆。我現在分享了這些鏈接。我很抱歉沒有這麼做。我會嘗試你的建議,並讓你知道我的公平。如果你想看看現在的鏈接,將不勝感激。 – user2043853

+0

剛剛看到您的留言(歐洲時間),很高興幫助! - 請考慮接受答案 –

0

這是我的新的腳本,這似乎是偉大的工作。謝謝您的幫助。

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("1PsSwL9-w3R51C3RcWCuVwHWKYSxw9btJDVRuGgipDAI") 
      .makeCopy("POD BBC Prickly Pear Contraction POD template"+' 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); 
}} 
+0

如果你想刪除文檔,你應該在代碼的最後部分使用setTrashed(true)... –

+0

我想現在保留文檔。另一方面,我想打開多個文件,並按特定的順序合併/組合成一個PDF文檔。有關如何將此添加到我的腳本的任何建議? – user2043853

+0

看看這篇文章[合併文檔沒有空白](http://stackoverflow.com/questions/10939031/how-to-delete-blank-pages-in-a-multipage-document) –