TypeError: Cannot read property '0' of undefined for an array already defined in google scripts

I have a javascript function that should create a table with a dynamic number of rows and 4 columns

Could anyone please tell me why I am getting an error when running this script?

var cellText = data [row] [col]; -> where the error appears

I already looked at the array (data) individually and it is populated.

Here is my javascript and html code below:

function getHtmlTable(table){
 
 var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Emails")
 var Avals = ws.getRange("A1:A").getValues();
var Alast = Avals.filter(String).length;
 var data = ws.getRange(4, 1, (Alast-3), 4).getValues();

  var rangito = ws.getRange("A4");

  // Read table content

  var fontColors = rangito.getFontColors();
  var backgrounds = rangito.getBackgrounds();
  var fontFamilies = rangito.getFontFamilies();
  var fontSizes = rangito.getFontSizes();
  var fontLines = rangito.getFontLines();
  var fontWeights = rangito.getFontWeights();
  var horizontalAlignments = rangito.getHorizontalAlignments();
  var verticalAlignments = rangito.getVerticalAlignments();
  
var col= 1;
var row= 1;

  // Build HTML Table, with inline styling for each cell
  var tableFormat = 'style="border:1px solid black;border-collapse:collapse;text-align:center" border = 1 cellpadding = 5';
  var html = ['<table '+tableFormat+'>'];

   // Populate rows
  for (row= 0;row <= Alast;row++) {
    html.push('<tr style="height: 19px;">');
    for (col= 0 ;col <= 4;col++) {
      // Get formatted data
     
     var cellText = data[row][col];

      var style = 'style="'
                + 'font-family: ' + fontFamilies[row][col]+'; '
                + 'font-size: ' + fontSizes[row][col]+'; '
                + 'font-weight: ' + fontWeights[row][col]+'; '
                + 'background-color: ' + backgrounds[row][col]+'; '
                + 'text-align: ' + horizontalAlignments[row][col]+'; '
                + 'vertical-align: ' + verticalAlignments[row][col]+'; '
                +'"';
      html.push('<td ' + style + '>'
                +cellText
                +'</td>');
    }
   
    html.push('</tr>');
  }
  html.push('</table>');
  
  return html.join('');
}

Upvotes: 2

Views: 64

Answers (1)

KingsInnerSoul
KingsInnerSoul

Reputation: 1382

var data = ws.getRange(4, 1, (Alast-3), 4).getValues();

getRange: getRange(row, column, numRows, numColumns)

  • row: The starting row index of the range; row indexing starts with 1.
  • column: The starting column index of the range; column indexing starts with 1.
  • numRows: The number of rows to return.
  • numColumns: The number of columns to return.

So data is: getRange(row=4, column=1, numRows=X, numColumns=4) which makes data an X by 4 2D array.

If your for loop you are running through the 2D data array, and calling it, data[row][col], where call will get up to 4, because the limiting condition is col <= 4. So just remove the = sign to have col < 4 or change it to col <= 3. Do not forget, most programming languages, except MatLAB and a few selected few, have arrays start at 0, not 1, so the index is zero based, and should be called with -1 from the length.

Upvotes: 2

Related Questions