Apache POI - DROP DOWN value removing '00000' from data - apache

I am building dynamic drop down through java code and it is working perfectly fine.
The code I am running is:
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Data Validation");
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint)
dvHelper.createExplicitListConstraint(new String[]{"0000011", "0000021", "0000031"});
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
XSSFDataValidation validation = (XSSFDataValidation)dvHelper.createValidation(
dvConstraint, addressList);
validation.setShowErrorBox(true);
validation.setSuppressDropDownArrow(true);
sheet.addValidationData(validation);
Drop down are coming properly but when I select any one of the value from drop-down 00000 are automatically removed and only 11 is getting displayed, but I want value to be 0000011 to be selected from drop down.
drop value is showing as 0000011 but after selection it display 11. Might be if we can change cell type to text it will help or some other way but how to do it?

I solved this on my own after lot of research. so I thought to post answer to help others.
// setting cell type as string to avoid removing 00000 from drop down
CellStyle textStyle = workbook.createCellStyle();
textStyle.setDataFormat((short)BuiltinFormats.getBuiltinFormat("text"));
sheet.setDefaultColumnStyle(0, textStyle);

Related

Appscript setformula parsing error. but seems working

I'm trying to set formula in google spreadsheet cell using this code
function SetFormuleSettimana(row) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var Settimane = ss.getSheetByName("Settimane");
var cell = Settimane.getRange("I" + row);
cell.setFormulaR1C1('ArrayFormula(Indice(Gruppi!A:G;Confronta(1;(G' + row + '=Gruppi!A:A)*(H' + row + '=Gruppi!B:B);0);5))');
}
The cell seems correctly filled with the formuala =ArrayFormula(Indice(Gruppi!A:G;Confronta(1;(G2=Gruppi!A:A)*(H2=Gruppi!B:B);0);5))
but I get a "Parsing Error", analyzing the formula in spreadsheet the help give correct value.
There is a problem with setting formulas with Apps Script in a language other than English.
To set your formulas programmatically, for the meantime you need to use the corresponding English names of the formulas.

Google Sheets API (v4) - `AutoResizeDimensions` not working

I've got a system that generates and automatically maintains lots of spreadsheets on a Drive account.
Whenever I add data to the sheet I run a 'format' method to pass over and make sure everything is ok.
This generally does things like:
set the default font and size across the sheet
set up the heading row
freeze rows
In addition, I have the code below to make sure the first two columns (index 0 and 1) in the sheet are autoresizing to fit their contents. when I run it though, this element doesn't seem to make a difference. The font, column freezes etc all work.
Other notes:
I only want those 2 columns to auto-resize
the amount of rows in a sheet can vary
this job is appended to the end of several in requestList
My code:
requestList.Requests.Add(new Google.Apis.Sheets.v4.Data.Request()
{
AutoResizeDimensions = new AutoResizeDimensionsRequest()
{
Dimensions = new DimensionRange()
{
SheetId = Convert.ToInt32(sheetId),
Dimension = "COLUMNS",
StartIndex = 0,
EndIndex = 1
}
}
});
var updateRequest = sheetService.Spreadsheets.BatchUpdate(requestList, spreadSheetId);
var updateResponse = updateRequest.Execute();
Could the order which I request the 'format' changes be affecting things maybe? Can anyone help?
As written in the documentation,
the start index is inclusive and the end index is exclusive.
So, For the first two columns, it should be
startIndex = 0,
endIndex = 2

How to write into a particular cell using xlsx npm package

I have to write a value to a particular cell (say the D4 cell) in my xlsm file. I can see the option of
XLSX.writeFile(workbook, 'out.xlsx');
in the XLSX package documentation (writing functions)
But I am not seeing anything to write a value to a particular cell (where should the values which needs to be written passed?). Or, it is not as clear as the example provided to read a particular cell value.
Would be glad if someone could provide me a simple example of snippet.
This is how I read a particular cell value:
if(typeof require !== 'undefined') XLSX = require('C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\xlsx');
var workbook = XLSX.readFile('xlsm');
var first_sheet_name = workbook.SheetNames[0];
var address_of_cell = 'D5';
var worksheet = workbook.Sheets[first_sheet_name];
var desired_cell = worksheet[address_of_cell];
desired_value = (desired_cell ? desired_cell.v : undefined);
console.log('Cell Value is: '+ desired_value);
So to write to a specific cell in a defined sheet - lets say first sheet, you can do:
const XLSX = require('xlsx');
// read from a XLS file
let workbook = XLSX.readFile('test.xls');
// get first sheet
let first_sheet_name = workbook.SheetNames[0];
let worksheet = workbook.Sheets[first_sheet_name];
// read value in D4
let cell = worksheet['D4'].v;
console.log(cell)
// modify value in D4
worksheet['D4'].v = 'NEW VALUE from NODE';
// modify value if D4 is undefined / does not exists
XLSX.utils.sheet_add_aoa(worksheet, [['NEW VALUE from NODE']], {origin: 'D4'});
// write to new file
// formatting from OLD file will be lost!
XLSX.writeFile(workbook, 'test2.xls');
Hope that helps
Modify value in D4
worksheet['D4'].v = 'NEW VALUE from NODE';
This will work only if the cell already defined in the file, but sometimes you will want to write to a new undefined cell.
so, the solution I found for that is:
modify value in new cell- D4
XLSX.utils.sheet_add_aoa(worksheet, [['NEW VALUE from NODE']], {origin: 'D4'});

Create a column dropdown in EPPlus

I have everything else worked out but I want to put a drop down on a cell (range of cells) so that users are forced to select from the list.
I've tried this:
var dd = worksheet.Cells[5, 3, row, 3].DataValidation.AddListDataValidation() as ExcelDataValidationList;
dd.AllowBlank = true;
//Add list here
But I can't find any method or property that allows me to link the list.
How is this done? I can't find any documentation on it.
The correct way is to use the Formula.Values.Add:
dd = worksheet.Cells[5, 4, row, 4].DataValidation.AddListDataValidation() as ExcelDataValidationList;
dd.AllowBlank = true;
dd.Formula.Values.Add("Yes");
dd.Formula.Values.Add("No");

How to disable validating row when it loses focus in Datagridview

I have a datagridview, and I would like to allow the end user to type whateever he wants in the rows and the validation will be done later programmatically.
For example
Column1 : AllowDBNull = true
The user will still be able skip the cell related to Column1 and move to another row (may be to fill it later)
And the validation will be performed once he clicks ValidateButton.
I have been looking to :
DataGridView.CausesValidation = False
But that was unsuccessful.
I was trying also
DataSourceUpdateMode = DataSourceUpdateMode.Never
but couldn't apply this property to DataGridView.Datasource
Here is a bit of my code
'CreateMasterDataTable and CreateDetailDataTable are two functions that return two datatables
Master_DT = CreateMasterDataTable()
Detail_DT = CreateDetailDataTable()
DataSet.Tables.Add(Master_DT)
DataSet.Tables.Add(Detail_DT)
DataSet.Relations.Add("Relation", Master_DT.Columns("City"), Detail_DT.Columns("City"))
Master_BindingSource.DataSource = DataSet
Master_BindingSource.DataMember = Master_DT.TableName
Details_BindingSource.DataSource = Master_BindingSource
Details_BindingSource.DataMember = "Relation"
DataGridView.DataSource = Details_BindingSource
Does anyone have an idea on how to disable the row validation once it loses focus please ?
Thanks