【问题标题】:How to send multiple sheets range data in one email google app script?如何在一封电子邮件谷歌应用程序脚本中发送多个工作表范围数据?
【发布时间】:2019-06-20 03:23:41
【问题描述】:

Google 云端硬盘文件夹中有两个电子表格。如何在一封电子邮件中发送多张工作表中的数据

到目前为止,当我执行我的脚本时,它将发送 3 封电子邮件,因为它包含两个工作簿中的 3 张工作表。一个工作簿包含两张工作表。我希望这两张纸在一封电子邮件中发送。现在它正在发送两封单独的电子邮件。

function getAllSheets(){
var file, files = 
DriveApp.getFolderById("1JTtvZNc41D575Ubu3NbzVYTh9tX8KhGj").getFiles();
while (files.hasNext()) {
file = files.next();
var activeSpreadSheet = SpreadsheetApp.open(file);
var sheets = activeSpreadSheet.getSheets();

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


var sheet = sheets[i]
var data = sheet.getDataRange().getValues();

var resultArr=[];
var xTitle = 'Part Numbers'; // XAxis Title
var yTitle = 'Quantity'; // YAxis Title

//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>";
          }


    //send email

    MailApp.sendEmail({to:"foo@bar.com",subject:subject, 
    htmlBody:body 
    + " Chart! <br> <img src='cid:chartImg'> ! <br> Done"});
         }

       }
   }

 };

【问题讨论】:

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


    【解决方案1】:

    您应该将邮件发送代码放在 for 语句之外,以便在您浏览完所有工作表或工作簿后发送邮件。

    逻辑是

    Function{
        Get files
        Through files{
            Get sheets
            Through sheets{
                Get Data
                Write data in body
            }
            Send mail with data
        }
    }
    

    看起来像(未经测试):

    function getAllSheets(){
        var file, files = 
        DriveApp.getFolderById("1JTtvZNc41D575Ubu3NbzVYTh9tX8KhGj").getFiles();
        while (files.hasNext()) {
            file = files.next();
            var activeSpreadSheet = SpreadsheetApp.open(file);
            var sheets = activeSpreadSheet.getSheets();
            var body='';
            //loop through sheets to look for value
            for (var i in sheets) {
                var sheet = sheets[i]
                var data = sheet.getDataRange().getValues();
    
                var resultArr=[];
                var xTitle = 'Part Numbers'; // XAxis Title
                var yTitle = 'Quantity'; // YAxis Title
    
                //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
    
                    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>";
                    }
                }
            }
            //send email
            MailApp.sendEmail({to:"sriram6897@gmail.com",subject:subject, 
            htmlBody:body 
            + " Chart! <br> <img src='cid:chartImg'> ! <br> Done"});
        }
     };
    

    【讨论】:

    • @Sriram 这是因为您在发送邮件之前没有存储图表。您应该尝试将图表存储在一个数组中并在您的邮件发送方法上循环以添加所有需要的图表
    • 是的,我想我明白了。非常感谢
    • 你好。如何在一封电子邮件中发送多个电子表格数据?
    • @Sriram 你应该把你的数据放在一个数组中,一旦你退出通过文件的语句,就做邮件部分
    【解决方案2】:

    如果您想将两个工作表中的信息附加到“body”和“subject”,则必须在循环工作表之前声明 var body,并在循环之后发送电子邮件。通过这种方式,您将从两张表中收集信息并将其分配给变量“body”和“subject”。

    我测试了一个类似于 Pierre-Marie Richard 的代码,但是也解决了“主题”问题。为可见性目的添加完整的有效负载最终结果:

    function getAllSheets(){
    var file, files = DriveApp.getFolderById("1JTtvZNc41D575Ubu3NbzVYTh9tX8KhGj").getFiles();
    while (files.hasNext()) {
    file = files.next();
    var URL=file.getUrl()
    var parent=file.getParents()
    Logger.log(parent)  
    var activeSpreadSheet = SpreadsheetApp.open(file);
    var sheets = activeSpreadSheet.getSheets();
    
    
    // define body and subject before looping though the sheets, like this information of both sheets will be attached to the body
    
     var body='';
      var subject = 'Range exceeded Alert';
    
    
    //loop through sheets to look for value
    for (var i in sheets) {
    
    var sheet = sheets[i]
    var data = sheet.getDataRange().getValues();
    
    var resultArr=[];
    var xTitle = 'Part Numbers'; // XAxis Title
    var yTitle = 'Quantity'; // YAxis Title
    
    
    //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)
        {
       // add the subject of each sheet here
          subject+= " "+'Range exceeded Alert' + "" + sheet.getName();
          //Creates a body through the obtained values
           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>";
              }
         }
       }
            //send email after looping through the sheets, so that information of both sheets will be sent
          MailApp.sendEmail({to:"your email",subject:subject,            
        htmlBody:body 
        + " Chart! <br> <img src='cid:chartImg'> ! <br> Done"});
       }
     };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多