简体   繁体   中英

Making table in google apps script from array

function arrayMail(){
    var ss=SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('Sheet1');
    var startRow = 2;
    var lastRow = sheet.getLastRow();
    var lastColumn = sheet.getLastColumn();
    var range = sheet.getRange(startRow,1,lastRow,lastColumn);
    var data = range.getValues();
    var array = [];
    for(var i=0;i<data.length;++i){
        var row = data[i];
        var rowTemp=[]; //create a blank array that will hold only the desired    columns in this row
        for (var j=0; j<row.length; j++)//iterates through columns
        {
           if(j==4||j==3||j==1||j==5){continue;} 
           rowTemp.push(row[j])

        }    
        var emailPos = row[3];
        var date = row[2];   
        if(emailPos =='example4@gmail.com'&& date == 0){
            array.push(rowTemp);
        }
    }
    GmailApp.sendEmail('example1@gmail.com','This is a test',array.join('\n\n'));
}

Above script I used for me sending email when date count is 0 and the email become the reference who to email. I wanted to make the array (which is in plain text if using script above) become a table format. I'm stuck with the table formatting. The question is, how do I make it in table format here? Beginner level in scripting


After reading from book/examples, I came up with below script. But it doesn't work as intended;

   function arrayMail(){
       var ss=SpreadsheetApp.getActiveSpreadsheet();
       var sheet = ss.getSheetByName('Sheet1');
       var startRow = 2;
       var lastRow = sheet.getLastRow();
       var lastColumn = sheet.getLastColumn();
       var range = sheet.getRange(startRow,1,lastRow,lastColumn);
       var data = range.getValues();
       var tableStart = "<table border=\"1\"><tr>";
       var tableEnd = "</tr></table>";
       var array = [];
       for(var i=0;i<data.length;++i){
           var row = data[i];
           var rowTemp=[]; //create a blank array that will hold only the desired columns in this row
       for (var j=0; j<row.length; j++)//iterates through columns
       {
           if(j==4||j==3||j==1||j==5){continue;} 
           rowTemp.push('<td>'+row[j]+'</td>');      
       }    
       var emailPos = row[3];
       var date = row[2];   
           if(emailPos =='example4@gmail.com'&& date == 0){
           array.push(rowTemp);
      }
    }
    var HTMLbody = tableStart +array.join('')+tableEnd;
    GmailApp.sendEmail('example@gmail.com','This is a test',HTMLbody,{htmlBody:HTMLbody});
    }

Result:

结果的形象

What I wanted to be is like;

Infusion Pump 0
Infusion Pump 0

This is not a full answer but the code below translates the array to the string you asked about in your Google+ post...

var array = [
  ['Infusion Pump', 'B', 'C', 'D', 'E', 'F'],
  ['Another Pump', 'B', 'C', 'D', 'E', 'F'],
  ['Defib', 'B', 'C', 'D', 'E', 'F']
];

var str = '';

for (var i = 0; i < array.length; i++) {

  str = str + '<tr>';

  for (var j = 0; j < array[i].length; j++) {

    str = str + '<td>' + array[i][j] + '</td>';

  }

  str = str + '</tr>';
}

console.log(str);

Based on SamScholefield's answer I did modified his code a bit to suit my need and here is the final code:

function arrayMail(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var startRow = 2;
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var range = sheet.getRange(startRow,1,lastRow,lastColumn);
var data = range.getValues();
var array = [];
for(var i=0;i<data.length;++i){
    var row = data[i];
    var rowTemp=[]; //create a blank array that will hold only the desired columns in this row
    for (var j=0; j<row.length; j++){    //iterates through columns
         if(j==4){continue;} 
         rowTemp.push(row[j]);         
        }    
    var emailPos = row[3];
    var date = row[2];   
      if(emailPos =='example1@gmail.com'&& date == -28){
      array.push(rowTemp);
      }
 }
var str = '';
var bodyEnd = '</table>';
var header = '<table border =1><tr><th>Message</th><th>Date Remind</th><th>Days Left</th><th>Email Address</th><th>Status</th></tr>';
for (var k = 0; k < array.length; k++) {
    str = str + '<tr>';
    for (var l = 0; l < array[k].length; l++) {
    str = str + '<td>' + array[k][l] + '</td>';
        }
    str = str + '</tr>';
var msg = header+str+bodyEnd;
}
GmailApp.sendEmail(Session.getActiveUser().getEmail(),'This is a test',msg,    {htmlBody:msg});
};

Result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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