Script to copy a range as picture and send it by m

2020-07-23 07:30发布

问题:

I need to create a script that does the same thing as the "copy as picture" option in MS Excel. I need certain tables located in a Google Sheets to be copied as picture and pasted in an email (Gmail). I have found a code that works converting charts to pictures and sending that through mail, but I haven't found a code that copies a table and pastes it as picture. The reason I want to paste the table as picture is to keep the format of the table since I know that building it in HTML would not allow me to keep conditional formatting and other features that HTML lacks.

People in other forums suggested a Sheets to Slides script, take the Range that the table data is in, and send this to Google Slides. Then, while in Slides, downloading the slide as a .jpeg/.png file for each slide and then adding this to the body of the email. I don't know how to do that either.

回答1:

You can try to replicate the table in HTML along with the conditional formatting.

function drawTable() {
    var ss_data = getData();
    var data = ss_data[0];
    var background = ss_data[1];
    var fontColor = ss_data[2];
    var fontStyles = ss_data[3];
    var fontWeight = ss_data[4];
    var fontSize = ss_data[5];
    var html = "<table border='1'>";
    for (var i = 0; i < data.length; i++) {
        html += "<tr>"
        for (var j = 0; j < data[i].length; j++) {
            html += "<td style='height:20px;background:" + background[i][j] + ";color:" + fontColor[i][j] + ";font-style:" + fontStyles[i][j] + ";font-weight:" + fontWeight[i][j] + ";font-size:" + (fontSize[i][j] + 6) + "px;'>" + data[i][j] + "</td>";
        }
        html += "</tr>";
    }
    html + "</table>"
    MailApp.sendEmail({
        to: Session.getActiveUser().getEmail(),
        subject: "Spreadsheet Range",
        htmlBody: html
    })
}


function getData(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Raw").getDataRange();
  var background = ss.getBackgrounds();
  var val = ss.getDisplayValues();
  var fontColor = ss.getFontColors();
  var fontStyles = ss.getFontStyles();
  var fontWeight = ss.getFontWeights();
  var fontSize = ss.getFontSizes();
  return [val,background,fontColor,fontStyles,fontWeight,fontSize];
}