You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apps Script开发咨询:Google Form复选框回复如何转为PDF内项目符号

解决方案

核心原因是replaceText仅支持纯文本替换,复选框响应原生为数组,直接传入会被自动转为逗号分隔的字符串,无法生成项目符号列表,需要单独处理复选框字段的占位符,在对应位置插入结构化列表。

前置准备

  • 先确认表单内哪些是复选框类型问题,以下示例假设{Qualifications}(第4项回复,索引3)和{Duties}(第5项回复,索引4)是复选框字段,你可根据实际业务调整索引
  • Google Doc模板内,复选框对应的占位符需要单独占一个段落,不要和其他文字放在同一行

修改后完整代码

function onFormSubmit(e) {
  // 处理复选框占位符为项目符号列表的辅助函数
  function replaceCheckboxPlaceholder(body, placeholder, options) {
    const range = body.findText(placeholder);
    if (!range) return;
    // 获取占位符所在段落
    const para = range.getElement().getParent().asParagraph();
    const paraIndex = para.getParent().getChildIndex(para);
    // 无选中项时显示默认文本
    if (!Array.isArray(options) || options.length === 0) {
      para.clear();
      para.setText("无");
      return;
    }
    // 插入项目符号列表
    body.insertListItems(paraIndex, options, DocumentApp.GlyphType.BULLET);
    // 删除原占位符所在的空段落
    para.removeFromParent();
  }

  //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();
  
  // 普通文本字段沿用原替换逻辑
  body.replaceText('{Position}', items[0].getResponse());  
  body.replaceText('{Location}', items[1].getResponse());
  body.replaceText('{Days/Hours}', items[2].getResponse());

  // 复选框字段调用专门方法替换为项目符号列表
  const qualifications = items[3].getResponse();
  replaceCheckboxPlaceholder(body, '{Qualifications}', qualifications);
  const duties = items[4].getResponse();
  replaceCheckboxPlaceholder(body, '{Duties}', duties);
  
  //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);
}

注意事项

  • 如果你的业务逻辑已经把复选框响应转为了逗号分隔字符串,可通过items[n].getResponse().split(',')转为数组后再传入辅助函数
  • 列表样式可调整:将DocumentApp.GlyphType.BULLET改为DocumentApp.GlyphType.NUMBER可生成有序数字列表
  • 无选中项时默认显示“无”,可根据需求修改辅助函数内的默认文本

内容的提问来源于stack exchange,提问作者Garrett Walker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 05:36:04