Writing to blank cell doesn't seem to work.
I am using the below code to update cells in a excel worksheet using XLSX in node. I am filtering the array and using the below in an IF loop based on some conditions. It all works correctly and I am able to write to my desired cells wherever they are in the row index, however, one of the conditions is that if a cell in that column is '0' then return it BUT there was problems with returning blank cells which I have managed to fix too, I can get the array with a '0' value for the cells which are blank.
The problem is coming in when writing to the worksheet, it can write to all rows where there is a '0' value but it fails to update an empty cell to a '1' value like the rest of the cells udpate. It can even iterate over that blank cell and change values below it but when the cell is blank it just fails to write.
The array looks like,
label label label label
0 0 0 1
0 0 0 // <- THIS: doesn't write to 1 if the cell is empty
0 0 0 1
0 0 0 0 // <- if it has a 0 value it will write to 1
0 0 0 1
const workbook = XLSX.readFile("" + appDir + "\\tmp\\feedbacklist.xlsx", {
type: "binary",
cellDates: true,
dateNF: "yyyy-mm-dd;@",
sheetStubs: true
});
const SheetNameList = workbook.SheetNames;
var rows = XLSX.utils.sheet_to_json(workbook.Sheets[SheetNameList[0]], {
raw: false,
defval: '0'
});
var index = rows.indexOf(filteredRows[x]);
console.log("Index: " + index);
const updateFeedbackCell = raw[XLSX.utils.encode_cell({
r: index + 1,
c: 13 // in the example array above the are only 4 columns but i have 13 in dev
})];
updateFeedbackCell.v = '1';
There are alot of posts regarding the blank cells and I have done whats suggested to a point where I can read them using defval: '0' and sheetStubs: true, but I get the '0' value back in the array and just cant write back to a blank cell.
I haven't found quite the same issue, please forgive me if I am wrong posting here. Any ideas please?
Its just a workaround, but I solved a similiar problem by creating a large array filled with "null". After this I used the XLSX.utils.aoa_to_sheet(arr) command and had a virtuell map where I copy all my data in. With this I could even fill empty cells with information. After saving the whole construction the file get scaled down for just the filled cells....
For the original problem, you also need updateFeedbackCell.t = 'n';
For future code, use XLSX.utils.sheet_add_aoa to write values. If a cell is marked as type z, setting the value won't change its status as a stub cell. sheet_add_aoa will automatically set the type.