conditional formatting not working - excel-2010

(am using PHPExcel 1.7.8)
I create a sheet and populate it with a table of numeric data. Then Excel refuses to apply conditional formatting of the 3-color-transition type to the numeric data.
That means, too:
1) I can use conditional formatting of the "top 10 values" kind
2) If I populate right next to the generated table few cells with numbers then I can use the cond formatting of transitional kind
3) If I copy two cells from the generated table somewhere else in a simple copy/paste fashion still can't use the transitional formatting
4) If I copy two cells from the generated table somewhere else using the "just the value"-pasting I do can use the transitional formatting
Another important observation here is, that when I select a cell of the generated table and click into the value bar - right at that moment - the cell changes its color to what it should be regarding the conditional formatting!
This is somewhat similar to another phenomena I observed with PHPExcel generated spreadsheets. Sometimes it happens that, when I double click into a cell to get into editing mode - the cell turns pit black. But still I can change the value.
There seems to be something wrong with how a cell is represented in the Excel-file I guess. Something related to the control of the coloring ... !?
I could of course copy/(value-)paste everything. But maybe I am just using PHPExcel the wrong way? Or there is a quick way to convert an Excel file at once in a useful way?
The full code I use is this:
$excelWorkbook = null;
if(file_exists($filename)) {
$reader = PHPExcel_IOFactory::createReader("Excel2007");
$excelWorkbook = $reader->load($filename);
} else {
$excelWorkbook = new PHPExcel();
}
$sheet = $excelWorkbook->getSheetByName($tabName);
if ($sheet !== null) {
$excelWorkbook->removeSheetByIndex($excelWorkbook->getIndex($sheet));
}
$sheet = new PHPExcel_Worksheet($excelWorkbook, $tabName);
$sheet = $excelWorkbook->addSheet($sheet);
$columns = array_keys($targetArray);
$rows = array_keys($targetArray[$columns[0]]);
for($i = 0; $i < count($columns); $i++){
$sheet->setCellValueByColumnAndRow($i+1,1,$columns[$i]);
}
for($i = 0; $i < count($rows); $i++){
$sheet->setCellValueByColumnAndRow(0,$i+2,$rows[$i]);
}
for($i = 0; $i < count($columns); $i++){
for($j = 0; $j < count($rows); $j++) {
$sheet->setCellValueByColumnAndRow($i+1, $j+2, $targetArray[$columns[$i]][$rows[$j]]);
}
}
$excelWorkbook->setActiveSheetIndex($excelWorkbook->getIndex($sheet));
$xlsx = new PHPExcel_Writer_Excel2007($excelWorkbook);
$xlsx->save($filename);

The workaround-type solution is to select all cells in the respective sheet and choose for background color 'none'.
Apparently the cells are colored white and this coloring overlays the conditional coloring.

This may be a bug due to the fact that for solid fills in cells Excel reverses the meaning of foreground and background colours but in conditional (dxf) formats it doesn't.
Or, to put it another way background and foreground are stored differently for cell and conditional formats.
I encountered this issue in the Perl Excel::Writer::XLSX module.

Related

Is there a way for lazy loading with FlowRow?

I use FlowRow in the accompanist project to auto wrap my text items to next line. It works as intended. However, when I have a large dataset (which I already load with paging), I don't find an api like LazyColumn to load and build the items as needed, if I loop through the pager flow, it tries to load to build everything at once. Any adice please?
lazyPagingItems = pager.flow.collectAsLazyPagingItems()
FlowRow(
) {
val items = lazyPagingItems
for (index in 1..items.itemCount-1) {
Text(
text = word,
maxLines = 1
)
}
}
Little late to the party. But it seems you could use LazyVerticalGrid or LazyHorizontalGrid in adaptive mode like below.
LazyVerticalGrid(
columns = GridCells.Adaptive(/* item min size */)
) {
// Items
}

Conditional formatting in google sheets to inherit formatting from other cell

