简体   繁体   English

Email 来自 Google Apps Scripts 的过滤结果

[英]Email the filter results from Google Apps Scripts

I have the following function (btw I am new to Google Apps Scripts).我有以下 function (顺便说一句,我是 Google Apps 脚本的新手)。

function testing(){
  let thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(thirtyMinuteWorksheet).getDataRange().getValues()

  let filterData = thisSpreadsheet.filter(function (row,index) {
      return row[11] >= 30
  });


if(filterData.length > 0){
      MailApp.sendEmail({
      to: 'email@gmail.com',
      subject: 'test',
      body: filterData,
      noReply: true
      });
  }

the filter data filters the sheet and returns all the rows that match the logic.过滤器数据过滤工作表并返回与逻辑匹配的所有行。 Id like to be able to email those rows.我希望能够 email 这些行。 if I run this, i get and email with this.如果我运行它,我会得到 email 。 (plus more, shorten it for simplicity). (加上更多,为简单起见缩短它)。

{10=[Ljava.lang.Object;@7f7f43f6, 179=[Ljava.lang.Object;@d387470, 
83=[Ljava.lang.Object;@5daa185f, 54=[Ljava.lang.Object;@39ca2a17, }

i can see this is an object but I couldnt find anything on their API that pointed me in the right direction as to how to covert this results into the actual values我可以看到这是一个 object 但我在他们的 API 上找不到任何东西,这为我指明了如何将这个结果转换为实际值的正确方向

ps ps

Yes, if i console.log filterData i do get the correct objects.是的,如果我 console.log filterData 我确实得到了正确的对象。 any ideas?有任何想法吗?

I figured out a way, since this is an array of objects, I just converted that into and HTML function.我想出了一个办法,因为这是一个对象数组,我只是将其转换为 HTML function。 as below.如下。

I passed in my array results and added some headers as well.我传入了我的数组结果并添加了一些标题。

function makeTableHTML(myArray,tableHeaders) {
    let result = "<table border=1>";
    result += "<tr>";
        for(var j=0; j<tableHeaders.length; j++){
            result += "<td>"+tableHeaders[j]+"</td>";
        }
        result += "</tr>";
    for(var i=0; i<myArray.length; i++) {
        result += "<tr>";
        for(var j=0; j<myArray[i].length; j++){
            result += "<td>"+myArray[i][j]+"</td>";
        }
        result += "</tr>";
    }
    result += "</table>";

    return result;
}

That gave me a way to add the return results into the sendmail function from Google Scripts.这给了我一种将返回结果添加到来自 Google Scripts 的发送邮件 function 的方法。

You can try using join instead since the data you are sending is an array.您可以尝试使用join代替,因为您发送的数据是一个数组。 Convert it to string to properly show the data.将其转换为字符串以正确显示数据。

Sample data:样本数据:

样本数据

Code:代码:

  let filterData = thisSpreadsheet.filter(function (row, index) {
    return row[11] >= 30;
  });

  // Preprocesses filterData before sending mail
  filterData.forEach(function (row, index) {
    filterData[index] = filterData[index].join(",");
  });
  filterData = filterData.join("\n");


  if (filterData.length > 0) {
    MailApp.sendEmail({
      to: 'email@gmail.com',
      subject: 'test',
      body: filterData,
      noReply: true
    });
  }

Sample output:样品 output:

样本输出

One quick answer is to try and wrap filterData with JSON.stringify() , but you can't modify how it shows in the mail.一个快速的答案是尝试使用JSON.stringify()包装filterData ,但您无法修改它在邮件中的显示方式。

Code:代码:

body: JSON.stringify(filterData),

Sample output:样品 output:

样本输出2

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

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