excel4node lib to autosize excel cell in order to fit longer text - node.js

Using excel4node lib in order to generate excel docs out of some web tables, is there a possibility to autosize cell when the text is too long to fit it in one cell and to make the width of the cell bigger?
In documentation they have this 2 functions:
ws.column(3).setWidth(50);
ws.row(1).setHeight(20);
But that won't fit the text inside, will just make the cell bigger. Will show example:
and my desired output:
Code for that comment cell:
reducedReport.forEach((element, index) => {
ws.cell(index + 2, 1).string(element["projectName"]);
ws.cell(index + 2, 2).string(element["workerName"]);
ws.cell(index + 2, 3).string(element["comment"]);
ws.column(3).setWidth(30);
ws.row(15).setHeight(40);
ws.cell(index + 2, 4).string(moment(element["date"] * 1000).format("DD-MM-YYYY"));
console.log(element["comment"]);
});
It's about comment column.

This feature is not implemented yet, as exposed in this GitHub issue. In that issue, the author is pointing to an approximation approach used by PHPOffice, to dynamically set the column's width considering the font and the content's length.

you can add alignment settings for cell setting :
alignment: {
shrinkToFit: true,
wrapText: true
}

Related

Increasing height merged cells Apache POI

I am using Apache POI SXSSF to generate xlsx document. The document uses Times New Roman sizes 9 and 11, and the default cell width and height have been changed. The question is how to calculate the height of the merged cells so that all the text fits (the height of the cell must be dynamically set according to the given text)? The server running the application does not have a display, and this code is running in the IBM Integration Bus.
The solution from How to get the needed height of a multi line rich-text field (any font, any font size) having defined width using Java? is not suitable. The server running the application is missing a display and the string int ppi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); returns an exception, and manually picking the ppi value is also not possible. If there is a display, everything works correctly.
And is there any way to use the "align center selection" function somehow?
I found that centering a selection gives a similar result as merging multiple cells horizontally, but I couldn't find an answer anywhere on how to use this in Apache POI. As a result, experimentally, I found out that in order to achieve this effect, you need to do the following things:
Create CellStyle; specify setWrapText(true) and setAlignment(HorizontalAlignment.CENTER_SELECTION) for it
Apply the style created in step 1 to all cells that need to be merged
Specify the value in the first cell
Code example:
Font font = wb.createFont(); // where wb - is SXSSFWorkbook object
font.setFontName("Times New Roman");
font.setFontHeightInPoints((short) 11);
CellStyle style = wb.createCellStyle();
style.setFont(font);
style.setWrapText(true);
style.setAlignment(HorizontalAlignment.CENTER_SELECTION);
for (int i = 0; i <= endCellNum - firstCellNum; i++){ // where endCellNum - number of last cell of selection and firstCellNum is number of first cell of selection
Cell cell = curRow.createCell(firstCellNum + i);
cell.setCellStyle(cs);
if (i == 0){
firstCell = cell;
}
}
firstCell.setCellValue(value);

Python Docx Table row height

So column width is done using cell width on all cells in one column ike this:
from docx import Document
from docx.shared import Cm
file = /path/to/file/
doc = Document(file)
table = doc.add_table(4,2)
for cell in table.columns[0].cells:
cell.width = Cm(1.85)
however, the row height is done using rows, but I can't remember how I did it last week.
Now I managed to find a way to reference the rows in a table, but can't seem to get back to that way. It is possible to change the height by using the add_row method, but you can't create a table with no rows, so the the top row will always be the default height, which about 1.6cms.
There is a way to access paragraphs without using add_paragraph, does anyone know how to access the rows without using the add_row method because it was that that I used to set row height in a table as a default.
I have tried this but it doesn't work:
row = table.rows
row.height = Cm(0.7)
but although this does not give an error, it also has no effect on the height.
table.rows is a collection, in particular a sequence, so you need to access each row separately:
for row in table.rows:
row.height = Cm(0.7)
Also check out row.height_rule for some related behaviors you have access to:
https://python-docx.readthedocs.io/en/latest/api/table.html#row-objects
When you assign to table.rows.height, it just adds a .height instance attribute that does nothing. It's one of the side-effects of a dynamic language like Python that you encounter a mysterious behavior like this. It goes away as you gain more experience though, at least it has for me :)
Some additional information:
The answer here is correct, but this will give a minimum row height. Using WD_ROW_HEIGHT_RULE.EXACTLY will fix the cell height to the set row height ignoring the contents of the cell, this can result in cropping of the text in an undesirable way.
para = table.cell(0,0).add_paragrph('some text')
SOLUTION:
add_paragraph actually adds a blank line above the text.
Use the following instead to avoid using add_paragraph:
table.cell(0,0).paragraphs[0].text = 'some text'
or using add_run can make it easier to also work with the text:
run = table.cell(0,0).paragraphs[0].add_run('some text')
run.bold = True

Appropriate Syntax For Alternating Row Colors

I am using this code:
=IIF(RunningValue(Fields!cost_center_id.Value, CountDistinct, nothing) Mod 2, "White", "Gainsboro")
But its result is not good. I think it's because of Fields!cost_center_id.Value. Can anybody tell me the most appropriate code?
And why the footer (Total) has also shade thought I haven't put code in it? Help me.
Thanks in advance.
Screenshots:
I used this code for the 3rd picture.
= IIF(RowNumber("LCSRDBDataSet_CostCenterSummary") Mod 2 = 0, "White", "Gainsboro")
The reason your expression doesn't work is because your id values might have gaps or not be ordered correctly.
One of these following expressions should give the wanted behavior.
= IIF(RowNumber(Nothing) Mod 2 = 0, "White", "Gainsboro")
= IIF(RowNumber("YourDataSet") Mod 2 = 0, "White", "Gainsboro")
You need to define the expression for the BackgroundColorproperty of the data row. The footer row (total) should be below the data row, and thus have no expression for it's background color, unless you want it to be different to the final row.

Set the column width of the exported Excel file

I am trying to figure out the syntax of using Alasql to specify the column width for an Excel file. I tried something like:
var mystyle = {
headers:true,
column: {width:"300px"}
};
but it didn't work.
Is Alasql capable of doing this and if so, what's the correct syntax to specify it?
Also I would like to know the syntax to specify the column width for some selected columns, for example only the second column should have width of 400px.
You need to specify which column that width belongs to.
var mystyle = {
headers:true,
columns: [{columnid: 'FirstName', width:300},{columnid: 'LastName', width:300}]
};
See working example from owner(had to fixed library reference): http://jsfiddle.net/95j0txwx/2427/

How to encircle Invalid data in Excel?

I want to show a circle around only invalid data.
i have done the complete steps shown in this link
But this circle shown is very big and covers the entire cell.
I want a small circle only covering the data not the entire cell's width.
Data validation is a built in Excel functionality. It checks whole cell value.
So it is not possible, using Data validation, to accomplish what your trying.
It MAY BE POSSIBLE using VBA, shapes, events and (hard) parsing character rendering. In your place, I would be glad with this very big circles!!! :)
I agree with #LS_dev. See this MS Article about changing data validation for printing. Try modifying it to loop through all your data validation and change the width and height.
You can probably do it with this part of the code by changing the width and height:
If Not c.Validation.Value Then
Set o = ActiveSheet.Shapes.AddShape(msoShapeOval, _
c.Left - 2, c.Top - 2, c.Width + 4, c.Height + 4)
o.Fill.Visible = msoFalse
o.Line.ForeColor.SchemeColor = 10
o.Line.Weight = 1.25

Resources