Reputation: 15374
The solution to this may be simple but I cannot seem to think of an easier solution at present. I am using Excel.js and want to add values from an array to a specific set of cells. In this example E6:E25
The problem I face is that I cannot seem to select specific cells, only all of them in a column.
var array = ["4","3","3","3","3","8","2","4","5","2","2","6","4","3","8","5","4","3","2","2"]
workbook.xlsx.readFile('myFile.xlsx')
.then(function() {
var worksheet = workbook.getWorksheet(1);
var column = worksheet.getColumn(5) // Selecting specific column
column.eachCell(function(cell, rownumber){
// eachCell count is 122, ideally not wanting to iterate 122 times
// when only needing 20
switch (cell['_address']) {
case 'E6':
worksheet.getCell(cell['_address']).value = array[0];
break;
case 'E7':
worksheet.getCell(cell['_address']).value = array[1];
break;
case 'E8':
worksheet.getCell(cell['_address']).value = array[2];
break;
// and so on 20 times
default:
}
});
return workbook.xlsx.writeFile('newFile.xlsx');
})
This is awful I know; I am looking to refactor this and make it a lot cleaner and efficient.
Upvotes: 0
Views: 1054
Reputation: 342
Doesn't a simple for loop like this do the same?
var array = ["4","3","3","3","3","8","2","4","5","2","2","6","4","3","8","5","4","3","2","2"];
for (i=0; i<array.length; i++) {
col = i+6;
worksheet.getCell('E'+col).value = array[i];
}
Upvotes: 1