Giter VIP home page Giter VIP logo

Comments (55)

vjustov avatar vjustov commented on July 17, 2024 1

Hello @randym, how one should approach a PR for this feature? what's to take into account?

from axlsx.

ykessler avatar ykessler commented on July 17, 2024 1

Re calculating height manually, see #125 (comment) for help in calculating text width

from axlsx.

jprince avatar jprince commented on July 17, 2024 1

Here's what I ended up implementing. Hopefully it can be of use to someone else in the future...

It loops over each column in a row, taking the cell value, splitting the string on line breaks (in case the content contains line breaks), then compares the length of each text_line to the column width to determine how many physical lines it spans in the sheet. Then add them together for each cell and take the cell with the most total text_lines and use the font size to translate that to a row height.

def infer_row_height(row)
  physical_lines = row.each_with_index.map do |cell, column_index|
    text = cell.value
    column_width = row.worksheet.column_info[column_index].width

    text_lines = text.to_s.lines # handle newlines entered by the user
    text_lines.map { |line| (string_width(line, row) / column_width.to_f).ceil }.sum
  end.max
  row.height = (physical_lines * FONT_SIZE) + LINE_PADDING # in my case 11, 5
end

# Copied directly from caxlsx: https://github.com/caxlsx/caxlsx/blob/ebd11df1c11e3fe3eb0870e4c43d7ff8771ffe0d/lib/axlsx/workbook/worksheet/cell.rb#L437
def string_width(string, row)
  font_scale = Style::FONT_SIZE / row.worksheet.workbook.font_scale_divisor
  (string.to_s.size + 3) * font_scale
end

In code for generating the workbook:

data.each { |d| sheet.add_row(d) }
sheet.column_widths(*columns.map { |c| c[:width] }) # must be set after adding data

# needs to run after column_widths is set
sheet.rows.each { |row| infer_row_height(row) }

from axlsx.

cj avatar cj commented on July 17, 2024

+1

from axlsx.

horaciob avatar horaciob commented on July 17, 2024

+1

from axlsx.

scanferla avatar scanferla commented on July 17, 2024

+1

from axlsx.

morkevicius avatar morkevicius commented on July 17, 2024

+1

from axlsx.

dszczyt avatar dszczyt commented on July 17, 2024

+1

from axlsx.

indrekj avatar indrekj commented on July 17, 2024

+1

from axlsx.

murphysw avatar murphysw commented on July 17, 2024

+1

from axlsx.

rafadc avatar rafadc commented on July 17, 2024

Can't you accomplish this through word wrap?

from axlsx.

osegrums avatar osegrums commented on July 17, 2024

+1

from axlsx.

rvsingh avatar rvsingh commented on July 17, 2024

+1

from axlsx.

rujaun avatar rujaun commented on July 17, 2024

+1

from axlsx.

panasyuk avatar panasyuk commented on July 17, 2024

+1

from axlsx.

songjiayang avatar songjiayang commented on July 17, 2024

+1

from axlsx.

hofdesu avatar hofdesu commented on July 17, 2024

+1 Is there any workaround for this in the meantime that people have come up with?

from axlsx.

jurriaan avatar jurriaan commented on July 17, 2024

You can just use word wrap or you can count the number of lines and multiply that by the +/- the font size

from axlsx.

trestles avatar trestles commented on July 17, 2024

could you do a gist for handling this? or is there a way to query the worksheet for the number of rows given a fontSize and a certrain string of text?

from axlsx.

hofdesu avatar hofdesu commented on July 17, 2024

Yeah, so I was using wordwrap but still had rows and words cut off.

Ended up writing a helper to do similar to what you suggest @jurriaan . Checked the cells autowidth value, then taking in to account that, any new lines, and a bit of a buffer for whitespace at the end of each line, assign a new row hight.
....
A bit (quite) flakey, but quick, and it will do the trick in the short term.

from axlsx.

jfrux avatar jfrux commented on July 17, 2024

+1

from axlsx.

Wolfer avatar Wolfer commented on July 17, 2024

+1

from axlsx.

jweissig avatar jweissig commented on July 17, 2024

@hofdesu can you share the helper re: calculating the height?

from axlsx.

jmuheim avatar jmuheim commented on July 17, 2024

@hofdesu yes, could you please show your helper?

from axlsx.

sathibabu-nyros avatar sathibabu-nyros commented on July 17, 2024

