-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];

...继续上面的脚本...

如果可能,请提供帮助。

4

2 回答 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 

既然您说文档创建工作正常,那么这些更改就有可能实现;-)

于 2013-02-05T19:39:51.557 回答
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);  
}}
于 2013-02-06T03:28:38.923 回答