Apps 脚本 - Google 表单到电子邮件 PDF - 需要复选框问题才能生成为要点
Apps Script - Google Form to Email PDF - Need Checkbox questions to generate as Bullet Points
提交 Google 表单时,它会在 Google 文档模板中找到要替换的项目,然后通过电子邮件发送给我。
我无法将表单复选框响应作为文档中的要点。它目前将列出所有已检查的选项,但它们以逗号分隔,而不是项目符号。
如有任何帮助,我们将不胜感激!
function onFormSubmit(e) {
//open the template document by ID
//you can find the ID in the link of the document
var templateDoc = DriveApp.getFileById('1Dwbjh1Jqerg_WKvqdERyCfnDnWY2yjNArGY_f38ioAs');
//create a copy of the template, we don't wanna mess up the template doc
var newTempFile = templateDoc.makeCopy();
//open the new template document for editing
var openDoc = DocumentApp.openById(newTempFile.getId());
var body = openDoc.getBody();
//get the responses triggered by On Form Submit
var items = e.response.getItemResponses();
//find the text in the template doc and replace it with the Form response
//items[0].getResponse() is the first response in the Form
//and it is the "Name"
body.replaceText('{Position}', items[0].getResponse());
//items[1].getResponse() is the second and it is the date
body.replaceText('{Location}', items[1].getResponse());
//You can add as much as you have and change them in the Template Doc like this
body.replaceText('{Days/Hours}', items[2].getResponse());
body.replaceText('{Qualifications}', items[3].getResponse());
body.replaceText('{Duties}', items[4].getResponse());
//and so on...
//Save and Close the open document
openDoc.saveAndClose();
var theBlob = newTempFile.getBlob().getAs('application/pdf');
//The name of the file is going to be the first and second question from the form
//change to your preference
var nameFile = items[0].getResponse() + '-' + items[1].getResponse() + '.pdf';
//send an email with the PDF
//If you don't want to send the PDF in the mail just delete everything
//from here -------
var email = 'youremail@email.com';
var subject = 'Your new document';
var body = 'Hello, <br/>Check this PDF file.';
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [{
fileName: nameFile,
content: theBlob.getBytes(),
mimeType: "application/pdf"
}]
});
//to here ------
// save the PDF file in your Drive
var savePDF = DriveApp.createFile (theBlob);
//if you want to save the file in a specific folder use this code
//get the ID from the folder link
//var folder = DriveApp.getFolderById('14nUc----------0lUb');
//var savePDF = folder.createFile (theBlob);
savePDF.setName(nameFile);
//delete the temp file
DriveApp.getFileById(newTempFile.getId()).setTrashed(true);
}
您不能直接用多个项目符号替换文本。
首先,您需要找到元素所在的位置。并且不是直接替换文本,而是需要遍历复选框响应返回的元素,然后将它们一一追加到包含占位符文本的 list/bullet 下方。添加所有元素后,删除占位符文本及其项目符号。
为此,我添加了两个单独的函数。第一个用于将新列表项附加到占位符项目符号,另一个用于在添加所有这些新列表项后删除占位符项目符号。请参阅下面的代码。
代码:
// make sure to copy the original template, and use that copy instead of directly editing the original template which what I did below
var templateDoc = DriveApp.getFileById('1Ep2yDj-EZohCQAZl-TV1iiFqfTdHulffAiHJWH_8YFY');
var openDoc = DocumentApp.openById(templateDoc.getId());
var body = openDoc.getBody();
function onFormSubmit(e) {
var items = e.response.getItemResponses();
items.forEach(function (item, index) {
switch (index){
case 0:
body.replaceText('{Position}', item.getResponse());
break;
case 1:
body.replaceText('{Location}', item.getResponse());
break;
case 2:
body.replaceText('{Days/Hours}', item.getResponse());
break;
case 3:
replaceBullet('{Qualifications}', item);
break;
case 4:
replaceBullet('{Duties}', item);
break;
default:
break;
}
});
// Now, we finished replacing the values we need to be replaced
// Add here the saving and closing of the document.
// Then prepare your send email code here.
}
function replaceBullet(template, item) {
var choices = item.getResponse();
choices.forEach(function (choice) {
appendToList(choice, template);
});
removeListTemplate(template);
}
function appendToList(elementContent, template) {
var childIndex = 0;
for (var i = 0; i < openDoc.getNumChildren(); i++) {
var child = openDoc.getChild(i);
if (child.getType() == DocumentApp.ElementType.LIST_ITEM && child.asText().getText() == template){
while(child.getType() == DocumentApp.ElementType.LIST_ITEM){
child = openDoc.getChild(i);
childIndex = body.getChildIndex(child);
i++;
}
child = openDoc.getChild(i-2);
var newElement = child.getParent().insertListItem(childIndex, elementContent).setGlyphType(DocumentApp.GlyphType.BULLET);
newElement.setListId(child);
}
}
}
function removeListTemplate(template) {
for (var i = 0; i < openDoc.getNumChildren(); i++) {
var child = openDoc.getChild(i);
if (child.getType() == DocumentApp.ElementType.LIST_ITEM && child.asText().getText() == template)
child.removeFromParent();
}
}
输出:
注:
- 当项目符号位于 table 内时,这将不起作用。这就是为什么我删除了 table 并在页面中有 2 列。
- 在第一列末尾插入“分栏符”,这样当项目符号被不同数量的项目符号替换时它不会移动
提交 Google 表单时,它会在 Google 文档模板中找到要替换的项目,然后通过电子邮件发送给我。 我无法将表单复选框响应作为文档中的要点。它目前将列出所有已检查的选项,但它们以逗号分隔,而不是项目符号。
如有任何帮助,我们将不胜感激!
function onFormSubmit(e) {
//open the template document by ID
//you can find the ID in the link of the document
var templateDoc = DriveApp.getFileById('1Dwbjh1Jqerg_WKvqdERyCfnDnWY2yjNArGY_f38ioAs');
//create a copy of the template, we don't wanna mess up the template doc
var newTempFile = templateDoc.makeCopy();
//open the new template document for editing
var openDoc = DocumentApp.openById(newTempFile.getId());
var body = openDoc.getBody();
//get the responses triggered by On Form Submit
var items = e.response.getItemResponses();
//find the text in the template doc and replace it with the Form response
//items[0].getResponse() is the first response in the Form
//and it is the "Name"
body.replaceText('{Position}', items[0].getResponse());
//items[1].getResponse() is the second and it is the date
body.replaceText('{Location}', items[1].getResponse());
//You can add as much as you have and change them in the Template Doc like this
body.replaceText('{Days/Hours}', items[2].getResponse());
body.replaceText('{Qualifications}', items[3].getResponse());
body.replaceText('{Duties}', items[4].getResponse());
//and so on...
//Save and Close the open document
openDoc.saveAndClose();
var theBlob = newTempFile.getBlob().getAs('application/pdf');
//The name of the file is going to be the first and second question from the form
//change to your preference
var nameFile = items[0].getResponse() + '-' + items[1].getResponse() + '.pdf';
//send an email with the PDF
//If you don't want to send the PDF in the mail just delete everything
//from here -------
var email = 'youremail@email.com';
var subject = 'Your new document';
var body = 'Hello, <br/>Check this PDF file.';
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [{
fileName: nameFile,
content: theBlob.getBytes(),
mimeType: "application/pdf"
}]
});
//to here ------
// save the PDF file in your Drive
var savePDF = DriveApp.createFile (theBlob);
//if you want to save the file in a specific folder use this code
//get the ID from the folder link
//var folder = DriveApp.getFolderById('14nUc----------0lUb');
//var savePDF = folder.createFile (theBlob);
savePDF.setName(nameFile);
//delete the temp file
DriveApp.getFileById(newTempFile.getId()).setTrashed(true);
}
您不能直接用多个项目符号替换文本。
首先,您需要找到元素所在的位置。并且不是直接替换文本,而是需要遍历复选框响应返回的元素,然后将它们一一追加到包含占位符文本的 list/bullet 下方。添加所有元素后,删除占位符文本及其项目符号。
为此,我添加了两个单独的函数。第一个用于将新列表项附加到占位符项目符号,另一个用于在添加所有这些新列表项后删除占位符项目符号。请参阅下面的代码。
代码:
// make sure to copy the original template, and use that copy instead of directly editing the original template which what I did below
var templateDoc = DriveApp.getFileById('1Ep2yDj-EZohCQAZl-TV1iiFqfTdHulffAiHJWH_8YFY');
var openDoc = DocumentApp.openById(templateDoc.getId());
var body = openDoc.getBody();
function onFormSubmit(e) {
var items = e.response.getItemResponses();
items.forEach(function (item, index) {
switch (index){
case 0:
body.replaceText('{Position}', item.getResponse());
break;
case 1:
body.replaceText('{Location}', item.getResponse());
break;
case 2:
body.replaceText('{Days/Hours}', item.getResponse());
break;
case 3:
replaceBullet('{Qualifications}', item);
break;
case 4:
replaceBullet('{Duties}', item);
break;
default:
break;
}
});
// Now, we finished replacing the values we need to be replaced
// Add here the saving and closing of the document.
// Then prepare your send email code here.
}
function replaceBullet(template, item) {
var choices = item.getResponse();
choices.forEach(function (choice) {
appendToList(choice, template);
});
removeListTemplate(template);
}
function appendToList(elementContent, template) {
var childIndex = 0;
for (var i = 0; i < openDoc.getNumChildren(); i++) {
var child = openDoc.getChild(i);
if (child.getType() == DocumentApp.ElementType.LIST_ITEM && child.asText().getText() == template){
while(child.getType() == DocumentApp.ElementType.LIST_ITEM){
child = openDoc.getChild(i);
childIndex = body.getChildIndex(child);
i++;
}
child = openDoc.getChild(i-2);
var newElement = child.getParent().insertListItem(childIndex, elementContent).setGlyphType(DocumentApp.GlyphType.BULLET);
newElement.setListId(child);
}
}
}
function removeListTemplate(template) {
for (var i = 0; i < openDoc.getNumChildren(); i++) {
var child = openDoc.getChild(i);
if (child.getType() == DocumentApp.ElementType.LIST_ITEM && child.asText().getText() == template)
child.removeFromParent();
}
}
输出:
注:
- 当项目符号位于 table 内时,这将不起作用。这就是为什么我删除了 table 并在页面中有 2 列。
- 在第一列末尾插入“分栏符”,这样当项目符号被不同数量的项目符号替换时它不会移动