Simple conditional text formatting with openpyxl by text - openpyxl

I am using openpyxl 3.0.3 and python 3.7.3. I don't know what version of Excel I'm using since it does not seem to say anywhere. It's Office 365.
What I want to do is simple: if the cell value is "fail", make the cell one color, if "pass" another, etc. But everything I try seems to run just fine, but when I try to open the workbook with Excel, it gets the same result: Excel saying "We found a problem with some content" and offering to repair the workbook. When I let it repair, the formatting is gone. I've tried all of the following, taken from various SO answers and other examples:
cellref = 'B2'
red_font = Font(size=14, bold=True, color='ffffff')
red_fill = PatternFill(start_color='ffcccc', end_color='ffcccc', fill_type='solid')
ws2.conditional_formatting.add(cellref, CellIsRule(operator='equal', formula=['fail'], fill=red_fill, font=red_font))
ws2.conditional_formatting.add(cellref, CellIsRule(operator='containsText', formula=['fail'], fill=red_fill, font=red_font))
ws2.conditional_formatting.add(cellref, CellIsRule(operator='lessThan', formula=['0'], fill=red_fill, font=red_font))
ws2.conditional_formatting.add(cellref, FormulaRule(formula=[f'NOT(ISERROR(SEARCH("fail",{cellref})))'], stopIfTrue=True, fill=red_fill))
All get the identical bad result. I have read the documentation and examples as well as every StackOverflow answer I can find on the subject. This seems like such a simple, basic usage of the package.

I believe it has to do with the combination of options you're choosing. It's actually easier (in my opinion) to declare a Rule and then assign that a type. Your second two rules were good. Compiled fine for me. The first two had the issue. I replaced them with a Rule of 'containsText' type.
Here's what I was able to come up with based on the example you provided:
cellref = 'B1:B6' #adjusted to show more cells
red_font = Font(size=14, bold=True, color='ffffff')
red_fill = PatternFill(start_color='ffcccc', end_color='ffcccc', fill_type='solid')
ws2.conditional_formatting.add(cellref, Rule(type='containsText', operator='containsText', formula=['fail'], dxf = DifferentialStyle(fill=red_fill, font=red_font)))
ws2.conditional_formatting.add(cellref, CellIsRule(operator='lessThan', formula=['0'], fill=red_fill, font=red_font))
ws2.conditional_formatting.add(cellref, FormulaRule(formula=[f'NOT(ISERROR(SEARCH("fail",{cellref})))'], stopIfTrue=True, fill=red_fill))
Results

I had trouble with the text search condition despite #APhillips helpful answer.
The best thing I got to work was:
from openpyxl import styles, formatting
cellref = 'B1:B6' #adjusted to show more cells
red_fill = styles.PatternFill(start_color='ffcccc', end_color='ffcccc', fill_type='solid')
rule = formatting.Rule(type='expression')
rule.formula = [f'ISNUMBER(SEARCH("fail", {cellref}))']
rule.dxf = styles.differential.DifferentialStyle(fill=red_fill)
sheet_object.conditional_formatting.add(cellref, rule)

Related

Where do I enter moment.js code so a DataTables column appears in my chosen format?

