Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

google apps script - How do I email an update to the table results for specific columns that meet the criteria less than 0?

I have a table that I would like to send an email for based on the values of columns E(table below). The Columns I would like to append to the email are A,D,E,F. The Rows I would like to include are those that have dropped below 0 from column E. How do I amend my function to allow for this change?

enter image description here

function EmailUpdate() {
    var hty = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Historical')
    var data = hty.getRange("A1:F" + hty.getLastRow()).getValues().filter(([,e], i) => i == 0 || e < 0);
        
    var TABLEFORMAT = 'cellspacing="2" cellpadding="2" dir="ltr" border="1" style="width:100%;table-layout:fixed;font-size:10pt;font-family:arial,sans,sans-serif;border-collapse:collapse;border:1px solid #ccc;font-weight:normal;color:black;background-color:white;text-align:left;text-decoration:none;font-style:normal;'
    var htmltable = '<table ' + TABLEFORMAT + ' ">';
   
    for (row = 0; row < data.length; row++) {

        htmltable += '<tr>';

        for (col = 0; col < data[row].length; col++) {
            if (data[row][col] === "" || 0) {
                htmltable += '<td>' + 'None' + '</td>';
            } else
            if (row === 0) {
                htmltable += '<th>' + data[row][col] + '</th>';
            } else {
                htmltable += '<td>' + data[row][col] + '</td>';
            }
        }

        htmltable += '</tr>';
    }

    htmltable += '</table>';
    Logger.log(data);
    Logger.log(htmltable);
    MailApp.sendEmail(Session.getActiveUser().getEmail(), 'Email Report', '' ,{
        htmlBody: htmltable
        
    })
}
question from:https://stackoverflow.com/questions/66068662/how-do-i-email-an-update-to-the-table-results-for-specific-columns-that-meet-the

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Replace:

var data = hty.getRange("A1:F" + hty.getLastRow()).getValues().filter(([,e], i) => i == 0 || e < 0);

With:

var data = hty.getRange("A1:F"+hty.getLastRow()).
           getDisplayValues().
           map(([a,,,d,e,f]) => [a,d,e,f]).
           filter((r,i)=>r[2].includes("-") || i==0 );

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...