umya-spreadsheet icon indicating copy to clipboard operation
umya-spreadsheet copied to clipboard

Copying cells with formulas

Open erosinnocenti opened this issue 1 year ago • 11 comments

Hi! Thanks for this great library. I'm writing an issue to ask help about cloning cells.

I implemented a simple function which iterates through all the cells of a row and clones them. After that, i used get_cell_mut using the coordinates of the target row to create the new cells, and then copied the contents using set_style(from_cell.get_style().clone()), set_cell_value(from_cell.get_cell_value().clone)) etc. The problem is that the formulas are copied but the references are still the same of the original row.

Is there a way to copy cells from position to another updating its formulas references with the row/column offsets?

erosinnocenti avatar May 11 '24 10:05 erosinnocenti

@erosinnocenti Thank you for contacting us. Sorry, formulas offsets are not supported. umya-spreadsheet keeps formulas as string.

Although not smart, an example implementation is shown below.

let mut formula = get_cell_value().get_formula();
formula = // Update value
get_cell_value_mut().set_formula(formula);

MathNya avatar May 13 '24 09:05 MathNya

Thanks for your reply and the code snippet. Do you think this feature could be implemented in the near future or is it not in your interest?

erosinnocenti avatar May 13 '24 12:05 erosinnocenti

The ability to parse formuals has been discussed for some time. However, it is expected to take some time due to the difficulty involved in implementing it. https://github.com/MathNya/umya-spreadsheet/issues/148

MathNya avatar May 14 '24 06:05 MathNya

We were able to respond. Please obtain the latest version and check it.

MathNya avatar Aug 04 '24 07:08 MathNya

We were able to respond. Please obtain the latest version and check it.

Hello @MathNya! Thanks for your effort, I'm on holidays right now. As soon as I'm back to office, I'll test it!

erosinnocenti avatar Aug 22 '24 19:08 erosinnocenti

Hi, today I updated the dependency, but still I encounter the same problem.
Could you please give me a snippet on how to copy a cell (which contains a formula), to another cell updating the references?

I give you an example, to better clarify the need:

  • Cell C1 contains formula =A1+B1
  • I want to copy C1 into C2 and get it automatically updated as =A2+B2

Thank you

erosinnocenti avatar Aug 26 '24 07:08 erosinnocenti

@erosinnocenti Sorry, I thought this issue was complete since I implemented the relative position calculation for the function. I forgot to reflect it in the copied cells. I will address this in the next update. (I don't think it will take too long.)

MathNya avatar Aug 26 '24 09:08 MathNya

@erosinnocenti Sorry, I thought this issue was complete since I implemented the relative position calculation for the function. I forgot to reflect it in the copied cells. I will address this in the next update. (I don't think it will take too long.)

No problem, thanks for your effort.
When the next updated is released I'll try again.

erosinnocenti avatar Aug 26 '24 09:08 erosinnocenti

The latest version has been released. Please try it out.

let mut cell = sheet.get_cell("C4").unwrap().clone();
cell.set_coordinate("C5");
sheet.set_cell(cell);

MathNya avatar Sep 23 '24 08:09 MathNya

Hello, thanks for the reply. I just tried the new version and I confirm that is working fine. I tested only a basic expression (i.e., =A1+B1) and after copying to row 2 it changed to =A2+B2 as expected.

erosinnocenti avatar Sep 26 '24 08:09 erosinnocenti

Frankly speaking, this library is getting really good.

Pity that some of the API is not very Rust-y...

Maybe we can do an overhaul for 2.0...

schungx avatar Sep 27 '24 01:09 schungx