Create dataValidation from named list in different sheet - java

i'm trying to create a dataValidation object with a List Constraint being the name of a ranged single contiguous column .... The list which i would pass on as argument to the createFormulaListConstraint() is in a different sheet than where i would like to display the dataValidation.
The following is the code that i have so far and when i try to run it a FormulaParseException: Specified name range does not exist in the current workbook is being thrown:
Name name = dctmWorkSheet.getWorkbook().createName();
name.setSheetIndex(1);
name.setNameName(cell.getStringCellValue());
name.setRefersToFormula(getRangeRefersToFormula(valueAssSheet, firstRow, rowIndex, insertCol));
validationHelper = dctmWorkSheet.getDataValidationHelper();
//------ THIS IS THE LINE RESPONSIBLE FOR THE EXCEPTION -------
constraint = validationHelper.createFormulaListConstraint(name.getNameName());
dataValidation = validationHelper.createValidation(constraint, cral);
dataValidation.setEmptyCellAllowed(true);
dataValidation.setShowErrorBox(true);
dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
dataValidation.createErrorBox("Error", "Please use Pick List Value");
dctmWorkSheet.addValidationData(dataValidation);
Thanks for any help or feedback......

Actually managed to solve my problem by creating an additional name to sheetIndex(0) and reference it to the name in sheetIndex(1)....
The following is the code which works as desired:
Name name = dctmWorkSheet.getWorkbook().createName();
name.setSheetIndex(1);
name.setNameName(cell.getStringCellValue());
name.setRefersToFormula(getRangeRefersToFormula(valueAssSheet, firstRow, rowIndex, insertCol));
Name name2 = dctmWorkSheet.getWorkbook().createName();
name2.setSheetIndex(0);
name2.setNameName(name.getNameName());
name2.setRefersToFormula(name.getRefersToFormula());
validationHelper = dctmWorkSheet.getDataValidationHelper();
constraint = validationHelper.createFormulaListConstraint(name2.getNameName());
dataValidation = validationHelper.createValidation(constraint, cral);
dataValidation.setEmptyCellAllowed(true);
dataValidation.setShowErrorBox(true);
dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
dataValidation.createErrorBox("Error", "Please use Pick List Value");
dctmWorkSheet.addValidationData(dataValidation);

Related

Nested table docx4j

I need to push a second table in my main table.
I use the main table to format correct the text: I'm creating a class in java for generate Curriculum, so I need a main table to create a good template
I try to push a
Tbl element in a Tc (cell element) of main table, but Word gives me error about a wrong template. It ask me if I want open anyway the document: It show correctly the nested table, but I don't want that the error is displayed.
ObjectFactory factory = Context.getWmlObjectFactory();
Tbl mainTable = TblFactory.createTable(2, 2, cellWidthTwips );
List<Object> rows = table.getContent();
Tr row = (Tr) rows.get(0);
List<Object> cells = row.getContent();
Tc cell = (Tc) cells.get(0);
Tbl nestedTable = TblFactory.createTable(1, 5, widthTips/columns );
cell.getContent().add(nestedTable);
I tried also
Tbl nestedTable2 = factory.createTbl();
Where I wrong?
Assuming MainDocumentPart mdp:
int widthTwips = 4788;
Tbl mainTable = TblFactory.createTable(2, 2, widthTwips);
List<Object> rows = mainTable.getContent();
Tr row = (Tr) rows.get(0);
List<Object> cells = row.getContent();
Tc cell = (Tc) cells.get(0);
Tbl nestedTable = TblFactory.createTable(1, 2, 2000);
// You'll get an error in Word if you don't have an empty <p/> after the nested table.
// Since TblFactory.createTable automatically added an empty <p>, add the table before it
cell.getContent().add(0, nestedTable);
mdp.getContent().add(mainTable);
See the explanatory comment in the code snippet.

In Excel file , How to set cell name using Java

