一段时间以来,我一直在努力寻找答案...
我有一个Google表单(在此示例中,我要说这是车辆的检查清单)。因此,我填写了表格,答案转到工作表,工作表中的应用程序脚本制作了doc模板的副本,替换了关键字,然后保存并通过电子邮件发送pdf。绝对可以。
我正在尝试通过表单添加图像上传作为一些答案。图像上传驱动良好,但我不知道要在应用脚本中放入什么内容以使图像嵌入到doc中,以便图像通过电子邮件发送到pdf上。现在,它仅提供指向该图像的驱动器链接。我希望我有道理。
// Get template from Google Docs and name it
var docTemplate = "doc ID";
var docName = "Vehicle check with images";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var email_address = "myemailaddress@here.com";
var vehicle_vrn = e.values[1];
var front_desc = e.values[2];
var front_image = e.values[3];
var rear_desc = e.values[4];
var rear_image = e.values[5];
var driver_desc = e.values[6];
var driver_image = e.values[7];
var passenger_desc = e.values[8];
var passenger_image = e.values[9];
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+vehicle_vrn)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('keyVrn', vehicle_vrn);
copyBody.replaceText('keyFrontdesc', front_desc);
copyBody.replaceText('keyFrontimage', front_image);
copyBody.replaceText('keyReardesc', rear_desc);
copyBody.replaceText('keyRearimage', rear_image);
copyBody.replaceText('keyDriversdesc', driver_desc);
copyBody.replaceText('keyDriversimage', driver_image);
copyBody.replaceText('keyPassdesc', passenger_desc);
copyBody.replaceText('keyPassimage', passenger_image);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "V Check with images concept";
var body = "Please find attached the completed vehicle check with images for " + vehicle_vrn + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}
答案 0 :(得分:0)
我对您的情况了解如下。
https://drive.google.com/open?id=### fileId ###
之类的URL检索为事件对象。
keyVrn
,keyFrontdesc
,keyFrontimage
,keyReardesc
,keyRearimage
,keyDriversdesc
,keyDriversimage
,{{1 }}和keyPassdesc
。而且每个值都只有一个。keyPassimage
,front_image
,rear_image
和driver_image
是上传的文件。您要从passenger_image
,keyFrontimage
,keyRearimage
和keyDriversimage
替换为它们。如果我的理解是正确的,那么该修改如何?我认为您的情况有几个答案。因此,请将此视为其中之一。
keyPassimage
替换文本值,但是不能使用文本将其替换为图像。因此,我为此使用了以下流程。
反映以上几点的脚本如下。
replaceText()
如果我误解了您的问题,请告诉我。我想修改它。
要调整插入图像的大小时,请进行以下修改。在此修改中,图像被转换为300像素。请根据您的情况进行修改。
// Get template from Google Docs and name it
var docTemplate = "doc ID";
var docName = "Vehicle check with images";
function onFormSubmit(e) {
var replaceTextToImage = function(body, searchText, fileId) {
var blob = DriveApp.getFileById(fileId).getBlob();
var r = body.findText(searchText).getElement();
r.asText().setText("");
r.getParent().asParagraph().insertInlineImage(0, blob);
}
//Get information from form and set as variables
var email_address = "myemailaddress@here.com";
var vehicle_vrn = e.values[1];
var front_desc = e.values[2];
var front_image = e.values[3].split("=")[1];
var rear_desc = e.values[4];
var rear_image = e.values[5].split("=")[1];
var driver_desc = e.values[6];
var driver_image = e.values[7].split("=")[1];
var passenger_desc = e.values[8];
var passenger_image = e.values[9].split("=")[1];
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+vehicle_vrn)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getBody();
copyBody.replaceText('keyVrn', vehicle_vrn);
copyBody.replaceText('keyFrontdesc', front_desc);
replaceTextToImage(copyBody, 'keyFrontimage', front_image);
copyBody.replaceText('keyReardesc', rear_desc);
replaceTextToImage(copyBody, 'keyRearimage', rear_image);
copyBody.replaceText('keyDriversdesc', driver_desc);
replaceTextToImage(copyBody, 'keyDriversimage', driver_image);
copyBody.replaceText('keyPassdesc', passenger_desc);
replaceTextToImage(copyBody, 'keyPassimage', passenger_image);
copyDoc.saveAndClose();
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
var subject = "sample attachment file";
var body = "sample text: " + vehicle_vrn + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
DriveApp.getFileById(copyId).setTrashed(true);
}
var replaceTextToImage = function(body, searchText, fileId) {
var blob = DriveApp.getFileById(fileId).getBlob();
var r = body.findText(searchText).getElement();
r.asText().setText("");
r.getParent().asParagraph().insertInlineImage(0, blob);
}