Your array (called Values) is a 2 dimension array corresponding to a row in your sheet, it means that you have to iterate through its first element.
I wrote a small script with a function that composes the message with headers and values like this :
function test(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var responses = ss.getSheetByName("Form Responses");
var lastRow = responses.getLastRow();
var values = responses.getRange("A"+(lastRow)+":AK"+(lastRow)).getValues();// get the range and values in one step
var headers = responses.getRange("A1:AK1").getValues();// do the same for headers
var message = composeMessage(headers,values);// call the function with 2 arrays as arguments
Logger.log(message);// check the result and then send the email with message as text body
}
function composeMessage(headers,values){
var message = 'Here are the data :'
for(var c=0;c<values[0].length;++c){
message+='
'+headers[0][c]+' : '+values[0][c]
}
return message;
}
note that you could use the same structure to build an even better looking email in html format using a html table (see reference here)
EDIT2
I wrote a little piece of code that generates both in text and HTML in a table, feel free to improve HTML formating with colors etc...
function testMail(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var responses = ss.getSheetByName("Sheet1");
var lastRow = responses.getLastRow();
var values = responses.getRange("A"+(lastRow)+":M"+(lastRow)).getValues();
var headers = responses.getRange("A1:AK1").getValues();
var message = composeMessage(headers,values);
var messageHTML = composeHtmlMsg(headers,values);
Logger.log(messageHTML);
MailApp.sendEmail(Session.getEffectiveUser().getEmail(),'test html', message,{'htmlBody':messageHTML});
}
function composeMessage(headers,values){
var message = 'Here are the data you submitted :
'
for(var c=0;c<values[0].length;++c){
message+='
'+headers[0][c]+' : '+values[0][c]
}
return message;
}
function composeHtmlMsg(headers,values){
var message = 'Here are the data you submitted :<br><br><table style="background-color:lightblue;border-collapse:collapse;" border = 1 cellpadding = 5><th>data</th><th>Values</th><tr>'
for(var c=0;c<values[0].length;++c){
message+='<tr><td>'+headers[0][c]+'</td><td>'+values[0][c]+'</td></tr>'
}
return message+'</table>';
}
EDIT 3
following your comment : here is the log of my test sheet + screen capture. Check your log to see if you get a similar structure with your data.
[13-07-16 14:29:40:920 CEST] Here are the data you submitted :<br><br><table style="background-color:lightblue;border-collapse:collapse;" border = 1 cellpadding = 5><th>data</th><th>Values</th><tr><tr><td>Titre</td><td>Mr</td></tr><tr><td>nom</td><td>Wales</td></tr><tr><td>prénom</td><td>xavier</td></tr><tr><td>adresse</td><td>Sunset Bld, 45678</td></tr><tr><td>code</td><td>5000</td></tr><tr><td>ville</td><td>Los Angeles</td></tr><tr><td>pays</td><td>USA</td></tr><tr><td>email</td><td>john.smith@gmail.com</td></tr><tr><td>tél1</td><td>1212345654345</td></tr><tr><td>tél2</td><td></td></tr><tr><td>Commun</td><td>Théatre</td></tr><tr><td>GROUPE</td><td>Festival</td></tr><tr><td>organisme</td><td>xxx</td></tr></table>
- "th" is the header tag (between <>, I cannot write it here because the Browser doesn't show it)
- "td" is the cell tag (between <>, I cannot write it here because the Browser doesn't show it)
- "tr" is the "row" tag (between <>, I cannot write it here because the Browser doesn't show it)
Each of these tags must be terminated by a /t... closing tag (with surrounding <>)to be valid. The loop structure in the script takes care of that automatically as you can see in the log data.
Note also that I added a table end tag at the end of the loop... I forgot it in my first code but it worked like that in my tests.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…