I want to make data like 2022/12/15 (imported from Google Sheets at the moment of page load) display as "Dec 15" in one column, and as "Thu" in another column.
I found the format codes:
moment().format("MMM d"); // "Dec 15" and
moment().format("ddd"); // "Thu".
I read https://momentjs.com/docs/ and https://momentjs.com/guides/, but could not find:
Where do I enter that code?
Can I skip entering that code, and instead just enter this?:
$(document).ready(function () {
$('#example').DataTable({
columnDefs: [
{
targets: 4,
render: DataTable.render.datetime('MMM d'),
},
],
});
});
Perhaps I should separate my questions into different posted questions, but I imagine it will be more helpful for future readers to find the combined challenges asked and answered in a single thread.
I'm using the WordPress plugin "Inline Google Spreadsheet Viewer", which is built on DataTables.
[Edit: I realized I should not paste that code into inline_gdocs_viewer.php, and intended to paste it into igsv-datatables.js. I could not however figure out where to paste within that file. What should I look for to know where to insert it? (The author of that plugin publicly announced that no more support will be given.) Should I post the contents of the js file somewhere so you can see it?]
[Edit 2: Would entering that code make column 5 always displayed in MMM d format, even if I want column 5 to be displayed in a different format? Shouldn't specifying what format to apply to which column happen when I create a page / post and use the shortcode to embed the spreadsheet?]
(I read the plugin's documentation and I read DataTables documentation.)
If my table doesn't have a name, what do I use instead of #example? Do I need to name my table? If so, how? (I tried to find the answer at, for example, https://www.w3schools.com/html/html_tables.asp.)
The source for the longer code came from a live example where the dates were in column 5, but the code says "targets:4". To apply the code to column 3 (for example), should I enter targets:2? I read https://datatables.net/reference/option/columns.render, but bizarrely that didn't address this basic question.
If you're wondering if I upgraded DataTables from the old version that comes with Inline Google Spreadsheet Viewer, yes I did.
If you're wondering if I got the script array to call moment.js, I might have. Into the scripts array I added
,
'moment' => array(
'src' => plugins_url('moment.min.js', __FIL__),
'deps' => array( 'moment' )
)
I'm not sure about the "deps" part. Does it mean dependencies? Was I right to use "moment" twice in that code?
If you only know a possible answer to one of these questions, that's better than nothing, so please do share what you can.
I know the Moment team recommends you choose a different library than Moment for new projects, but DataTables to this day recommends Moment for sorting by Day of the week or sorting by Date.
Thanks
Using moment.js 2.29.2 and DataTables 1.13.1.
What I tried, and what I expected:
I read the documentation for all the programs, and I read forum discussions for all the programs, and I studied examples, expecting to find where to enter the code, but nowhere could I find what to do with it. It was always assumed that that part was already known.

Using datepicker in DotNetNuke-7 module

I'm a DNN beginner. I am currently building a module in which I can display statistics. My DotNetNuke Version is 7.0. The statistic is from Chartjs. For this statistic I would like to implement filters. These filters should be datepickers. As UI element I have now included a textbox with TextMode='Date'. But currently I have problems to set the default value of this field. The default value should be 01.01. of the current year.
I have already tried to set the value via C# server side. Unfortunately without success. I also tried to set the value on the client side via JavaScript. Unfortunately also without success.
These are some lines I tried in JavaScript:
document.getElementById(<%= this.DatumVon.AccessKey %>).value = "01.01.2019";
document.getElementById(<%= this.DatumVon.AccessKey %>).innerText = "01.01.2019";
document.getElementById("DatumVon").value = "01.01.2019";
These are some lines I tried in C# in the method "Page_Load" (server side):
this.DatumVon.Text = "01.01.2019";
I expected the value of the TextBox to be 01.01.2019. However, it currently only contains dd.mmm.yyyy. How can I change this?
Thank you.
There is something wrong with your localization. Please refer to the jQuery UI datepicker documentation (the "Localization" section), this should give you the answer.
wow... I solved it. I made it. Sometimes the solution is right in front of you and you don't see it. Michael Tobisch was absolutely right. When setting the value, the format is very important. I have always used the German format. A DNN TextBox with TextMode="Date" can't handle that. DNN converts this TextBox into an HTML input field. But this input field can only be clear with the format "yyyy-mm-dd". Depending on the geographical position of the client (at least that's what I think) the text displayed in the input field will be formatted. But the value of the input field always has the same format ("yyyy-mm-dd"). So very important here: the displayed text and the actual value have different formats.
Many thanks again to Michael Tobisch for the mental inspiration and the patience with me.
What is also important is that the access to the actual ID of a DNN element works as follows: this.Element.ClientID and not as I assumed before this.Element.AccessKey. This was also buggy.

openpyxl: How to get multiple auto-filters on a single sheet

How can I get multiple filters on a single sheet?
A single filter is easy:
worksheet.auto_filter.ref = "A1:D4"
I can't figure out how to do multiple filters. I tried a few things like:
worksheet.auto_filter.ref = "A1:D4,A6:D9"
worksheet.auto_filter.ref = "A1:D4;A6:D9"
but noting works.
Thanks,
Ryan
The specification says that there may be only a single auto-filter per worksheet.
As long as you're happy with working with a development version you might want to look at the 2.4 development branch which has much improved filtering and sort support. I don't use filters much myself so it could use some testing and the feedback would be very helpful.
The API has changed a bit and the best documentation is in the tests:
af = AutoFilter('A1:F1')
af.add_filter_column(5, ["0"], blank=True)
ws.auto_filter = af
Found out I can use tables to implement the sort/filter functionality. And you can have multiple tables in a single sheet. Unfortunately I can't find a way to do this in openpyxl but XlsxWriter can do this very easily.
worksheet1.add_table('A1:C4')
worksheet1.add_table('F8:H15')

Copy language content to another

I'm on IP 4.0.11.
Is it possible to copy all content of a language to another (ie to edit only texts and not style or html)? Is there an option or perhaps in database?
Thanks in advance.
At this time you can only copy pages. In your case I'd do like this:
Create a new language.
Create same menus in new language (same name and alias).
Then go to an original language.
Select page (copying parent copies children, too)
Click "copy" icon.
Go to a new language.
Select required menu.
Click "paste" icon.
Repeat 3-8.
It's a bit of manual work, but it's a fool-proof. It shouldn't take you very long. Changing a database may create many errors.
Thanks for answer, Audrius. This works fine with one caveat: The pages pasted into the new language still gets prepended with the old language's language prefix.
Example: copied english page "home" to Norwegian and set the url to "hjem". The new page now got the url /en/hjem instead of no/hjem as expected.
I'm on 4.0.16. Thankful for any hints.
Update May 21st:
Did some debugging. Apparently there is nothing in the code of Ip\Internal\Pages copySinglePage() that cares about changing the $copy['languageCode'] for the new page to be inserted.
Line 93 and onwards (referring to 4.0.16):
unset($copy['id']);
$copy['parentId'] = $newParentId;
$copy['pageOrder'] = $newIndex;
$copy['urlPath'] = UrlAllocator::allocatePath($copy['languageCode'], $copy['urlPath']);
whereas I'd also expect some logic setting
$copy['languageCode'] = something;
Hope this reaches the developers

REUSE_ALV_GRID_DISPLAY different designs

I want to build a normal ALV-list with ABAP.
I´m using the function module REUSE_ALV_GRID_DISPLAY.
At the moment my code Looks like this
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = z_repid
i_grid_title = ls_title
is_layout = ps_layout
it_fieldcat = lt_fieldcat
i_save = gs_save
is_variant = gs_variant
TABLES
t_outtab = mytable.
My program works fine and the output I get is ok.
But I want to use more attitudes for different style / layout definitions in the ALV.
The reason is, the ALV-list includes a lot of values,
and is made up of two different internal tables ("mytable_a" and "mytable_b" i´ve added to "mytable", which is for output).
I want the tables to look different, but don´t know which possibilitys I get to implement this.
At the beginning of my project I wanted to build a ALV Tree Control,
but I have no Idea how to interpret the different function modules for this.
Can Anybody can give me a simple example how to build ALV_Tree_Control (I know the demo-programs in SAP, but they won´t help me) or can tell me what possibilities I have with the normal ALV Grid?
I hope you understand my problem,
thanks in advance