Using Axlsx some of my cells are set to wrap text which works great. The problem I'm running into is how the auto-width of the cells seems to be calculated. It seems like what's happening is initially the data comes in like this:
cell line1 cell line2 cell line3 cell line4 cell line5
Which Axlsx using to set the width to be that big. But then wrap text comes in and correctly sets the cell to this:
cell line1
cell line2
cell line3
cell line4
cell line5
It's height is then set correctly to display all five values but the row width is still huge. Does anyone know if there's a way to get Axlsx to auto-size the cell as if you were to double click the cell within Excel to have it auto size the width? Setting Excel column widths correctly when adding to spreadsheet via Axlsx seems to elude to it but I couldn't get it to have any effect - not sure the :widths property exists anymore or works?
I also have been playing around with column_widths as well e.g. sheet.column_widths *30. Does anyone know if there's a way to force all columns to have the same widths with this property? It doesn't seem to accept an array - only integers/floats/fixnums separated by commas and doing *30 only sets the first column to a width of 30. I don't actually know beforehand how many columns I'm going to be using (dynamic data) so I'd love to be able to set that to just auto-size all of the columns to a certain width.
Check colum_widths function, It iterates through arguments passed and sets that many(number of arguments passed) columns width to passed values.
so, If you want to set X number of columns width to Y, then you should pass [Y]*x array as argument to column_widths, and use spalt(*) operator to convert this array to arguments list.
In your case Number of columns are going to de dynamic, so you can use sheet.column_info.count to get number of columns in sheet and If you want to set all that columns' width to say 30, then all you need to do is, AFTER adding data set width using -
sheet.column_widths *([30]*sheet.column_info.count)
Not a real solution, but I use the following code to set the column width of "wrap columns" when Axlsx makes them too wide:
sheet.column_widths *(sheet.column_info.map { |info| [info.width, 50].min })
Related
As the title suggests, I'm trying to figure out how to format cells in the spreadsheet to be filled by a specific color based on if they have a value in them (negative or positive) AND depending on what specific word is in a particular column. If there's no value in the cell, there would be no fill.
Example: Column A cells have various words in it (all of the cells with at least two words) but I'm focused on the specific words "Penalty" and "Rewards". Column C through L have varying dollar amounts (some positive, some negative) and many of the cells are blank. I'd like the cells with values in them (C:L) to match the color of the corresponding word cells (A) based off of the words (Penalty/Reward).
This is what I'm trying to make it look like...
I attempted to find the answer elsewhere, but couldn't quite figure it out.
see:
=COUNTIF($A1, "*reward*")*(A1<>"")
=COUNTIF($A1, "*penalty*")*(A1<>"")
Is there a way to delete empty cells in a given range and shift the column up to the desired display as shown below? Closest I came was
=ARRAYFORMULA({A1:C1; TRANSPOSE(SPLIT(TRANSPOSE(QUERY(A2:C,,999^99)), " "))})
which removes empty cells, but splits the first names and surnames into separate cells, which I have not figured out how to avoid. Pfa a made-up sample of current and desired displays:
Current Display
Desired Display
I'm new at this, but I came up with a bit of a brute force method, which may help you.
={
{(A1:C1)};
{FILTER(A2:A100,A2:A100<>"");indirect("N1:N" & 100-counta(A2:A100))},
{FILTER(B2:B100,B2:B100<>"");indirect("N1:N" & 100-counta(B2:B100))},
{FILTER(C2:C100,C2:C100<>"");indirect("N1:N" & 100-counta(C2:C100))}}
Assuming your data block is in columns A1:C100, this formula filters blank cells from each individual column, and then pads each column with blank cells at the bottom, to make the three arrays equal in length/dimension.
Note that in "100-counta(...", the 100 is the expected maximum length of your data column.
This could be calculated, and must be the same for all three columns.
Note also that the first array is horizontal (ends with a semi-colon), followed by the three columns, stacked beside each other (ends with a comma).
Here is a working example.
https://docs.google.com/spreadsheets/d/1MGaqqGrkmIliuAzEqxPtdEVZXWPN2K5W7jFFM-ZnwgE/edit?usp=sharing
If I missed something you were trying to achieve, let me know.
Also, I'm sure that there is a more elegant way to do this, or one not requiring the use of a block of "reserved" blank cells, but I couldn't think of that at the moment.
Edit: The formula as follows also works. But you need to remember to set the "100" value to be equal to the number of rows in your data block, since we pad the columns with the necessary number of blanks rows, after removing the blank cells in each column.
={
{(A1:C1)};
{FILTER(A2:A,A2:A<>"");indirect("N1:N" & 100-counta(A2:A))},
{FILTER(B2:B,B2:B<>"");indirect("N1:N" & 100-counta(B2:B))},
{FILTER(C2:C,C2:C<>"");indirect("N1:N" & 100-counta(C2:C))}}
I'm trying to make a Google spreadsheet where I want the sum of the values in the row to appear in the AH cell of that row.
The row would be populated with letters like L or X and I'm using COUNTIF to give value to the alphabet characters.
For example,
=COUNTIF(C4:AG4,"X")*9 + COUNTIF(C4:AG4,"L")*12
How can I write the range such that it looks at cells C through AG from the same row the formula is in rather than change it for every row?
You don't need to change the formula, if you write that formula in one cell and then you drag the little square at the bottom right of the cell, excel will automatically change the row number
Just copy downwards:
As you see, the row index changed to 5 automatically.
Let's say I have 6 different cells (that are not all in a line). I want to check if the values in these cells are equal. How could I do this with a function? I'd want the function simply to display "EQUAL" or "NOT EQUAL" (or maybe change the cell background color?).
One option for 6 cells would be this:
=IF(AND(A1=B2,B2=C3,C3=D4,D4=E5,E5=F6),"EQUAL","NOT EQUAL")
Another option - this way you don't need to reference the same cell twice:
=IF(AND(ARRAYFORMULA(A1={B2,C3,D4,E5,F6})),"EQUAL","NOT EQUAL")
If you wanted to color some cells if values in these cells are equal, you would need to create a Conditional Formatting rule with a similar formula:
Select the cells you want to color
Format > Conditional Formatting
Select "Custom formula is"
Fill in one of the above formulas without the IF part of formula, e.g.
=AND(ARRAYFORMULA(A1={B2,C3,D4,E5,F6}))
Select the formatting style (color)
Done
as formula for conditional formatting:
=countunique({A1,B2,C3,D4,E5,F6})=1
as function it would be similar: =if([formula],"EQUAL","NOT EQUAL"):
update: the requested clarification:
put the wanted cells in a custom array {A1,B2,C3,D4,E5,F6}
(delimiters: , = new column, ; = new row; for countunique either is fine)
get the unique values countunique(...)
if the outcome is 1 then all values are the same
update 2: original answer used =count(unique(...)) instead of the combined function =countunique(...)
Here's an option without using an array that is easier to implement for large numbers of cells:
=IF(MIN(A1,B2,C3,D4,E5,F6)=MAX(A1,B2,C3,D4,E5,F6),"Equal", "Not Equal")
I'm trying to apply conditional formatting to a range of cells.
If cell 'C5' as per picture is not empty then apply colour yellow to cells C6:C12.
What I have tried changes the colour of only one cell i.e. just C6.
Please try selecting C6:C12, Format, Conditional formatting..., Format cells if..., Custom formula is:
=C$5<>""
and chose yellow, Done.
To achive that, you should select the Cells you want the conditional formatting to apply to, and then set the Formatting to "Custom formular", which should be
=$C$5<>""
The $ means, that neither the Column nor Row should advance with the target area.
So
=$G1<>""
would mean, that if your Target Area was A1:F4 (the first four cells of the first six columns), that the formatting would depend on G1 for A1:F1, on G2 for A2:F2 and so on (the Row is advancing in this example, while the column is still fixed as being G.)
You need both $ - i.e. =$C$5<>""
Just tested and works here.