I would like to send an email to a list of people that contains a table and charts extracted from a spreadsheet.

Doing this is manually is simple - copy the range to clip board, paste into the message editor. Similarly with charts. However, my application requires multiple charts and ranges which makes the process time consuming and error prone.

I have tried to build the message as follows:

function makeMail(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var dataTable = ss.getSheetByName("calc")
                    .getRange("B116:I125")
                    .getDataTable(true);
  var sData = ss.getSheetByName("Results")                    
                .getRange("A5:F22")
                .getValues();

  var chartImage = Charts.newLineChart()
                      .setTitle('Top Teams')
                      .setDataTable(dataTable)
                      .build()
                      .getAs('image/png'); //get chart as image

  var mB = "<h2> Testing Mail </h2>";
  mB +=  "<style> table,th,td { border:1px solid black;} </style>";
  mB +=  " <table> <tr> <th>Team Name</th> <th> Owners </th> <th>This Week$</th>"
  mB +=  " <th>Rank</th><th>YTD pts</th><th>YTD Rank</th></tr></tr>";
  for (var i=0;i<6;i++){
    mB += "<td> " + sData[2][i] + "</td>";
    mB += "</tr>";
  } 
  mB += "</table>";
MailApp.sendEmail({
  to: "xyzzy@gmail.com",
  subject: "Chart",
  htmlBody: mB,
  inlineImages:{
  chartImg: chartImage
  }
 });  
}

The main problems with his approach are the complexity of the code to perform such a trivial task and there seems to be a problem with the way that HTML is rendered by the mailer - the style tags for the table have no effect.

Is there a better way to approach this problem.

有帮助吗?

解决方案

Besides styles already addressed in a comment, you can speed it up by using pre-created charts in rhe sheets and change range if needed. You can also use named ranges to make it easier to read and maintain by editing ranges from the spreadsheet instead of modifying code.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top