脚本将范围复制为图片并通过邮件发送 [英] Script to copy a range as picture and send it by mail

查看:119
本文介绍了脚本将范围复制为图片并通过邮件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个脚本,该脚本的作用与MS Excel中的按图片复制"选项相同.我需要将Google表格中的某些表格复制为图片并粘贴到电子邮件(Gmail)中.我找到了可以将图表转换为图片并通过邮件发送的代码,但是没有找到可以复制表格并将其粘贴为图片的代码.我想将表格粘贴为图片的原因是保持表格的格式,因为我知道用HTML构建表格不会允许我保留条件格式和HTML缺少的其他功能.

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.

其他论坛中的人们建议使用表格到幻灯片"脚本,获取表格数据所在的范围",然后将其发送到Google幻灯片.然后,在幻灯片"中,为每个幻灯片将幻灯片下载为.jpeg/.png文件,然后将其添加到电子邮件正文中.我也不知道该怎么做.

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.

推荐答案

您可以尝试将HTML表和条件格式一起复制.

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

这篇关于脚本将范围复制为图片并通过邮件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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