I e across this use case where I have to give outline borders for a range of cells (Start: A1 to C5 and End: A4 to C5) and I searched google for results but not getting properly. I want an outer border for a particular range and I used the below snippet code but not working
Code:
const workbook = new Workbook();
const loeTermsandConditionsWorksheet1 = workbook.addWorksheet('(dummy) page',{views:[
{activeCell: 'A1', showGridLines:true}
]})
//OUTER BRODER STARTS
const createOuterBorder = (loeTermsandConditionsWorksheet1, start = {row: 1, col: 5}, end = {row: 4, col: 5}, borderWidth = 'medium') => {
const borderStyle = {
style: borderWidth
};
for (let i = start.row; i <= end.row; i++) {
const leftBorderCell = loeTermsandConditionsWorksheet1.getCell(i, start.col);
const rightBorderCell = loeTermsandConditionsWorksheet1.getCell(i, end.col);
leftBorderCell.border = {
...leftBorderCell.border,
left: borderStyle
};
rightBorderCell.border = {
...rightBorderCell.border,
right: borderStyle
};
}
for (let i = start.col; i <= end.col; i++) {
const topBorderCell = loeTermsandConditionsWorksheet1.getCell(start.row, i);
const bottomBorderCell = loeTermsandConditionsWorksheet1.getCell(end.row, i);
topBorderCell.border = {
...topBorderCell.border,
top: borderStyle
};
bottomBorderCell.border = {...bottomBorderCell.border, bottom: borderStyle};
}
};
//OUTER BRODER END
this is what I am expecting:
but not getting the correct result.
I e across this use case where I have to give outline borders for a range of cells (Start: A1 to C5 and End: A4 to C5) and I searched google for results but not getting properly. I want an outer border for a particular range and I used the below snippet code but not working
Code:
const workbook = new Workbook();
const loeTermsandConditionsWorksheet1 = workbook.addWorksheet('(dummy) page',{views:[
{activeCell: 'A1', showGridLines:true}
]})
//OUTER BRODER STARTS
const createOuterBorder = (loeTermsandConditionsWorksheet1, start = {row: 1, col: 5}, end = {row: 4, col: 5}, borderWidth = 'medium') => {
const borderStyle = {
style: borderWidth
};
for (let i = start.row; i <= end.row; i++) {
const leftBorderCell = loeTermsandConditionsWorksheet1.getCell(i, start.col);
const rightBorderCell = loeTermsandConditionsWorksheet1.getCell(i, end.col);
leftBorderCell.border = {
...leftBorderCell.border,
left: borderStyle
};
rightBorderCell.border = {
...rightBorderCell.border,
right: borderStyle
};
}
for (let i = start.col; i <= end.col; i++) {
const topBorderCell = loeTermsandConditionsWorksheet1.getCell(start.row, i);
const bottomBorderCell = loeTermsandConditionsWorksheet1.getCell(end.row, i);
topBorderCell.border = {
...topBorderCell.border,
top: borderStyle
};
bottomBorderCell.border = {...bottomBorderCell.border, bottom: borderStyle};
}
};
//OUTER BRODER END
this is what I am expecting:
but not getting the correct result.
Share Improve this question edited Aug 24, 2021 at 10:43 R. Richards 25.2k10 gold badges66 silver badges65 bronze badges asked Aug 24, 2021 at 6:32 syedali SDsyedali SD 871 gold badge2 silver badges11 bronze badges1 Answer
Reset to default 4I don't know what your incorrect result is. But i use this for outer borders and it works as well! Original Link: ExcelJS Outline border issue
const createOuterBorder = (worksheet, start = {row: 1, col: 1}, end = {row: 1, col: 1}, borderWidth = 'medium') => {
const borderStyle = {
style: borderWidth
};
for (let i = start.row; i <= end.row; i++) {
const leftBorderCell = worksheet.getCell(i, start.col);
const rightBorderCell = worksheet.getCell(i, end.col);
leftBorderCell.border = {
...leftBorderCell.border,
left: borderStyle
};
rightBorderCell.border = {
...rightBorderCell.border,
right: borderStyle
};
}
for (let i = start.col; i <= end.col; i++) {
const topBorderCell = worksheet.getCell(start.row, i);
const bottomBorderCell = worksheet.getCell(end.row, i);
topBorderCell.border = {
...topBorderCell.border,
top: borderStyle
};
bottomBorderCell.border = {
...bottomBorderCell.border,
bottom: borderStyle
};
}
};
I use it like this:
createOuterBorder(
worksheet,
(start = { row: 2, col: 2 }),
(end = { row: 5, col: 5 })
);
The result (see the image) is start on cell 2, row 2 (index here is 1 not 0!) and end on cell 5, row 5.
I see your code looks almost like mine. Well, maybe you just called the method wrong?
Greetings, Florian