We have Excel 2013-2016 Add-in in C#. We use following method to get worksheet font:
var defaultFont = Microsoft.Office.Interop.Excel.Worksheet.Cells.Font
Everything worked until user changed font of some particular cell. For example user changed cell A1 and set font "Arial". By default we have font "Calibri".
Thereafter when we try to get default font by Worksheet.Cells.Font we get nothing - just empty object. I assume that because of ambiguity: A1 has font "Arial", other cells - default font "Calibri". My goal to get default font, i.e. "Calibri".
So far I re-write my code and now I check Styles too:
var workbookFont = Microsoft.Office.Interop.Excel.Worksheet.Parent.Styles["Normal"].Font;
This workaround returns exactly what I need - my "Calibri" font. But then I found , that's because this font is default for all new worksheets/workbooks (it can be setup in Options of Excel). It doesn't work if user selected all cells in worksheet and changed default font to "Times New Roman", then I would get "Calibri" - because this is default font will be used once new worksheet/workbook created. Again I faced issue when I can't get real default font for current worksheet. My next thought was to get font of some particular cell at the edge of worksheet that is not much usable for user like:
var defaultFont = workSheet.Cells[1048576][16384].Font
It looks pretty weird, but it works. My assumption that user doesn't use the last cell on a worksheet. (The 1048576 and 16384 are max size of worksheet ). I don't know implications of these method, so I'm wondering does it exist some "legal" way to get default font of worksheet , without such crutch ?
You were on the right path when using:
var workbookFont = Microsoft.Office.Interop.Excel.Worksheet.Parent.Styles["Normal"].Font;
The Normal style is the default font for a sheet, but the sheet might not actually have any instances of the default font (or the Normal style), or, all cells decorated with the Normal style may have had the font overridden on each cell format.
If a user applies their own fonts to various ranges, then the sheet will potentially have numerous fonts, and none of them are guaranteed to be the same as the font in the Normal style. The Font.Name might differ across a sheet, in which case Font.Name returns null, even though the Font.Size might be consistent (or if it isn't, then it too returns null).
When a user applies a custom font to a range that already has the Normal style, then the Style remains associated with the range, and the font overrides any font defined in the style.
Furthermore, it is common practice to select all the cells on the sheet when changing the font of the UsedRange (so that the row heights adjust proportionally), even if the UsedRange is only a small portion of all Cells.
So, you have to choose a font that is representative of the fonts that are used in the sheet, or apply fonts to new ranges as if they were Normal. That choice should be informed by what you know about the sheet, and what you intend to do with the font:
If you're inserting a column or row, then I'd suggest that you mimic Excel behavior and use the adjacent formats.
If you're adding a new range that is not adjacent to the UsedRange, then you will probably want to default to the Normal style.
If you're looking for a representative font of the UsedRange, you might want to avoid the first few rows and columns, as these tend to be headers, and the last rows can sometimes be totals. You'll need to loop over the cells in the range to find an appropriate cell format.
If you're reproducing the sheet content in some other format, then you'll need to inspect the font of each cell and every cell in the UsedRange.
The bottom-right-most cell is not necessarily any more or less similar to Normal or the fonts used in the UsedRange, so I'd advise against using it.
Related
Is there any way to set style for all next cells in excel worksheet? Like for the rest of the document after certain cell?
I am using Apache POI 3.9 for exporting data to excel worksheet. Client wants colored font for invalid data. Everything is running fine, but sometimes, when I put text in empty cells in "INFORMATION" column, it makes font colored red, even after reseting cell style or explicitly asking for condition and then setting default cell style. Youre more likely to get my idea from pic of code.
Ive uploaded some pic of excel too. Hope it helps. It starts on row 37, text somehow changes color even after not running through setting conditions. I havent recognized any pattern in there.
I tried to change conditions, change cell style setting location in code... Had no effect.
Edit: It seems like the problem is not in AP but in Excel itself. It looks like the font color is changed after user input and its caused by autoformatting that is based on the context. So only option here is to change style of previous data. Could not find any other solution to this.
So I was wondering if you could change the background color of certain cells in Microsoft Excel using Applescript.
I've understood how one can target specific cells but neither back color (contained in the scripting dictionary), background color (not contained in the scripting dictionary) nor color result in anything but missing value's and errors. This is my code so far:
tell application "Microsoft Excel" to set back color of cell "A1" of front sheet of front document to {255, 244, 233}
Ranges/cells have an 'interior' which you can access using interior object. Look for 'interior' in the dictionary. There are other associated properties as well. You can also affect border colours similarly.
So…
set color of interior object of range "A1" to {255, 244, 233}
You can also apply some of the preset colours by applying a color index, like so…
set color index of interior object of range "A1" to 6
There is a method to getting the myriad color index numbers but I've forgotten what it is (other than trial and error). But if you assign a colour by clicking on a palette, you can then get color index for that range. Actually, the applescript doc for excel has a palette table with the numbers.
I have a table of contents in Excel 365. In the table, column 2 contains the names of worksheets and column 3 contains the names of tables and/or named ranges in the associated worksheet. I used the HYPERLINK function to link each cell to its specific component: hyperlinks for the worksheet column go to cell A1 on the associated worksheet; hyperlinks in the table/named range column go to that specific table or named range.
Since this results in several repeats of worksheet names, I used conditional formatting to change the text to white when the worksheet name is a repeat of the one directly above it. This makes it look more like an outline. It works like a charm up until the point that I click on the link in a repeated cell, making it a FollowedHyperlink. When I return to the table, this is no longer whited out. Is there a way to make these stay white, even after they've been followed?
Here's the formula I used for the conditional formatting rule applied it throughout the range =$C$6:$C$24:
=C5=C6
Here's what it looks like before I click one of the whited-out cells:
Here's what it looks like after I click one of the whited-out cells:
Urdearboy posted a link showing how to change the style of a followed link.
https://superuser.com/questions/728825/how-do-i-prevent-excel-from-changing-the-color-of-a-clicked-link/819931#819931
It is probably the best solution without using VBA.
But if you want some not control over it, if for example, you may want all links in a table if contents to be uniform color regardless if they were taken or not, but you want links in the rest of the book to change. That's as easy as a single line of VBA in the worksheet's SheetFollowHyperlink event:
Target.Range.Font.Color = vbBlue
And if you wanted to apply it to the entire workbook instead of a single sheet you would put it in the workbook's SheetFollowHyperlink event.
But if you want to be a little creative...
You could make the link disappear by blending it with the background color:
Target.Range.Font.Color = Target.Range.Interior.Color
Or you can draw attention to it, by changing the back color:
Target.Range.Interior.Color = vbYellow
Really, your imagination is the only limit. You can do just about anything you'd ever want to do ( and a lot more of things you would never want to do).
When I run a query in MS Excel, and return the data to the worksheet, it always returns the data in font Helv 7. This format also affects the format of the row numbers & column headers. My default font/size is Calibri 11. I've looked all over and I can't seem to find any setups to change.
I looked at your workbook and the problem has nothing to do with Power Query. Any newly created sheet will have a font of Helvetica 7.
The problem is due to your "Normal" style which has been changed.
Right click on the Style box, select Modify; then Format; and change the font format to whatever you want for a new worksheet.
If you do this on the sheet that contains the query result, the font will change on that sheet (but not on other sheets that have previously been created).
i am currently coding a button to transfer all the data from a listview into an excel workbook so it can be printed as barcode labels. the way the barcode works is that it is a true type font. im running into the problem however through vb6 where i dont know how to format a range of cells to have a different font style so i can change certain cells to be able to show barcodes. ive gotten as far as
oSheet.range("D2:D10").Font
where D2:D10 is not the set range of cells just ones i was playing with. im not sure where to go after the font property here.
Have you tried:
oSheet.range("D2:D10").Font.Name = "BarCode Font Name"