I've been trying to figure out how to apply conditional formatting to the background of a cell. I've searched the internet but haven't been able to find anything related to this.
I am interested in formatting a cell conditionally, so that if the contents are equal to a text string, the background color will inherit the background color of another cell.
Does anyone know if this is possible and how to accomplish it?
This is the quasi-code for the functionality I am looking to achieve:
If (cell contents are empty) then
Apply background from cell offset two cells to the right
End If
This is not possible with plain conditional formatting, you'll need an onEdit function.
function onEdit(e) {
var r = e.range;
if (e.value.length > 0) {
var reference = e.source.getActiveSheet().getRange(r.getRow(), r.getColumn()-2)
var color = reference.getBackground();
r.setBackground(color);
} else {
r.setBackground("white");
}
}
Note that this will not update the cell's background color when you change the reference cell's background color.

Table merge cells - Vaadin

I'm Creating table with Vaadin. Some of the cells are repeating. So I want them to merge in one cell, as you can see on the image:
The first image show how the table looks now, and the second is how I want to look with merged cells.
I'm using this code:
Table table = new Table(AppData.getMessage("menu.report2"));
table.addContainerProperty(tableHeaders[0], String.class, null);
table.addContainerProperty(tableHeaders[1], String.class, null);
table.addContainerProperty(tableHeaders[2], String.class, null);
table.addContainerProperty(tableHeaders[3], String.class, null);
List<User> employeeList = employeeDAO.findAllEmployees();
int i;
for (i = 0; i < employeeList.size(); i++) {
User employee = employeeList.get(i);
table.addItem(new Object[]{
CaseStatus.OPEN,
tasksDAO.countTasks(CaseStatus.OPEN),
employee.getFirstAndLastName(),
tasksDAO.countTasks(employee, CaseStatus.OPEN)},
i);
}
for (int j = 0; j < employeeList.size(); j++) {
User employee = employeeList.get(j);
table.addItem(new Object[]{
CaseStatus.CLOSED,
tasksDAO.countTasks(CaseStatus.CLOSED),
employee.getFirstAndLastName(),
tasksDAO.countTasks(employee, CaseStatus.CLOSED)},
i + j);
}
table.setPageLength(table.size());
addComponent(table);
setComponentAlignment(table, Alignment.TOP_CENTER);
setMargin(true);
I think, this is not possible in "normal way". But I have an idea how to simulate this.
First solution:
You could simply use GridLayout to build grid as you want. Then nothing limit your imagination beside size of such grid. It shouldn't be to big (also pointed here).
Second solution:
Another advice is a bit crazy.
Disable Table/Grid stripes.
In group of similar cells just fill only first row. Left rest blank (or space in string).
Then you dispose of repeated data (from your first and second column), but you can't alignment them vertically (the text will stay at top - first row from group).
Third solution:
Check Add-ons list. For example ItemGrid.

ui-grid - How to pin a ROW to the top

