图表内嵌图像裁剪

时间:2015-12-04 21:57:29

标签: google-apps-script gmail google-visualization

我正在尝试将在Google工作表中创建的图表作为内嵌图像放入gmail html正文中。它看起来,但它被裁剪(图像左),我无法弄清楚如何显示整个图表。图表在整个图片中显示为完整且正常(右图)

cropped chart image uncropped chart

以下是代码:

//creates chart of tasks to embed an inline image in gmail message
  var chart = sheet.newChart()
     .setChartType(Charts.ChartType.TABLE)
     .addRange(range)
     .setOption('height', 800)
     .setPosition(1,1,0,0)
     .build();

  sheet.insertChart(chart);

  var table = sheet.getCharts()[0];

  var htmlList = '<h2>Tasks:</h2><img src="cid:chart1" />';
      htmlList = htmlList + '<br><br>Click <a href="https://sites.google.com/a/dwightlondon.org/dsl-whole-school-staff-hub/new-students" target="_blank">here</a> to visit Induction page';

table.modify().setOption('height', 800)
  var taskList = table.getAs('image/gif').setName('chart1');



  GmailApp.sendEmail('adadasd@dwightlondon.org', 
                     'Updated induction tasks for '+userType + ' ' + student, 
                     'tasks: ',
                     { htmlBody: htmlList, 
                       inlineImages: {
                          chart1: taskList
                        }});

  sheet.removeChart(table[0]);

1 个答案:

答案 0 :(得分:0)

我认为图像因默认尺寸值而被裁剪。

使用嵌入式图表我无法更改尺寸,但是,我尝试使用Charts服务并能够设置尺寸。

所以稍微修改你的代码我得到了完整的图表:

var chart = Charts.newTableChart().setDataTable(range.getDataTable()).setDimensions(600, 600).build();

var htmlList = '<h2>Tasks:</h2><img src="cid:chart1" />';
      htmlList = htmlList + '<br><br>Click <a href="https://sites.google.com/a/dwightlondon.org/dsl-whole-school-staff-hub/new-students" target="_blank">here</a> to visit Induction page';

  GmailApp.sendEmail('adadasd@dwightlondon.org', 
                     'Updated induction tasks for '+userType + ' ' + student, 
                     'tasks: ',
                     { htmlBody: htmlList, 
                       inlineImages: {
                          chart1: chart.getBlob().setName('chart1')
                        }});

检查这是否适合您。