excel icon indicating copy to clipboard operation
excel copied to clipboard

Sheet.merge breaks the document

Open migueldma opened this issue 3 years ago • 7 comments

Hi,

Context: I made a copy of an Excel file located in assets directory and saved it in the Temporary folder of the device. Then, I tried to update/change that file.

When I use Sheet.merge it correctly applies the merge but breaks my document, removing all the previous merged cells(the ones that came with the file) and throw an error when you tried to open the file in google docs. If you remove the Sheet.merge or replace it with a simple Cell value insertion, the file works fine. Here's some code:

    Sheet firstSheet = excel['Sheet1'];    
    int rowStart = 9;    
    String colStart = "A";
    String colEnd = "D";

    int oldActionId = -1;
    int row = rowStart;
    String col = colStart;

    data.forEach((element) {
      if(element["action_id"] != oldActionId){
        oldActionId = element["action_id"];
        
        firstSheet.merge(CellIndex.indexByString("$colStart$row"), CellIndex.indexByString("$colEnd$row"), customValue: element["action_name"]); //Breaks the file.        
        row++;
      }
    });

migueldma avatar Mar 30 '21 22:03 migueldma

@migueldma Thanks for posting the issue, I'll have a look into it

justkawal avatar Apr 02 '21 18:04 justkawal

@migueldma Can you provide me a sample structured file? by removing your important data from it.

It would be helpful to know the edge and corner cases.

justkawal avatar Apr 05 '21 04:04 justkawal

@migueldma You can mail on [email protected]

justkawal avatar Apr 05 '21 13:04 justkawal

@migueldma You can mail on [email protected]

Hi, do you need the excel file, the project or both?

migueldma avatar Apr 05 '21 23:04 migueldma

@migueldma I only need your sample excel file for testing, be sure to remove your important and private data from the file, if any.

justkawal avatar Apr 06 '21 03:04 justkawal

Hi, did the file help?

migueldma avatar Apr 13 '21 19:04 migueldma

I don't know if this is a different issue but I've also noticed that editing contents of a merged cell also breaks the document.

maboumra avatar Nov 12 '21 14:11 maboumra

Hello,

the following code breaks the document:

  var month='2023-03'; 
  var excel = Excel.createExcel();
  excel.rename(excel.getDefaultSheet()!, month);
  excel.setDefaultSheet(month);
  Sheet sheet = excel[month];

  // Title
  sheet.merge(CellIndex.indexByString('A1'), CellIndex.indexByString('E1'), customValue: 'Timestamps - ${_monthName(month)} ${_year(month)}');
  sheet.cell(CellIndex.indexByString('A1')).cellStyle = CellStyle(fontSize: 18, bold: true, horizontalAlign: HorizontalAlign.Center,);

 var fileBytes = excel.save();
  var directory = (await getApplicationDocumentsDirectory()).path;

  File(join("$directory\\output_file_name.xlsx"))
    ..createSync(recursive: true)
    ..writeAsBytesSync(fileBytes!);

amigne avatar Mar 12 '23 07:03 amigne

When specifying a string value as customValue parameter, the generated OpenXML code in xl\worksheets\sheet1.xml is

<c r="A1" s="1"><v>Timestamps - Janvier 2023</v></c>

When specifying the value to cell A1, then merging the cells, the generated OpenXML code is

<c r="A1" s="1" t="s"><v>0</v></c>

The second option looks more conform to the specs, as for strings, the <v>...</v> tag should not contain the text itself, but a reference to the string, stored in xl\sharedStrings.xml.

I think this bug is caused by https://github.com/justkawal/excel/blob/12529d8472e8e6ab36e551b090f2205f85ba2916/lib/src/sheet/sheet.dart#L719

The value is directly assigned to the private property _value, instead of calling its setter value, with value.value = customValue.

I don't know if using the private _value property instead of calling its public setter value was made by intent (to prevent other side effects caused by the setter). @justkawal, could you please give me some light on it? Would it be safe to fix this by changing the value._value by value.value?

amigne avatar Mar 13 '23 13:03 amigne

Fixed in #218

FauconSpartiate avatar Mar 17 '23 05:03 FauconSpartiate