简体   繁体   English

如何根据从电子表格中提取的数据生成图表

[英]How to generate a chart from the data which is pulled from spreadsheet

I have a range of data from spreadsheet that will be sent to an email every week once. 我有一系列来自电子表格的数据,每周都会发送一封电子邮件。 How can I generate a chart from the data? 如何从数据生成图表?

I have successfully pulled the data from the spreadsheet and send the data to email. 我已成功从电子表格中提取数据并将数据发送到电子邮件。 I am not sure how to create a chart with the data extracted. 我不确定如何使用提取的数据创建图表。

function CheckSales(){

  var app = SpreadsheetApp;
  var activeSheet = app.getActiveSpreadsheet().getSheets();
  //var data=activeSheet.getDataRange().getValues();

  //loop through sheets to look for value
  for (var i in activeSheet) {

  SpreadsheetApp.setActiveSheet(activeSheet[i])
  var sheet = app.getActiveSheet();
  var data = activeSheet[i].getDataRange().getValues();

  var emailAddress=SpreadsheetApp.getActiveSpreadsheet()
  .getSheetByName("Sheet1").getRange("B2").getValue();
  var resultArr=[];

  //To Loop through the whole data Rows
  for(var i=1;i<data.length;i++)
  {
    //Takes columns from L to S (To loop through the Columns)
    for(var j=11;j<19;j++)
    {
      var cellVal=data[i][j];
      Logger.log(cellVal)
      if(cellVal>0)
      {
        //Stores the Part No, Month Header Value of the Column, Cell Value 
which is greater then 0
        resultArr.push([data[i][0],data[0][j],cellVal])
      }
    }
  }
  if(resultArr.length>0)
  {
    var subject = 'Range exceeded Alert' + "" + sheet.getName();

    //Creates a body through the obtained values
    var body='';
    for(var m=0;m<resultArr.length;m++)
    {
      body+="For Part No "+resultArr[m][0].toString()+" and Month 
"+resultArr[m][1]
      .toString()+", Value is "+resultArr[m][2].toString()+"<br>";
    }

    MailApp.sendEmail({to:emailAddress,subject:subject,htmlBody:body});
  }

 }

}

Now I have two sheets with data that will send email twice with the part number month and the value. 现在我有两张包含数据的工作表,这些数据将使用零件编号月份和值发送两次电子邮件。 I expect these data to generate a chart 我希望这些数据能够生成图表

I got the solution. 我得到了解决方案。 Below is the code 下面是代码

  if (sheet.getCharts().length > 0){
  var chart = sheet.getCharts()[0];
  chart = chart.modify()
  .removeRange(chart.getRanges()[0])
  .addRange(sheet.getRange('L1:S22'))
  .build();
  sheet.updateChart(chart);
  }

  else{
  var sheet = SpreadsheetApp.getActiveSheet();
  var chart = sheet.newChart()
  .setPosition(5, 6, 5, 5)
  .setChartType(Charts.ChartType.BAR)
  .addRange(sheet.getRange('L1:S22'))
  .build();
  sheet.insertChart(chart);
  }
  var charts = SpreadsheetApp.getActiveSheet().getCharts();



 MailApp.sendEmail({to:emailAddress,subject:subject,htmlBody:body,attachments: 
 charts});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM