Cloning/Duplicating a "TEMPLATE" worksheet #2525
msedek
started this conversation in
Ideas & Proposals
Replies: 3 comments
-
when done in react the file comes out corrupted |
Beta Was this translation helpful? Give feedback.
0 replies
-
var Excel = require("exceljs");
var workbook = new Excel.Workbook();
workbook.xlsx
.readFile("docs/0000.xlsx")
.then(async () =>
{
const worksheet = workbook.getWorksheet(1);
duplicateWorksheet(workbook, worksheet, `name`);
});
function duplicateWorksheet(workbook, originalWorksheet, name)
{
const newWorksheet = workbook.addWorksheet(name);
// Copy Model
newWorksheet.model = Object.assign({},
{
...originalWorksheet.model,
name,
...{model: {name}}
});
// Deep Copy Rows & Cells
originalWorksheet.eachRow((row, rowNumber) =>
{
const newRow = newWorksheet.getRow(rowNumber);
row.eachCell({ includeEmpty: true }, (cell, colNumber) =>
{
const newCell = newRow.getCell(colNumber);
newCell.value = cell.value;
newCell.style = Object.assign({}, cell.style);
});
});
// Copy Merged Cells
originalWorksheet.model.merges.forEach(merge =>
{
newWorksheet.mergeCells(merge);
});
return newWorksheet;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
request for the author to add to the guide the method to clone a worksheet as the feature is present but not documented and it is such a useful feature... often times we have a template we need to duplicate over and over in a workbook and the process to do that is not described in the documentation resulting in hours and hours lost searching and finding undocumented and bad implemented stuff in the issues and there is no other source of information about but here, so I want to contribute with the correct way (or AT LEAST what I have found) is working perfectly and for massive worksheet clonation with 0 issue at the moment
Beta Was this translation helpful? Give feedback.
All reactions