+1

from axlsx.

aschyiel avatar aschyiel commented on July 17, 2024

+1

from axlsx.

arturtr avatar arturtr commented on July 17, 2024

+1

from axlsx.

raphaottoni avatar raphaottoni commented on July 17, 2024

+1

from axlsx.

codexorange avatar codexorange commented on July 17, 2024

+1

from axlsx.

xjcbruce avatar xjcbruce commented on July 17, 2024

+1

from axlsx.

flashharry82 avatar flashharry82 commented on July 17, 2024

+1

from axlsx.

karamosky avatar karamosky commented on July 17, 2024

+1

from axlsx.

jbrodie avatar jbrodie commented on July 17, 2024

+1

from axlsx.

HenryTimelessness avatar HenryTimelessness commented on July 17, 2024

+1

from axlsx.

TimotheeVille avatar TimotheeVille commented on July 17, 2024

+1

from axlsx.

gilangmugnirespaty avatar gilangmugnirespaty commented on July 17, 2024

+1

from axlsx.

AndreFSilveira avatar AndreFSilveira commented on July 17, 2024

+1

from axlsx.

eugene-ershov avatar eugene-ershov commented on July 17, 2024

+1

from axlsx.

alexeymoshkin avatar alexeymoshkin commented on July 17, 2024

+1

from axlsx.

h0jeZvgoxFepBQ2C avatar h0jeZvgoxFepBQ2C commented on July 17, 2024

+1

from axlsx.

leoduquesnel avatar leoduquesnel commented on July 17, 2024

+1

from axlsx.

kirylrb avatar kirylrb commented on July 17, 2024

+1

from axlsx.

rusllonrails avatar rusllonrails commented on July 17, 2024

+1

from axlsx.

kleyy avatar kleyy commented on July 17, 2024

+1

from axlsx.

MPM10223 avatar MPM10223 commented on July 17, 2024

+1

from axlsx.

marcinwierzbicki avatar marcinwierzbicki commented on July 17, 2024

+1

from axlsx.

sahilchopra avatar sahilchopra commented on July 17, 2024

+1

from axlsx.

mathieugagne avatar mathieugagne commented on July 17, 2024

+1

from axlsx.

pohodnya avatar pohodnya commented on July 17, 2024

+1

from axlsx.

quochien avatar quochien commented on July 17, 2024

+1

from axlsx.

keangnage avatar keangnage commented on July 17, 2024

+1

from axlsx.

benkoshy avatar benkoshy commented on July 17, 2024

For anyone wanting a cheap hack to get a rough estimate of the row height, here's what worked for me.

def set_row_height(row, text)
	column_width = row.worksheet.column_info[8].width # column 8 is where my data lives
	length_of_text = if text.nil?
					0
				   else
				 	text.length
				 end

	row_height = length_of_text / 52 * 14
	row.height = row_height
end
def get_line_item_data(line_item, line_item_type, sheet)
   line_item_data = [line_item.created_at.strftime("%m/%d/%Y"), line_item.id, line_item.item_index, line_item.quote_id, line_item.quote.project_name, line_item_type , line_item.hours, line_item.price, line_item.scope_description ]
   line_item_data
end

wb = xlsx_package.workbook
wb.add_worksheet(name: "Line Items") do |sheet|	
	sheet.add_row ["Created At", "Line Item Id", "Line Item Index", "Quote ID", "Project Name", "Line Item Type", "Hours", "Price", "Description", "Links......"]

	@line_items.each do |line_item|
		row = sheet.add_row(get_line_item_data(line_item, line_item_type, sheet), style: wrap_text)
		set_row_height(row, line_item.scope_description )
	end
end

Watch out - we've hard coded some values, and remember, the size of the text can change. Overall, it might give you some ideas on where to start. Is it robust? No. Is it pretty? It's just good enough.

from axlsx.

jprince avatar jprince commented on July 17, 2024

@benkoshy can you please clarify the magic numbers? I assume 14 is font size? How did you get the 52 coefficient? Is that supposed to be column_width?

from axlsx.

benkoshy avatar benkoshy commented on July 17, 2024

@jprince sorry mate i completely forgot. I hacked out a solution, but it probably does deserve a fuller treatment: some sort of PR in the main library.

from axlsx.

jbrodie avatar jbrodie commented on July 17, 2024

No worries. It would be a great option to be able to have in this library though.

from axlsx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.