【问题标题】:Employee of the week award script modification not working本周员工奖脚本修改不起作用
【发布时间】:2025-12-13 06:40:01
【问题描述】:

我已经修改了“本周最佳员工奖”,但无法让它在提交后将准确的数据提取到我的文档中。

我的目标是:

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

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

如果可能,请提供帮助。

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    你的剧本很...。我该怎么说...近似?

    我没有对其进行测试,也看不到您的电子表格,因为您忘记将它们公开显示,但我可以建议脚本的第一部分,并在 cmets 中进行一些更正和解释。

    希望它能帮助您实现它。

    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 
    

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

    【讨论】:

    • 感谢您的回复。我现在已经分享了链接。我很抱歉没有早点这样做。我会尝试你的建议,让你知道我是如何公平的。如果您现在想查看链接,将不胜感激。
    • 刚刚看到您的消息(欧洲时间),很高兴它有所帮助! - 请考虑接受答案
    【解决方案2】:

    这是我的新脚本,似乎运行良好。感谢您的帮助。

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

    【讨论】:

    • 如果你想删除文档,你应该在代码的最后部分使用 setTrashed(true)...
    • 我想暂时保留文档。另一方面,我想打开多个文件并按特定顺序将它们合并/组合成一个 PDF 文档。关于如何将其添加到脚本中的任何建议?
    • 看看这个帖子merging docs without blanks