Search code examples
loopsmessage

Using loops to build a message for MailApp


I have a program that steps through a spreadsheet line by line and if TotalSOH <= MinSOH two arrays are built, i.e: PalletNo[j] & SOH[j]. I am able to return each index of PalletNo[j] & SOH[j] as a message (refer the "message" variable in the code below) in my MailApp function by manually cycling through the array so I know the fundamental code works however I would like to create a loop in order for it to do this for me as the array may be a lot larger than two elements in the future.

I would like the message to read (each line);

PalletNo[0] SOH[0]

PalletNo[1] SOH[1]

PalletNo[2] SOH[2], etc, etc

Can somebody help please? Even some direction would be great, not necessarily the answer.

I have tried to create a for loop within the "message" variable and even within the MailApp function itself but this created syntax issues.

Thanks for your help, Chris.

function LowT4KANBANSOHEmail() 
{
  
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName ('T4 Pallet (SOH) (Power Bi)');
  
  var now = new Date();
  var startRow = 1;
  var numRows = sheet.getLastRow();
  var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
  var data = dataRange.getValues();
  var j = 0;  
  
  var PalletNo = Array();
  var SOH = Array();

      for (var i = 0; i < data.length; ++i)
      {
        var row = data[i];  
        var TotalSOH = row[3];
        var MinSOH = row [4];

        if (TotalSOH <= MinSOH){
        PalletNo[j] = row[0];
        SOH[j] = row[3];
        j=j+1;
        }else
        {
        }
      }//End of for loop 
  
  var message = 
  PalletNo[0]+
  " (x"+
  SOH[0]+
  ")"+
  "\n"+
  PalletNo[1]+
  " (x"+
  SOH[1]+
  ")"+
  "\n";
  
  MailApp.sendEmail("emailaddress",
                    "Subject",
                     message
                    );
  
}//End of function

Solution

  • I have solved my own question.

    I am also new to Javascript and to this forum, and thought I should feed the answer back to the community for future reference in case someone else has a similar problem. As a beginner it is hard to know how to contribute, rather than feel you are always asking questions.

    I created a new array, with each element/index representing a line number in the final message. In order to split each line up, I used the .join feature.

    The code is below (some of the syntax may be wrong as I may have accidentally removed some when removing confidential information);

    function Answer() 
    {
      
      var ss = SpreadsheetApp.getActive();
      var sheet = ss.getSheetByName ('Spreadsheet');
      
      var now = new Date();
      var startRow = 1;
      var numRows = sheet.getLastRow();
      var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
      var data = dataRange.getValues();
      var j = 0;  
      
      var PalletNo = Array();
      var SOH = Array();
      var PalletNoSOH = Array();
      
          for (var i = 0; i < data.length; ++i)
    
          {
            var row = data[i];  
            var TotalSOH = row[3];
            var MinSOH = row [4];
    
            if (TotalSOH <= MinSOH){
            PalletNo[j] = row[0];
            SOH[j] = row[3];
            PalletNoSOH[j] = (PalletNo[j].toString() + " (x" + SOH[j].toString() + ")");        
            j=j+1;
            }else
            {
            }
          }//End of for loop 
    
      MailApp.sendEmail("emailaddress",
                        "Subject",
                        PalletNoSOH.join("\n")
                        );
      
    }//End of function