如何从表单响应生成Google文档,其中包含所有表单项和响应?

时间:2014-04-29 15:04:33

标签: google-apps-script google-docs google-form

我目前正在使用以下内容将电子邮件发送到包含标题/问题的分发列表以及提交Google表单时的相应答案:

function sendFormByEmail(e) {

 // Subject field of email

 var emailSubject = "New Student Case Filed";  

 // Comma-separated list of email addresses for distribution.
 var yourEmail     = "my email address";

 // Spreadsheet key, found in the URL when viewing your spreadsheet.
 var docKey       = "my spreadsheet key";

 // If you want the script to auto send to all of the spreadsheet's editors, set this value as 1.
 // Otherwise set to 0 and it will send to the yourEmail values.
 var useEditors     = 0;

 // Have you added columns that are not being used in your form? If so, set this value to 
 // the NUMBER of the last column that is used in your form.
 // for example, Column C is the number 3
 var extraColumns = 40;

  if (useEditors) {
  var editors = DocsList.getFileById(docKey).getEditors();
  if (editors) { 
   var notify = editors.join(',');
  } else var notify = yourEmail;
 } else {
  var notify = yourEmail;
 }

 // The variable e holds all the submission values in an array.
 // Loop through the array and append values to the body.
 // Need to omit headers with no response*

 var s = SpreadsheetApp.getActive().getSheetByName("StudentCases");
 if (extraColumns){
  var headers = s.getRange(1,1,1,extraColumns).getValues()[0];
 } else var headers = s.getRange(1,1,1,40).getValues()[0];

 var message = "";
 for(var i in headers) {
   message += headers[i] + ' : '+ e.values[i].toString() + '\n\n'; 
 }

现在我还想要一个包含标题和回复的Google文档。到目前为止,我已经能够创建文档,添加标题,并添加一个段落,但现在我需要在Google文档中复制标题/响应数组。

// Create a new Google Doc named 'Case File' * need to figure out how to pull last name response from form.

 var doc = DocumentApp.create('Case File: Last Name*');

 // Access the body of the Doc, add a paragraph, *need to append array of headers/answers

 var body = doc.getBody().body.appendParagraph(); 

 // Get the URL of the Google Doc to include in Email

 var url = doc.getUrl();

 // Get ID of Doc to attach to email

 var id = doc.getId()

我想解决的另一个问题;我只需要包含响应的标题/问题,因为许多标题/问题不一定能得到答案。换句话说,如果没有答案,那么就不会附加到电子邮件中。

1 个答案:

答案 0 :(得分:0)

您似乎提供了一般要求列表,但没有像您尝试过的结果那样。如果您提供更准确的问题,StackOverflow会对您有所帮助。

您能分享一下您尝试过的确切代码吗?你的结果是什么?

从高层次来看,我将继续这个一般的工作流程。

  1. 在您选择的变量命名法中使用占位符草拟gDoc模板~FName~LName等。
  2. 使用onFormSubmit触发器在提交新表单时制作gDoc模板的副本。
  3. 将复制的gDoc中的~FName~LName占位符替换为
  4. 形式中捕获的内容
  5. 将复制的gDoc另存为PDF
  6. 将PDF通过电子邮件发送到表单提交中提供的电子邮件地址