I have an excel file. I want to set a unique name to a particular cell.
But there is no method called "cell.setName".
Regarding official documentation:
Named Range is a way to refer to a group of cells by a name. Named Cell is a degenerate case of Named Range in that the 'group of cells' contains exactly one cell. You can create as well as refer to cells in a workbook by their named range. When working with Named Ranges, the classes org.apache.poi.ss.util.CellReference and org.apache.poi.ss.util.AreaReference are used.
Note: Using relative values like 'A1:B1' can lead to unexpected moving of the cell that the name points to when working with the workbook in Microsoft Excel, usually using absolute references like '$A$1:$B$1' avoids this, see also this discussion.
Creating Named Range / Named Cell
// setup code
String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet(sname);
sheet.createRow(0).createCell(0).setCellValue(cvalue);
// 1. create named range for a single cell using areareference
Name namedCell = wb.createName();
namedCell.setNameName(cname + "1");
String reference = sname+"!$A$1:$A$1"; // area reference
namedCell.setRefersToFormula(reference);
// 2. create named range for a single cell using cellreference
Name namedCel2 = wb.createName();
namedCel2.setNameName(cname + "2");
reference = sname+"!$A$1"; // cell reference
namedCel2.setRefersToFormula(reference);
// 3. create named range for an area using AreaReference
Name namedCel3 = wb.createName();
namedCel3.setNameName(cname + "3");
reference = sname+"!$A$1:$C$5"; // area reference
namedCel3.setRefersToFormula(reference);
// 4. create named formula
Name namedCel4 = wb.createName();
namedCel4.setNameName("my_sum");
namedCel4.setRefersToFormula("SUM(" + sname + "!$I$2:$I$6)");
Link: https://poi.apache.org/components/spreadsheet/quick-guide.html#NamedRanges
// setup code
String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet(sname);
sheet.createRow(0).createCell((short) 0).setCellValue(cvalue);
// 1. create named range for a single cell using areareference
Name namedCell = wb.createName();
namedCell.setNameName(cname);
String reference = sname+"!A1:A1"; // area reference
namedCell.setRefersToFormula(reference);
From : http://poi.apache.org/components/spreadsheet/quick-guide.html#NamedRanges

Apache POI : Update cells in a named range

I am using Apache POI library to read/write to xlsx. In my original xlsx, I have a namedrange "XYZ".
I want to update the values of cells within this name range. How can I do that?
I did this:
XSSFName name = workbook.getName("XYZ");
String formula = name.getRefersToFormula();
System.out.println("Formula = " + formula);
Now, I dont know how to get a handle to individual cell in this named range.
Can anyone please point me to the correct API that I can use?
Rgds
Sapan
There is an example from the Busy Developers' Guide for retrieving the cells in the range. Then you can use Cell.setCellValue() to update.
// setup code
String cname = "TestName";
Workbook wb = getMyWorkbook(); // retrieve workbook
// retrieve the named range
int namedCellIdx = wb.getNameIndex(cname);
Name aNamedCell = wb.getNameAt(namedCellIdx);
// retrieve the cell at the named range and test its contents
AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
CellReference[] crefs = aref.getAllReferencedCells();
for (int i=0; i<crefs.length; i++) {
Sheet s = wb.getSheet(crefs[i].getSheetName());
Row r = s.getRow(crefs[i].getRow());
Cell c = r.getCell(crefs[i].getCol());
// extract the cell contents based on cell type etc.
}

Retrieve all from HBase except the value of a particular column family

I am writing a Java application that retrieves and presents data from an HBase database.
When writing the Get method for retrieving a row, I would like to get all the data for that row, but exclude the value for a particular column family (the "big" column family). Note: I need to retrieve the column names (qualifiers?) in that family because they contain valuable information.
Is it possible to write a Filter for that?
I have two solutions. The first one does not work and the second one is quite slow.
First solution (using a composite filter):
HTable table = getTable();
Get get = new Get(row);
FilterList filter = new FilterList(FilterList.Operator.MUST_PASS_ONE);
FilterList subFilterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
subFilterList.addFilter(new KeyOnlyFilter());
subFilterList.addFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
filter.addFilter(subFilterList);
filter.addFilter(new FamilyFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
get.setFilter(filter);
retrieveAndUseResult(table, get);
This solution works neither conceptually nor in practise - but perhaps I am on the right track using a composite FilterList?
Second solution (using two gets):
HTable table = getTable();
Get get = new Get(row);
// exclude the entire "big" column family
get.setFilter(new FamilyFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
retrieveAndUseResult(table, get);
Get get2 = new Get(row);
// include the "big" column family, but only retrieve the key
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterList.addFilter(new KeyOnlyFilter());
filterList.addFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
get2.setFilter(filterList);
retrieveAndUseResult(table, get2);
This works, but I would favor having to do only one get.
I ended up using a variant of the second solution - using two gets. But I used a batch get list to speed it up.
The code:
HTable table = getTable();
Get get = new Get(row);
// exclude the entire "big" column family
get.setFilter(new FamilyFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
Get get2 = new Get(row);
// include the "big" column family, but only retrieve the key
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterList.addFilter(new KeyOnlyFilter());
filterList.addFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
get2.setFilter(filterList);
List<Get> getList = new ArrayList<Get>();
getList.add(get);
getList.add(get2);
retrieveAndUseResults(table, getList);

JMesa: pre-sorting a table

I am creating a multiple column JMesa table. I want to pre-sort the table on a specified column. How can I achieve this?
You have to call addSort() on SortSet, then associate the SortSet with the TableModel. Sample code:
SortSet mySortSet = new SortSet() {};
mySortSet.addSort("your column property here", Order.ASC);
Limit limit = TableModelUtils.getLimit("table model id here", request, itemsList);
limit.setSortSet(mySortSet);
model.setLimit(limit);

Categories