电子表格中的变量行号(Apps脚本)

时间:2015-06-05 14:15:37

标签: google-apps-script google-sheets

在我的电子表格中,我已经从用户那里获得了答案,并添加了一列来评分。现在,我想将他们输入的所有数据和他们各自的成绩邮寄给他们。要做到这一点,我必须知道变量的行号" key2"这将针对用户的每个响应进行更改。我在这里提供我的代码。有人请帮助我。

/* Send Confirmation Email with Google Forms */

function Initialize() {

    var triggers = ScriptApp.getProjectTriggers();

    for (var i in triggers) {
        ScriptApp.deleteTrigger(triggers[i]);
    }

    ScriptApp.newTrigger("SendConfirmationMail")
    .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
    .onFormSubmit()
    .create();

 }

function SendConfirmationMail(e) {
    var s1 = SpreadsheetApp.getActiveSpreadsheet();
    var s2 = SpreadsheetApp.setActiveSheet(s1.getSheetByName("Sheet2"));

    try {
        var  cc, sendername, subject, column2;
        var message, value, textbody, sender;

        // This is your email address and you will be in the CC
        cc = Session.getActiveUser().getEmail();

        // This will show up as the sender's name
        sendername = "Quiz2win";

        // Optional but change the following variable
        // to have a custom subject for Google Docs emails
        subject = "Google Form Successfully Submitted";

        // This is the body of the auto-reply
        message += "We have received your details.<br />Thanks!<br /><br />";

        column2 = s2.getRange(1,1,1,10).getValues()[0];
        // This is the submitter's email address
        sender = e.namedValues["Email Address"].toString();

        // Only include form values that are not blank

        for ( var keys in column2 ) {

            var key2 = column2[keys];
            if ( e.namedValues[key2] ) {
                message += key2 + ' :: '+ e.namedValues[key2] + "<br />";
            } else  {
                var key1 = s2.getValues();
                message += key2 + ' :: ' + key1 + "<br/>";
            }
        }

        textbody = message.replace("<br>", "\n");

        GmailApp.sendEmail(sender, subject, textbody, 
                        {cc: cc, name: sendername, htmlBody: message});

    } catch (e) {
    Logger.log(e.toString());
}

1 个答案:

答案 0 :(得分:0)

您在此处提及的范围s2.getRange(1,1,1,10)基本上只从表格中获取第一行数据。

而是将所有行和列值放入范围变量中,并遍历每行的范围,并在循环中发送电子邮件。

查看此documentation了解详情。