1
votes

In my Spreadsheet, I have taken the answers from the users and added a column to grade them. Now, I want to mail them the whole data they have entered and their respective grades. To do that, I have to know the Row number of the variable "key2" which will be changing for every response of the user. I am providing my code here. Some one please help me.

/* 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

1 Answers

0
votes

The range you are mentioning here s2.getRange(1,1,1,10) is basically getting only first row of data from the sheet.

Instead get all the rows and column values into a range variable and loop through the range for each row and send an email in the loop itself.

Check this documentation for more details.