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

如何让Google Forms将问题与答案同步至Google Doc?

Solution: Add Questions to Your Generated Google Doc

Got it, the issue here is that e.values only gives you the submitted responses—not the actual questions from your form. To include both, we need to pull the questions directly from the Google Form itself and pair them with the corresponding answers. Here's how to adjust your script:

First, you'll need your Form's ID (you can grab this from the form's URL—it's the long string between /d/ and /edit). Then we'll modify the script to fetch the form's questions, loop through them, and write both question and answer into the document.

Updated Code

function myFunction(e) {
  // Replace this with your actual Google Form ID
  const FORM_ID = "YOUR_FORM_ID_GOES_HERE";
  const form = FormApp.openById(FORM_ID);
  const formItems = form.getItems();
  
  // Extract responses (skip index 0 which is the timestamp)
  const responses = e.values.slice(1);
  
  // Get respondent name (adjust index if your name field is in a different position)
  const respondentName = e.values[1];
  const doc = DocumentApp.create(`${respondentName}'s PEL`);
  const docBody = doc.getBody();
  
  // Add a header for the document
  docBody.appendParagraph(`${respondentName}'s PEL Submission`)
         .setHeading(DocumentApp.ParagraphHeading.HEADING1)
         .setBold(true);
  docBody.appendParagraph(""); // Add spacing
  
  // Loop through each question-answer pair
  formItems.forEach((item, index) => {
    // Make sure we don't exceed the number of responses
    if (index < responses.length) {
      const questionText = item.getTitle();
      const answerText = responses[index];
      
      // Write the question in bold
      docBody.appendParagraph(`**${questionText}**`)
             .setBold(true);
      // Write the answer
      docBody.appendParagraph(answerText);
      // Add space between each section
      docBody.appendParagraph("");
    }
  });
  
  // Save and close the document to finalize
  doc.saveAndClose();
}

Key Notes:

  • Form ID: Don't forget to replace YOUR_FORM_ID_GOES_HERE with your actual form ID.
  • Field Positions: If your name field isn't the second item (index 1 in e.values), adjust respondentName = e.values[X] where X is the correct index.
  • Non-Text Items: For items like checkboxes or multiple choice, e.values will still return the selected options as text (comma-separated for checkboxes), so this code works out of the box. If you need more formatting for specific item types (like bullet points for checkboxes), let me know and I can expand on that!
  • Permissions: Make sure your script has access to the Form—when you first run it, you'll need to authorize it to access your Google Forms and Docs.

That should do it! Now when someone submits your form, the generated Doc will have both the questions and their answers clearly laid out.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 02:28:16