pdf转换不一致-Google脚本(doc.getAs())

时间:2018-07-25 12:00:12

标签: javascript file pdf documentation implicit-conversion

我有一个脚本,可以生成个人证书并通过电子邮件发送它们

  1. 制作一份Google文档的副本-可以使用证书模板的地方。
  2. 更改一些浮动字段。 (受训者的姓名等)
  3. 将其转换为pdf,然后通过电子邮件发送。

在证书的模板中,我有一些图像(邮票等)。发送pdf时,有些图像消失了,因此模板转换未正确完成。 我以为是这个问题:
var docblob = doc.getAs('application / pdf'); 但我也用过 var docblob1 = doc.getAs(MimeType.PDF); 我真的不知道为什么无法正确解析pdf。

任何帮助都会很好。


function ModelCertificates() {

  var myvector=CoreData[1];
  var pdf_name= myvector[1];
  var template_link=myvector[2];
  var subject = myvector[4]
  var email_body = myvector[5];

  // grab the data from the spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Role Model');

  // count completed certificates from final column
  var completedCount = sheet.getRange("E1:E").getValues().filter(String).length;

  // get pending rows of data
  var pendingData = sheet.getRange(completedCount + 1, 1, sheet.getLastRow() - completedCount, 5).getValues();

  pendingData.forEach(function(row, i) {
    try {
      // get the student variables
      //var teachable_date = row[0];
      var name = row[0];
      var field = row[1];
      var datee = row[2];
      var email = row[3]; 



      // create certificate
      var cert = createCertificateModel(name,field,datee,pdf_name,template_link); 
      var doc = cert[0]; 

      // turn certificate into pdf
      var docblob = doc.getAs('application/pdf');
      var docblob1 = doc.getAs(MimeType.PDF);


      docblob1.setName(doc.getName() + ".pdf");
      var file = DriveApp.createFile(docblob1);

      // email certificate
      emailCertificate(name, email, file, email_body,subject);

      // trash the doc and the file
      var doc_file = DriveApp.getFileById(cert[1]); 
      doc_file.setTrashed(true);
      file.setTrashed(true);

      // mark this row as complete in the dataset
      sheet.getRange(completedCount + 1 + i,5).setValue("Email sent");
    }
    catch(e) {
      Logger.log(e.message);
    }
  });
}


function createCertificateModel(name,field,formatted_date,pdf_name,template_link) {

  // extract the Google Doc template ID from the URL
  var rx = /\/d\/(.*)\/edit/i;
  var templateId =template_link.match(rx)[1];

  // make copy of the template and assign to variable doc
  var certificate = DriveApp.getFileById(templateId).makeCopy(pdf_name);
  var certificateID = certificate.getId();
  var doc = DocumentApp.openById(certificateID);

  // update the certificate
  var doc = updateCertificatemodel(doc, name,field,formatted_date);

  return [doc, certificateID];
}
/**
* updateCertificate function to update the certificate with student data
* @param {doc} doc - the certificate being created
* @param {name} string - student name
* @param {course} string - course name
* @param {date} string - course completion date
* @returns {doc} file - the updated certificate
*/



function updateCertificatemodel(doc, name,field,datee) {

  var body = doc.getBody();

  // find the {{variables}} in the google doc and replace with the actuals  
  body.replaceText("{{student_name}}", name);
  body.replaceText("{{field_name}}", field);
  body.replaceText("{{date}}", datee);

  doc.saveAndClose();

  return doc;
}
function emailCertificate(name, email, file, email_body,subject) {
  var myname = getOwnName();



  var htmlBody = 'Hello ' + name + ',<br><br>' +
                        email_body + '<br><br>' +
                       'Sincerely,<br>' +
                        myname;

  if (email) {
    GmailApp.sendEmail(
      email, 
      subject,
      '',
      {
        htmlBody: htmlBody,
        attachments: file,
        name: subject
      }
    );
  }
}

0 个答案:

没有答案