Goal: I'm trying to add a feature to my Excel 2016 VSTO plugin. The feature will get 1 column from the active sheet, and iterate over it changing the background color based on string length.
Problem: I'm having trouble getting string length from the cells. I can not figure out the proper syntax I currently have var count = row.Item[1].Value2.Text.Length;
Code: Here is what I have
public void CharacterLengthCheck(int length = 24, int Column = 3)
{
Worksheet sheet = Globals.ThisAddIn.Application.ActiveSheet;
var RowCount = sheet.UsedRange.Rows.Count;
Range column = sheet.Range[sheet.Cells[1, Column], sheet.Cells[RowCount, Column]];
foreach (Range row in column)
{
var count = row.Item[1].Value2.Text.Length;
if (count > length)
{
row.Item[1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
else
{
row.Item[1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
}
}
}
I think your problem is here:
row.Item[1].Value2.Text.Length
The length function is Len(x), so try Len(row.Item[1].Value2.Text)
Thank you to #Jeeped for their advice on using conditional formatting. However the answer to my original question is.
Change
var count = row.Item[1].Value2.Text.Length;
to
string text = (row.Item[1].Value).ToString();
var count = text.length;
I believe this is because value is dynamic and needs to be explicitly cast to string
Related
I'm trying to filter the last column on a worksheet but I can't seem to get the Index of the column. To be clear, I need the index relative to the worksheet, no the range. I used VisibleView to find the Column, but there may be hidden rows, so my plan is to then load that column via getRangeByIndexes but I need the relative columnIndex to the worksheet.
I've tried a bunch of variations of the below, but I either get Object doesn't support 'getColumn' or columnIndex is undefined
Note: In the below example I've hardcoded 7 as that will be the last column relative to the VisibleView (Columns and rows are already hidden), but I'd like this to by dynamic for other functions and just returnthe "last visible column index".
var ws = context.workbook.worksheets.getActiveWorksheet()
var visible_rng = ws.getUsedRange(true).getVisibleView()
visible_rng.load(["columnCount", "columnIndex"])
await context.sync();
console.log('visible_rng.columnIndex')
console.log(visible_rng.getCell(0,7).columnIndex)
console.log(visible_rng.getColumn(7).columnIndex)
Well this method seems a bit hacky, please share if you know a better way! But, first thing I found was that getVisibleView only metions rows in the Description.
Represents the visible rows of the current range.
I decided to try getSpecialCells and was able to load the address property. I then had to use split and get the last column LETTER and convert this to the Index.
I also wanted the columnCount but this wasn't working w/ getSpecialCells so I polled that from getVisibleView and return an Object relating to Visible Views that I can build on the function later if I need more details.
Here it is:
async function Get_Visible_View_Details_Obj(context, ws) {
var visible_rng = ws.getUsedRange(true).getSpecialCells("Visible");
visible_rng.load("address")
var visible_view_rng = ws.getUsedRange(true).getVisibleView()
visible_view_rng.load("columnCount")
await context.sync();
var Filter_Col_Index = visible_rng.address
var Filter_Col_Index = Filter_Col_Index.split(",")
var Filter_Col_Index = Filter_Col_Index[Filter_Col_Index.length - 1]
var Filter_Col_Index = Filter_Col_Index.split("!")[1]
if (Filter_Col_Index.includes(":") == true) {
var Filter_Col_Index = Filter_Col_Index.split(":")[1]
}
var Filter_Col_Index = Get_Alpha_FromString(Filter_Col_Index)
var Filter_Col_Index = Get_Col_Index_From_Letters(Filter_Col_Index)
var Filter_Col_Index_Obj = {
"last_col_ws_index": Filter_Col_Index,
"columnCount": visible_view_rng.columnCount,
}
return Filter_Col_Index_Obj
}
Helper Funcs:
function Get_Alpha_FromString(str) {
return str.replace(/[^a-z]/gi, '');
}
function Get_Col_Index_From_Letters(str) {
str = str.toUpperCase();
let out = 0, len = str.length;
for (pos = 0; pos < len; pos++) {
out += (str.charCodeAt(pos) - 64) * Math.pow(26, len - pos - 1);
}
return out - 1;
}
I am creating an excel, when i write some values example 1-19, when I open the excel doc, i see 1-19, but if i click on it then excel tries to format it as a date
IS THERE A WAY to force the sheet to not use any formulas or formatting?
I have checked and the dataformat is string.
private void Test1(ref ISheet worksheet, string[] array, IWorkbook workbook, int iRow, XSSFFont font2)
{
var format = HSSFDataFormat.GetBuiltinFormats();
ICellStyle _TextCellStyle = workbook.CreateCellStyle();
_TextCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("#");
IRow file = worksheet.CreateRow(iRow);
int iCol = 0;
for (int y = 0; y < array.Length; y++)
{
ICellStyle style = workbook.CreateCellStyle();
style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
//style.DataFormat = HSSFDataFormat.
ICell cell = file.CreateCell(iCol, CellType.String);
cell.SetCellValue(array[y]);
style.SetFont(font2);
// cell.CellStyle = style;
var getst = cell.CellType;
cell.CellStyle = _TextCellStyle;
iCol++;
getst = cell.CellType;
}
}
Your data remains in "General" format even after you are using correct format string "#" as per documentation. Some time library methods don't work in NPOI so you'll have to try different approach.
You can try one of these
_TextCellStyle.DataFormat = workbook.CreateDataFormat().GetFormat("text"); //Instead of "#"
Or prefix single quote for data when writing it for excel file like '1-19
I'm not sure if this is an issue or if it is expected behavior from Excel. If it is indeed expected behavior, I would appreciate an explanation about what is happening, since I cannot see a pattern.
If you set a format and some values to a range and then assign only values to another range located below the first one, the format of the first range is applied partially (randomly?) to the new range.
My test function:
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
function loadSampleData(applyFormat) {
var columnCount = 30;
if (applyFormat) {
columnCount = 5;
}
var data = [];
for (var rows = 0; rows < 4; rows++) {
var row = [];
for (var columns = 0; columns < columnCount; columns++) {
var value = columns;
if (getRandomInt(1, 3) == 2) {
value = "text";
}
row.push(value);
}
data.push(row);
}
return Excel.run(function (ctx) {
var selectedRange = ctx.workbook.getSelectedRange().load(['rowCount', 'columnCount']);
var extendedRange;
return ctx.sync().then(function () {
var totalRows = data.length;
var totalColumns = data[0].length;
var deltaRows = totalRows - selectedRange.rowCount;
var deltaColumns = totalColumns - selectedRange.columnCount;
extendedRange = selectedRange.getResizedRange(deltaRows, deltaColumns);
if (applyFormat) {
extendedRange.format.fill.color = "orange";
extendedRange.format.font.color = "white";
}
else {
extendedRange.clear();
}
extendedRange.values = data;
}).then(ctx.sync)
}).catch(errorHandler);
}
Steps:
Create one button (Button A) in the task pane. This button must call loadSampleData and pass true, to write some data and format (fill and font color) to a range. (Range A)
Create another button (Button B) in the task pane. This button must call loadSampleData and pass false, to write only data (no format) to a bigger range (Range B).
Click the A1 cell, then click Button A. Notice the range and format that appears.
Click A6, then click Button B. Notice that a bigger range is written and that some of its cells have the same format as the range in step 3, even though no explicit format was set to the range.
With A6 still selected, click Button B multiple times. Notice that as the values of the range change, the formatted cells change as well.
Notes:
This doesn't happen if Range B is written above Range A.
This doesn't happen if Range B is written 4 (or more) rows below Range A.
Thanks!
Fascinatingly, I can reproduce the issue even in the UI, without programmability. Excel is doing some sort of pattern-matching, though I'll admit I have a hard time deciphering what the pattern is...
I will discuss with our team -- but in terms of concrete guidance, I think your safest bet is to set the values, and then clear the formatting. If you want to be super-careful, you can even take it one step further:
Set number formatting (to ensure that strings vs numbers vs date strings get interpreted as you want them to be)
Set the values
range.clear(Excel.ClearApplyTo.formats);
Re-set the number formatting
I will admit that this seems like a workflow worth improving. Let me see if we can either fix "values", or provide a method that lets you set values and number formats in one go, and in a stricter fashion than what is happening today.
I'm using epplus to read an excel file, I want to loop through each rows and each column and look for the column header to check against a collection and read the cell value if it matches. I'm new to epplus and couldn't find any reference to do something like this. I have looked into the similar question in this link, but I don't have something that loops through column headers.
Is there a possibility for doing this? Any help is much appreciated.
Thanks!
If you want to find a column in the Excel file that matches the name you specify you can use this function:
public string FindColumnAddress(ExcelWorksheet sheet, string columnName)
{
int totalRows = sheet.Dimension.End.Row;
int totalCols = sheet.Dimension.End.Column;
var range = sheet.Cells[1, 1, 1, totalCols];
for (int i = 1; i <= totalCols; i++)
{
if (range[1, i].Address != "" && range[1, i].Value != null && range[1, i].Value.ToString() == columnName)
return range[1, i].Address;
}
return null;
}
It will return the address of the column header.
You can change it to return i as the index of the column.
Or if you prefer a solution using Linq:
/// <summary>
/// Get 1 based column number of first column with 'columnName' as column header
/// </summary>
/// <param name="sheet"></param>
/// <param name="columnName">Column name to find</param>
/// <returns>1 based column number if found or null if column not found</returns>
public int? FindColumnAddress(ExcelWorksheet sheet, string columnName)
{
int totalCols = sheet.Dimension.End.Column;
var header = sheet.Cells[1, 1, 1, totalCols].ToList();
var idx = header.IndexOf(header.FirstOrDefault(hdritm => hdritm.Text == columnName));
return idx < 0 ? (int?)null : idx + 1;
}
Note: If you are retrieving many column header locations, you might want to generate the "header" list and then use the Linq query to retrieve each column number w/o having to rebuild the list for every search.
I found suggestion to use IndexOf here:
Get List<> element position in c# using LINQ
How to autofit content in cell using jxl api?
I know this is an old question at this point, but I was looking for the solution to this and thought I would post it in case someone else needs it.
CellView Auto-Size
I'm not sure why the FAQ doesn't mention this, because it very clearly exists in the docs.
My code looked like the following:
for(int x=0;x<c;x++)
{
cell=sheet.getColumnView(x);
cell.setAutosize(true);
sheet.setColumnView(x, cell);
}
c stores the number of columns created
cell is just a temporary place holder for the returned CellView object
sheet is my WriteableSheet object
The Api warns that this is a processor intensive function, so it's probably not ideal for large files. But for a small file like mine (<100 rows) it took no noticeable time.
Hope this helps someone.
The method is self explanatory and commented:
private void sheetAutoFitColumns(WritableSheet sheet) {
for (int i = 0; i < sheet.getColumns(); i++) {
Cell[] cells = sheet.getColumn(i);
int longestStrLen = -1;
if (cells.length == 0)
continue;
/* Find the widest cell in the column. */
for (int j = 0; j < cells.length; j++) {
if ( cells[j].getContents().length() > longestStrLen ) {
String str = cells[j].getContents();
if (str == null || str.isEmpty())
continue;
longestStrLen = str.trim().length();
}
}
/* If not found, skip the column. */
if (longestStrLen == -1)
continue;
/* If wider than the max width, crop width */
if (longestStrLen > 255)
longestStrLen = 255;
CellView cv = sheet.getColumnView(i);
cv.setSize(longestStrLen * 256 + 100); /* Every character is 256 units wide, so scale it. */
sheet.setColumnView(i, cv);
}
}
for(int x=0;x<c;x++)
{
cell=sheet.getColumnView(x);
cell.setAutosize(true);
sheet.setColumnView(x, cell);
}
It is fine, instead of scanning all the columns. Pass the column as a parameter.
void display(column)
{
Cell = sheet.getColumnView(column);
cell.setAutosize(true);
sheet.setColumnView(column, cell);
}
So when you wiill be displaying your text you can set the particular length. Can be helpfull for huge excel files.
From the JExcelApi FAQ
How do I do the equivilent of Excel's "Format/Column/Auto Fit Selection"?
There is no API function to do this for you. You'll need to write code that scans the cells in each column, calculates the maximum length, and then calls setColumnView() accordingly. This will get you close to what Excel does but not exactly. Since most fonts have variable width characters, to get the exact same value, you would need to use FontMetrics to calculate the maximum width of each string in the column. No one has posted code on how to do this yet. Feel free to post code to the Yahoo! group or send it directly to the FAQ author's listed at the bottom of this page.
FontMetrics presumably refers to java.awt.FontMetrics. You should be able to work something out with the getLineMetrics(String, Graphics) method I would have though.
CellView's autosize method doesn't work for me all the time. My way of doing this is by programatically set the size(width) of the column based on the highest length of data in the column. Then perform some mathematical operations.
CellView cv = excelSheet.getColumnView(0);
cv.setSize((highest + ((highest/2) + (highest/4))) * 256);
where highest is an int that holds the longest length of data in the column.
setAutosize() method WILL NOT WORK if your cell has over 255 characters. This is related to the Excel 2003 max column width specification: http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP005199291.aspx
You will need to write your own autosize method to handle this case.
Try this exemple:
expandColumns(sheet, 3);
workbook.write();
workbook.close();
private void expandColumn(WritableSheet sheet, int amountOfColumns){
int c = amountOfColumns;
for(int x=0;x<c;x++)
{
CellView cell = sheet.getColumnView(x);
cell.setAutosize(true);
sheet.setColumnView(x, cell);
}
}
Kotlin's implementation
private fun sheetAutoFitColumns(sheet: WritableSheet, columnsIndexesForFit: Array<Int>? = null, startFromRowWithIndex: Int = 0, excludeLastRows : Int = 0) {
for (columnIndex in columnsIndexesForFit?.iterator() ?: IntProgression.fromClosedRange(0, sheet.columns, 1).iterator()) {
val cells = sheet.getColumn(columnIndex)
var longestStrLen = -1
if (cells.isEmpty()) continue
for (j in startFromRowWithIndex until cells.size - excludeLastRows) {
if (cells[j].contents.length > longestStrLen) {
val str = cells[j].contents
if (str == null || str.isEmpty()) continue
longestStrLen = str.trim().length
}
}
if (longestStrLen == -1) continue
val newWidth = if (longestStrLen > 255) 255 else longestStrLen
sheet.setColumnView(columnIndex, newWidth)
}
}
example for use
sheetAutoFitColumns(sheet) // fit all columns by all rows
sheetAutoFitColumns(sheet, arrayOf(0, 3))// fit A and D columns by all rows
sheetAutoFitColumns(sheet, arrayOf(0, 3), 5)// fit A and D columns by rows after 5
sheetAutoFitColumns(sheet, arrayOf(0, 3), 5, 2)// fit A and D columns by rows after 5 and ignore two last rows