如何使用数组从电子表格数据格式化电子邮件? [英] How to format email from spreadsheet data using arrays?

查看:36
本文介绍了如何使用数组从电子表格数据格式化电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编码完全陌生,我需要一些代码方面的帮助.这是我的问题:我想在 Google 电子表格中获取许多数据单元格并通过电子邮件发送.我想出了如何将数据拉入电子邮件并发送电子邮件,但是,数据仅以逗号分隔,很难区分.如何使每个单元格的数据显示在电子邮件中的单独一行上?我相信我可以用数组来做到这一点,但正如我所说,我是新手,我无法弄清楚如何使用我目前阅读的内容来做到这一点.如果你能提供一个解释,那也将不胜感激——希望这能让我不再问同样的问题并教我.感谢您的耐心和帮助!

I am totally new to coding and I need some help on a bit of code. Here is my problem: I want to take many cells of data in a Google spreadsheet and send it in an email. I figured out how to pull the data into an email and the email sends, however, the data is only separated by commas and it is hard to distinguish. How can I make each cell's data appear on a separate line in the email? I believe I can do this with arrays, but as I said, I am new to this and I cannot figure out how to do it with what I have read so far. If you could provide an explanation, that would be greatly appreciated as well - this will, hopefully, keep me from asking the same question again and teach me. Thank you for your patience and help!

function emailData() {

//Gather data from "Responses" sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var responses = ss.getSheetByName("Form Responses");
var lastRow = responses.getLastRow();
var data = responses.getRange("A"+(lastRow)+":AK"+(lastRow));
var values = data.getValues();

//Gather email addresses from "Email" sheet (I have not gotten this far yet)
var email = ss.getSheetByName("Email");
var startRow = 2;
var emailAdress = "email@email.com";
var subject = "Capacity Campaign Contact Form";
MailApp.sendEmail(emailAdress,subject,values);
}

推荐答案

你的数组(称为 Values)是一个二维数组,对应于你工作表中的一行,这意味着你必须遍历它的第一个元素.我编写了一个带有函数的小脚本,该函数将消息与标题和值组合在一起,如下所示:

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;
}

请注意,您可以使用相同的结构使用 以 html 格式构建外观更好的电子邮件html 表(请参阅此处的参考)

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)

编辑2

我写了一小段代码,可以在表格中生成文本和 HTML,随意使用颜色等改进 HTML 格式...

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>';
}

编辑 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éâtre</td></tr><tr><td>GROUPE</td><td>Festival</td></tr><tr><td>organisme</td><td>xxx</td></tr></table>

  • "th" 是标题标签(在 <> 之间,我不能在这里写,因为浏览器没有显示它)
  • "td" 是单元格标签(在 <> 之间,我不能在这里写,因为浏览器没有显示)
  • "tr" 是 "row" 标签(在 <> 之间,我不能在这里写,因为浏览器没有显示)

这些标签中的每一个都必须以/t... 结束标签(带周围的 <>)终止才能有效.正如您在日志数据中看到的那样,脚本中的循环结构会自动处理这些.

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.

这篇关于如何使用数组从电子表格数据格式化电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