Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过电子邮件发送迷你图表发送空白单元格,而不是数据_Javascript_Google Apps Script_Google Sheets_Google Sheets Api_Sparklines - Fatal编程技术网

Javascript 通过电子邮件发送迷你图表发送空白单元格,而不是数据

Javascript 通过电子邮件发送迷你图表发送空白单元格,而不是数据,javascript,google-apps-script,google-sheets,google-sheets-api,sparklines,Javascript,Google Apps Script,Google Sheets,Google Sheets Api,Sparklines,我有一个Google工作表,在单元格图表中包含数据单元格和=sparkline(),我想通过电子邮件发送此数据视图。我目前使用应用程序脚本制作HTML电子邮件,但在电子邮件的表格中,闪烁图表显示为空白单元格 在谷歌表单上查看的数据: 电子邮件中查看的数据: 这是我的应用程序脚本代码: function drawTable() { var ss_data = getData(); var data = ss_data[0]; var background = ss_dat

我有一个Google工作表,在单元格图表中包含数据单元格和
=sparkline()
,我想通过电子邮件发送此数据视图。我目前使用应用程序脚本制作HTML电子邮件,但在电子邮件的表格中,闪烁图表显示为空白单元格

在谷歌表单上查看的数据:
电子邮件中查看的数据:

这是我的应用程序脚本代码:

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.getUser().getEmail(),
        subject: "Spreadsheet Data",
        htmlBody: html
    });
}


function getData(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").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];
}
函数drawTable(){
var ss_data=getData();
var数据=ss_数据[0];
var背景=ss_数据[1];
var fontColor=ss_数据[2];
var fontStyles=ss_数据[3];
var fontWeight=ss_数据[4];
var fontSize=ss_数据[5];
var html=“”;
对于(变量i=0;i

是否有人可以建议在电子邮件中显示火花线图表所需的代码,或其他解决方法?

此解决方法如何?我认为在你的情况下有几个变通办法。所以,请把这看作是其中之一

不幸的是,
SPARKLINE()
创建的图表无法直接导入到电子邮件中。因此,作为一种解决方法,我使用了以下流程

  • 使用EmbeddedChart将由
    SPARKLINE()
    创建的图表转换为图像
  • 将图像作为内联图像发送电子邮件
  • 修改脚本:
    drawTable();
    函数drawTable(){
    var ss_data=getData();
    var数据=ss_数据[0];
    var背景=ss_数据[1];
    var fontColor=ss_数据[2];
    var fontStyles=ss_数据[3];
    var fontWeight=ss_数据[4];
    var fontSize=ss_数据[5];
    var html=“”;
    var images={};//已添加
    对于(变量i=0;i
    结果:

    注:
    • 在这个修改后的脚本中,它假设您将图表用作
      迷你图
    • 我使用
      insertChart()
      在工作表中创建了一个图表,因为直接从
      newChart()
      检索的blob不能用作图像。将图表创建到图纸时,从创建的图表中检索到的blob可以用作图像
    • 如果要修改图像的大小,请修改
      setOption(“宽度”,200)
      setOption(“高度”,100)
    参考资料:

    如果我误解了你的问题,我很抱歉。

    我也遇到过同样的问题,在文档和幻灯片中嵌入带有小插图的表格。我认为这是一个错误,并报告了它@欢迎光临。我很高兴你的问题解决了。
    drawTable();
    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'>";
        var images = {}; // Added
        for (var i = 0; i < data.length; i++) {
            html += "<tr>"
            for (var j = 0; j < data[i].length; j++) {
                if (typeof data[i][j] == "object") { // Added
                    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;'><img src='cid:img" + i + "'></td>"; // Added
                    images["img" + i] = data[i][j]; // Added
                } else {
                    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.getUser().getEmail(),
            subject: "Spreadsheet Data",
            htmlBody: html,
            inlineImages: images // Added
        })
    }
    
    function getData(){
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
      var ss = sheet.getDataRange();
      var val = ss.getDisplayValues();
      var background = ss.getBackgrounds();
      var fontColor = ss.getFontColors();
      var fontStyles = ss.getFontStyles();
      var fontWeight = ss.getFontWeights();
      var fontSize = ss.getFontSizes();
      var formulas = ss.getFormulas(); // Added
      val = val.map(function(e, i){return e.map(function(f, j){return f ? f : getSPARKLINE(sheet, formulas[i][j])})}); // Added
      return [val,background,fontColor,fontStyles,fontWeight,fontSize]; 
    }
    
    // Added
    function getSPARKLINE(sheet, formula) {
      formula = formula.toUpperCase();
      if (~formula.indexOf("SPARKLINE")) {
        var chart = sheet.newChart()
          .setChartType(Charts.ChartType.SPARKLINE)
          .addRange(sheet.getRange(formula.match(/\w+:\w+/)[0]))
          .setTransposeRowsAndColumns(true)
          .setOption("showAxisLines", false)
          .setOption("showValueLabels", false)
          .setOption("width", 200)
          .setOption("height", 100)
          .setPosition(1, 1, 0, 0)
          .build();
        sheet.insertChart(chart); 
        var createdChart = sheet.getCharts()[0];
        var blob = createdChart.getAs('image/png');
        sheet.removeChart(createdChart);
        return blob;
      }
    }