Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
Generate number of columns dynamically

I've got these two functions working fine, but I'm realizing the number of columns should be generated dynamically. Would you be able to help me do that? Thanks! ^_^

function getTableData(){
  var ss = SpreadsheetApp.openByUrl(url);
  var ws = ss.getSheetByName("Data");
  var lr = ws.getLastRow();
  var lc = ws.getLastColumn();
  var data = ws.getRange(2,1,lr-1,lc).getDisplayValues();
  Logger.log(data);
  return data;
}

function generateTable(dataArray){

var tbody = document.getElementById("table-body");

  dataArray.forEach(function(r){

    var row = document.createElement("tr");
    var col1 = document.createElement("td");
    col1.textContent = r[0];
    var col2 = document.createElement("td");
    col2.textContent = r[1];
    var col3 = document.createElement("td");
    col3.textContent = r[2];
    row.appendChild(col1);
    row.appendChild(col2);
    row.appendChild(col3);
    tbody.appendChild(row);
  });

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Store your dynamic column length in a variable and create a nested loop

Sample:

var colNumber = 5; 
dataArray.forEach(function(r){
   var row = document.createElement("tr");
   for(var i = 0; i < colNumber; i++){
     var col = document.createElement("td");
     col.textContent = r[i];
     row.appendChild(col);
   }
   tbody.appendChild(row);
});
about 4 years ago · Juan Pablo Isaza Report

0

you are near, just put loop on till the no of columns you have lc, keep adding td in one row and than after loop add that tr to main table

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!