在表单提交时创建的多个文件夹

时间:2016-01-24 23:57:43

标签: javascript forms google-form

在后台运行到google表单的脚​​本返回一个新的文件夹和电子邮件,回答每个问题而不只是1个文件夹和1个电子邮件....任何人都可以帮助看看我哪里出错了!

function onSubmit() {
var form = FormApp.openById('123456789abcdefghij');
var formResponses = form.getResponses();
 for (var i = 0; i < formResponses.length; i++) {
  var formResponse = formResponses[i];
   var itemResponses = formResponse.getItemResponses();
   for (var j = 0; j < itemResponses.length; j++) {
    var itemResponse = itemResponses[j];
     Logger.log('Response #%s to the question "%s" was "%s"',
       (i + 1).toString(),
        itemResponse.getItem().getTitle(),
        itemResponse.getResponse());

var DriveFolder = DriveApp.getFolderById("0B2eMBrrkabcdefghij");
 var foldername = itemResponses[2].getResponse();
var folderpath = DriveFolder.createFolder(foldername).getUrl();
var to = itemResponses[1].getResponse();}

 MailApp.sendEmail(
     to,"Change Management Request", "Thank you for Submitting Step 1. Please fill out Step 2 (https://drive.google.com/open?id=0B2eMBrrkabcdefji) and upload to" +folderpath); 
}}

1 个答案:

答案 0 :(得分:0)

重新格式化代码显示用于创建发送邮件的文件夹的部分嵌套在for循环内的深度:

function onSubmit() {
    var form = FormApp.openById('123456789abcdefghij');
    var formResponses = form.getResponses();
    for (var i = 0; i < formResponses.length; i++) {
        var formResponse = formResponses[i];
        var itemResponses = formResponse.getItemResponses();
        for (var j = 0; j < itemResponses.length; j++) {
            var itemResponse = itemResponses[j];
            Logger.log(
                'Response #%s to the question "%s" was "%s"',
                (i + 1).toString(),
                itemResponse.getItem().getTitle(),
                itemResponse.getResponse());

            var DriveFolder = DriveApp.getFolderById("0B2eMBrrkabcdefghij");
            var foldername = itemResponses[2].getResponse();
            var folderpath = DriveFolder.createFolder(foldername).getUrl();
            var to = itemResponses[1].getResponse();
        }

        MailApp.sendEmail(
            to,
            "Change Management Request",
            "Thank you for Submitting Step 1. Please fill out Step 2 (https://drive.google.com/open?id=0B2eMBrrkabcdefji) and upload to" +folderpath); 
    }
}

可能你想将它们移出,但不知道你想要哪个字段来计算文件夹名称,我只能猜测:

function onSubmit() {
    var form = FormApp.openById('123456789abcdefghij');
    var formResponses = form.getResponses();
    for (var i = 0; i < formResponses.length; i++) {
        var formResponse = formResponses[i];
        var itemResponses = formResponse.getItemResponses();
        for (var j = 0; j < itemResponses.length; j++) {
            var itemResponse = itemResponses[j];
            Logger.log(
                'Response #%s to the question "%s" was "%s"',
                (i + 1).toString(),
                itemResponse.getItem().getTitle(),
                itemResponse.getResponse());
        }
    }

    var DriveFolder = DriveApp.getFolderById("0B2eMBrrkabcdefghij");
    var folderAndToResponse = formResponses[formResponses.length - 1].getItemResponses(); 
    var foldername = folderAndToResponse[2].getResponse();
    var folderpath = DriveFolder.createFolder(foldername).getUrl();
    var to = folderAndToResponse[1].getResponse();

    MailApp.sendEmail(
        to,
        "Change Management Request",
        "Thank you for Submitting Step 1. Please fill out Step 2 (https://drive.google.com/open?id=0B2eMBrrkabcdefji) and upload to" + folderpath); 
}