Skip to content

Commit 187ac27

Browse files
addComment API refactorings to processDiff
1 parent a25fbcd commit 187ac27

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ local.properties
8080
# Custom
8181
!apps/*
8282
/target/
83+
*.xls*

src/main/java/edu/abhi/poi/excel/ExcelDiffChecker.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public static void main(String[] args) {
2525
String FILE_NAME2 = args[1];
2626
commentFlag = args.length == 2;
2727

28-
String RESULT_FILE = FILE_NAME1.substring(0, FILE_NAME1.lastIndexOf(".")) + " vs " +
29-
FILE_NAME2;
28+
String RESULT_FILE = FILE_NAME1.substring(0, FILE_NAME1.lastIndexOf(".")) + " vs " + FILE_NAME2;
3029

3130
boolean success = true;
3231

@@ -83,14 +82,14 @@ public static void main(String[] args) {
8382
if(cell1 == null)
8483
cell1 = row1.createCell(columnIndex);
8584

86-
Utility.addComment(resultWorkbook, sheet1, rowIndex, cell1, "SYSTEM", cell2);
85+
Utility.processDiff(cell1, cell2, commentFlag);
8786
}
8887
} else if(Utility.hasNoContent(cell2)) {
8988
if(Utility.hasContent(cell1)) {
90-
Utility.addComment(resultWorkbook, sheet1, rowIndex, cell1, "SYSTEM", null);
89+
Utility.processDiff(cell1, null, commentFlag);
9190
}
9291
} else if(!cell1.getRawValue().equals(cell2.getRawValue())) {
93-
Utility.addComment(resultWorkbook, sheet1, rowIndex, cell1, "SYSTEM", cell2);
92+
Utility.processDiff(cell1, cell2, commentFlag);
9493
}
9594
}
9695
}

src/main/java/edu/abhi/poi/excel/Utility.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
import org.apache.poi.ss.usermodel.DateUtil;
1111
import org.apache.poi.ss.usermodel.Drawing;
1212
import org.apache.poi.ss.usermodel.Sheet;
13-
import org.apache.poi.ss.usermodel.Workbook;
1413
import org.apache.poi.xssf.usermodel.XSSFCell;
1514

1615
/**
17-
* @author abhis
16+
* @author abhishek sarkar
1817
*
1918
*/
2019
public class Utility {
@@ -28,33 +27,35 @@ public static boolean hasContent(XSSFCell cell) {
2827
}
2928

3029
@SuppressWarnings("rawtypes")
31-
public static void addComment(Workbook workbook, Sheet sheet, int rowIndex, XSSFCell cell1, String author, XSSFCell cell2) throws Exception {
30+
public static void processDiff(XSSFCell cell1, XSSFCell cell2, boolean commentFlag) throws Exception {
31+
32+
Sheet sheet = cell1.getSheet();
3233

3334
System.out.println(String.format("Diff at cell[%s] of sheet[%s]", cell1.getReference(), sheet.getSheetName()));
3435

3536
ExcelDiffChecker.diffFound = true;
3637

37-
if(!ExcelDiffChecker.commentFlag) {
38+
if(!commentFlag) {
3839
System.out.println(String.format("Expected: [%s], Found: [%s]", getCellValue(cell1), getCellValue(cell2)));
3940
return;
4041
}
4142

42-
CreationHelper factory = workbook.getCreationHelper();
43+
CreationHelper factory = sheet.getWorkbook().getCreationHelper();
4344
//get an existing cell or create it otherwise:
4445

4546
ClientAnchor anchor = factory.createClientAnchor();
4647
//i found it useful to show the comment box at the bottom right corner
4748
anchor.setCol1(cell1.getColumnIndex() + 1); //the box of the comment starts at this given column...
4849
anchor.setCol2(cell1.getColumnIndex() + 3); //...and ends at that given column
49-
anchor.setRow1(rowIndex + 1); //one row below the cell...
50-
anchor.setRow2(rowIndex + 5); //...and 4 rows high
50+
anchor.setRow1(cell1.getRowIndex() + 1); //one row below the cell...
51+
anchor.setRow2(cell1.getRowIndex() + 5); //...and 4 rows high
5152

5253
Drawing drawing = sheet.createDrawingPatriarch();
5354
Comment comment = drawing.createCellComment(anchor);
5455

5556
//set the comment text and author
5657
comment.setString(factory.createRichTextString("Found " + Utility.getCellValue(cell2)));
57-
comment.setAuthor(author);
58+
comment.setAuthor("SYSTEM");
5859

5960
cell1.setCellComment(comment);
6061
}

0 commit comments

Comments
 (0)