user8512043
user8512043

Reputation: 1157

Auto Fill Row On Content Length

I've shifted to ExcelJs from Sheet JS as I didn't find the option to adjust row values in each column. I've managed to do it with ExcelJs but still there's one issue that I am trying to resolve. So here's what I had earlier with Sheet JS:

Excel Data

This is what I've now with ExcelJs:

Excel Data 2

Now the thing is, when I download the excel with ExcelJs, by default it sets the row data like in the image 1. So you can see the Permissions column shrinks. When I double click on the columns and rows in the excel file after download, I get the row data as image 2. I like to have the row data as image 2 by default during the download. Is it something possible to do? I used the following code snippet:

const workbookNew = new ExcelJS.Workbook();
const worksheetNew = workbookNew.addWorksheet('User Data');

// Add headers
const headers = Object.keys(newdata[0]);
worksheetNew.addRow(headers);

// Add data
// newdata is the jSon data
newdata.forEach((item) => {
  const row: any = [];
  headers.forEach((header) => {
    row.push(item[header]);
  });
  worksheetNew.addRow(row);
});

 let rowIndex = 1;
for (rowIndex; rowIndex <= worksheetNew.rowCount; rowIndex++) {
  worksheetNew.getRow(rowIndex).alignment = { vertical: 'middle', horizontal: 'left', wrapText: true }; // I believe, this is where it sets the row data
 }

workbookNew.xlsx.writeBuffer().then((buffer: any) => {
  const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
  FileSaver.saveAs(blob, `${excelFileName}.xlsx`);
});

Upvotes: 0

Views: 74

Answers (0)

Related Questions