【问题标题】:Undefined merge field in google apps script谷歌应用脚​​本中未定义的合并字段
【发布时间】:2014-07-23 21:40:03
【问题描述】:

我有一个基于客户在线填写的 Google 表单的 Google 电子表格的 Google Apps 脚本。该脚本由 OnFormSubmit 触发,并基于 Google Doc 模板生成 pdf,并使用 MailApp.sendEmail 通过电子邮件将 pdf 发送给我。

这个脚本直到最近都运行良好。脚本运行成功,但 pdf 输出不正确。现在,脚本中似乎忽略了留空的字段,因此我的 pdf 输出显示了下一个非空白字段的值。啊!

有人知道这里发生了什么吗?

以下是我的脚本示例:

var docTemplate = "1FZL4rVe0LLpvMtIsq_3-pwv5POllIsyYThjfemkbkfg";
var docName = "Travel Details";

function onFormSubmit(e) {
var last = e.values[1];
var first = e.values[2];
var order = e.values[3];
var date = e.values[4];
var gender = e.values[5];
var email = "example@gmail.com";

var copyId = DocsList.getFileById(docTemplate)
.makeCopy(docName+' for '+last + ', ' + first)
.getId();

var copyDoc = DocumentApp.openById(copyId);

var copyBody = copyDoc.getActiveSection();

copyBody.replaceText('keyLast', last);
copyBody.replaceText('keyFirst', first);
copyBody.replaceText('keyOrder', order);
copyBody.replaceText('keyDate', date);
copyBody.replaceText('keyGender', gender);

copyDoc.saveAndClose();

var pdf = DocsList.getFileById(copyId).getAs("application/pdf");

MailApp.sendEmail(email, subject, "", {htmlBody: office_message, attachments: pdf, 
noReply:true});

DocsList.getFileById(copyId).setTrashed(true);
}

问题示例:如果客户将表单上的日期字段留空,则生成的 pdf 中的性别值将放在日期值应位于的位置,并且 pdf 上的性别值显示“未定义”。

有什么想法吗?

【问题讨论】:

    标签: pdf google-apps-script google-spreadsheet-api mailmerge


    【解决方案1】:

    您应该验证所有变量的值。

    if (last === undefined) {
      last = 'No Data!';  //re-assign a different value
    };
    

    所以,如果变量last 以某种方式设置为未定义,则您正在更改它的值。这样,希望 pdf 仍能显示该字段。

    如果最近出现了一些错误,您应该将其报告为错误。如果一切正常,但现在坏了,Google 可能已经改变了一些东西。

    您的代码可能有问题。我不知道。您是否在“查看”菜单和“执行记录”下查看是否有任何错误?您还应该使用 Logger.log 语句:Logger.log('The value of last is: ' + last); 将输出打印到日志。然后您可以检查实际发生的情况。

    【讨论】:

      【解决方案2】:

      我不是出色的编码员,但我一直使用此脚本发送 pdf,如果缺少字段,我从未收到未定义的。通常,如果缺少某些内容,则将 keygender 替换为空白点并且没有错误。在电子表格中,这通常意味着列已更改。它曾经是 timestamp(0)、last(1)、first(2)、order(3)、date(4)、gender(5),现在它们的顺序不同了。

      【讨论】:

        【解决方案3】:

        试试下面的代码吧

        //commons errors - 
        //Triggers are not set
        //spaces after Form questions
        //e.values dont work when fields are not mandatory and left blank
        //e.namedValues dont work for sending emails use e.values[#]
        //place holder keys in template dont match
        //spelling errors
        //Note expect undefined error when de-bugging as values are not defined until form   completed and submitted - run first with a small test form as per below
        
        // Get Template
        //from Google Docs and name it
        var docTemplate = " ";  // *** replace with new templae ID if new Template created***
        var docName     = "Test Script"; //replace with document name
        
        
        // When Form Gets submitted
        function onFormSubmit(e) { 
         //Get information from the form and set as variables 
         //var  variablename = "static entry or form value"
        //Note: var Variablename = e.namedValues["X"]; is taking the value from the spreadsheet    by column name so update if spreadsheet or form questions change
        //Additions to the form will be added to the end of the spreadsheet regardless of their    position in the form
        var Timestamp = e.namedValues["Timestamp"];
        var full_name = e.namedValues["Name"];
        var position = e.namedValues["Position"]
        var contact_email = e.namedValues["Contact Email"];
        var phone_number = e.namedValues["Telephone Number"];
        
        
        // Get document template, copy it as a new doc with Name and email, and save the id
        var copyId = DocsList.getFileById(docTemplate)
                    .makeCopy(full_name+' '+docName+' for ' +contact_email+'  '+Timestamp)//Update or remove Variablename to create full doc Name
                    .getId();
        // Open the temporary document
        var copyDoc = DocumentApp.openById(copyId);
        // Get the documents body section
        var copyBody = copyDoc.getActiveSection();
        
        // Replace place holder keys <<namedValues>> in template  
        //copyBody.replaceText('<<X>>', Variablename); Variables from above
        //***Update if template is changed***
        copyBody.replaceText('<<Timestamp>>', Timestamp);
        copyBody.replaceText('<<Name>>', full_name);
        copyBody.replaceText('<<Position>>', position);
        copyBody.replaceText('<<Contact Email>>', contact_email);
        copyBody.replaceText('<<Telephone Number>>', phone_number);
        
        // 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"); 
        
        {      
        // Add the data fields to the message 
        
        var s = SpreadsheetApp.getActiveSheet();
        var columns = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];    
        var message = " ";    
        
        // Only include form fields that are not blank
        for ( var keys in columns ) {
          var key = columns[keys];
          if ( e.namedValues[key] && (e.namedValues[key] != "") ) {
            message += key+ ' : '+ e.namedValues[key] + "<br>"; 
          }
        }}
        
        // Attach PDF and send the email
        //***Change the "To" email address when to form goes live***
        var to = "youremail@gmail.com";
        var senders_name = e.values[1]
        var contact_email = e.values[3]
        var subject = "Test";
        var htmlbody = "text goes here"+
           "<br> <br>"+message+
           "<br> <br>Submitted By:"+
             "<br> <br>"+full_name+
           "<br>"+position+
             "<br>"+contact_email+
               "<br>"+phone_number+
               "<br> <br>Generated by Hansmoleman for Compu-Global-Hyper-Mega-Net";
        
         MailApp.sendEmail({
         name: senders_name,
         to: to,
         cc: contact_email,
         replyTo: contact_email,
         subject: subject,
         htmlBody: htmlbody,
         attachments: pdf, 
         }); 
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多