【发布时间】:2021-09-26 08:07:48
【问题描述】:
我有谷歌表单,其响应如下表(电子表格)所示。
此脚本无法生成二维码。标题中的二维码公式,因此它会自动在 B 列的电子表格中生成二维码,其中包含 D 列的数据。我不知道如何解决。
var docTemplate = "doc ID";
var docName = "Vehicle check with images";
function onFormSubmit(e) {
var replaceTextToImage = function(body, searchText, fileId) {
var width = 300; // Please set this.
var blob = DriveApp.getFileById(fileId).getBlob();
var r = body.findText(searchText).getElement();
r.asText().setText("");
var img = r.getParent().asParagraph().insertInlineImage(0, blob);
var w = img.getWidth();
var h = img.getHeight();
img.setWidth(width);
img.setHeight(width * h / w);
}
//Get information from form and set as variables
var email_address = "myemailaddress@here.com";
var qrCode = e.values[1].split("=")[3];//I want to try
var empName = e.values[2];
var empId = e.values[3];
var photo = e.values[4].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 '+empName)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getBody();
replaceTextToImage(copyBody, 'qrcode', qrCode);//problem could not be generated
copyBody.replaceText('name', empName);
copyBody.replaceText('id', empId);
replaceTextToImage(copyBody, 'photo', photo);
copyDoc.saveAndClose();
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
var subject = "sample attachment file";
var body = "sample text: " + empName + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
DriveApp.getFileById(copyId).setTrashed(true);
}
| Timestamp | ={"QR CODE";ARRAYFORMULA(IF(D2:D<>"";IMAGE("https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&D2:D);))} | Name | Id | Photo |
|---|---|---|---|---|
| 10/07/2021 8:35:24 | QR CODE | Robert | 1234 | https://drive.google.com/open?id=14SAL5EK8tqOESgZyAayScbTqhSEE89Wa |
【问题讨论】:
-
您看到二维码或电子表格了吗?也许用更简单的变体(对于“B2”单元格)替换它是有意义的:
=IMAGE("https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&D2) -
是的。我使用数组公式在电子表格中看到 QR 码。当我使用
=IMAGE("https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&D2)时,我不得不把它拖下来。不过,在有人提交谷歌表单后,我需要 pdf 文件作为附件。 -
可能原因在这里:
var blob = DriveApp.getFileById(fileId).getBlob();你可以这样插入文件。例如,来自云端硬盘的照片。但我怀疑qrCode = e.values[3].split("=")[1];不会返回文件的 fileID,因为您在 Drive 上根本没有这样的文件。我不知道它到底返回了什么。网址?文本?可能您需要通过UrlFetchApp.fetch(url).getBlob()以另一种方式获取二维码的blob。看我的例子。 -
当您将变量
qrCode发送到函数replaceTextToImage()时,您能否显示它究竟包含什么?是你二维码的fileID吗?由于该函数需要文件 id。
标签: google-apps-script pdf-generation spreadsheet google-forms