I'm using the angularjs ui-grid and I have a "total" row that I want to pin to the top of the grid no matter what is the current sorting.
How can I accomplish that?
I think this is what you are looking for : Row Pinning
Essentially add another hidden column, something like this:
{
field: 'pinned',
visible: false,
sort: {direction: uiGridConstants.ASC, priority: 0}, //use uiGridConstants.DESC for pinning to the bottom
suppressRemoveSort: true,
sortDirectionCycle: [uiGridConstants.ASC] //use uiGridConstants.DESC for pinning to the bottom
}
Row entities which have pinned = true rise to the top, even when other sorting are applied.
DISCLAIMER: I know it's not exactly answers the question, but this is how I solved it for now until I'll have a better solution:
Create an other grid above the main grid :
<div style="height:30px" ui-grid="totalGridOptions"></div>
<div ui-grid="gridOptions" class="grid"></div>
with definitions:
$scope.totalGridOptions = {showHeader:false,enableHorizontalScrollbar:false};
and then bind the columns of the main grid to the total grid (for width and other adjustments):
$scope.$watch('gridOptions', function (newVal) {
$scope.totalGridOptions.columnDefs = newVal.columnDefs;
}, true);
I think you should use something like this
$scope.gridOptions.data.unshift({label:value});
unshift adds it to the top
Edit 2 / Actual Solution: The way I finally settled this issue was by using custom header cell templates. I essentially create a second header row by adding a div at the bottom of what was previously my header. Here's a simple version:
<div class="super-special-header>
<div class="header-display-name">{{col.displayName}}</div>
<div class="totals-row">{{grid.appScope.totals[col.field]}}</div>
</div>
I store my totals on the controller's $scope and can access them in that div with grid.appScope.totals[whateverThisColumnIs]. This way I can still update them dynamically, but they don't get mixed into a sort function like my previous 'solution' was aiming for.
Edit 1 / Dead-end 'solution': Just ran into a problem with my solution, if your table is long (you have to scroll to get to bottom rows), the totals row will scroll out of view. Going to leave this 'solution' here so no one else makes the same mistake!
I had this same issue but with a twist. Since I was going to need to change the default sorting algorithms for many of the columns anyway, I set my algorithm up to skip the first element in the sort. You can use the sortingAlgorithm property on any columndef that would be part of a sortable column. This is really only a solution if you have only a few sortable columns though. It becomes unmaintainable for huge tables.
I couldn't find any built-in feature for ui-grid to keep a specific row at the top of the grid when sorting is applied. But this could be done using sortingAlgorithm parameter in the columnDefs( please refer to http://ui-grid.info/docs/#!/tutorial/Tutorial:%20102%20Sorting).
I have written an algorithm which keeps the row('total' is the particular cell value in the row) at the top of the grid without applying a sorting.
var sortingAlgorithm = function (a, b, rowA, rowB, direction) {
if (direction == 'total') {
if (a == 'total') {
return 0;
}
return (a < b) ? -1 : 1;
} else {
if (a == 'total') {
return 0;
}
if (b == 'total') {
return 1;
}
}
}

How to avoid JQuery UI accordion with a long table inside from scrolling to the beginning when new rows are appended?

I have a table of many rows in a JQuery UI accordion.
I dynamically append the table this way:
var resJson = JSON.parse(connector.process(JSON.stringify(reqJson)));
for ( var i in resJson.entryArrayM) {
// test if entry has already been displayed
if ($("#resultTr_" + resJson.entryArrayM[i].id) == null)
continue;
$("#resultTable > tbody:last").append(listEntry.buildEntryRow(resJson.entryArrayM[i]));
}
Firstly I check if a row of the same tr id already exists. If not, I would append to the last row of the table.
It works. But the problem is: every time a row is appended, the accordion would scroll to the first row of the table. Since the table is remarkably long, it makes users inconvenient to scroll down again and again to watch newly-added rows. So how to avoid this?
First of all, just do one append rather than appending every time through the loop:
var resJson = JSON.parse(connector.process(JSON.stringify(reqJson)));
var seen = { };
var rows = [ ];
var trId = null;
for(var i in resJson.entryArrayM) {
// test if entry has already been displayed
var trId = 'resultTr_' + resJson.entryArrayM[i].id;
if($('#' + trId).length != 0
|| seen[trId])
continue;
rows.push(listEntry.buildEntryRow(resJson.entryArrayM[i]));
seen[trId] = true;
}
$("#resultTable > tbody:last").append(rows.join(''));
Also note that I corrected your existence test, $(x) returns an empty object when x doesn't match anything, not null. Not only is this a lot more efficient but you'll only have one scroll position change to deal with.
Solving your scrolling issue is fairly simple: find out what element is scrolling, store its scrollTop before your append, and reset its scrollTop after the append:
var $el = $('#whatever-is-scrolling');
var scrollTop = $el[0].scrollTop;
$("#resultTable > tbody:last").append(rows.join('')); // As above.
$el[0].scrollTop = scrollTop;
There might be a slight visible flicker but hopefully that will be lost in the noise of altering the table.
You could also try setting the table-layout CSS property of the <table> to fixed. That will keep the table from trying to resize its width or the width of its columns and that might stop the scrolling behavior that you're seeing. The downside is that you'll have to handle the column sizing yourself. But, you could try setting table-layout:fixed immediately before your append operation to minimize the hassle.

Resources