sestegra / spreadsheet_decoder

Spreadsheet Decoder is a library for decoding spreadsheets for ODS and XLSX files.
MIT License
55 stars 21 forks source link

fix for absolute path target #34

Closed wapa5pow closed 2 years ago

wapa5pow commented 2 years ago

Problem

Some excel library (for example, Styleframe as python library) outputs excel file which contains xl/_rels/workbook.xml.rels file's Target differently as follows.

Cursor_and_StyleframeTest_workbook_xml_rels

With the current spreadsheet_decoder, it generates error.

How to reproduce

To generate excel file which has the problem, execute the following python file.

from styleframe import StyleFrame
from datetime import date

sf = StyleFrame({
    'a': [1],
    'b': [2],
})

ew = StyleFrame.ExcelWriter(r'data.xlsx')
sf.to_excel(ew, 'sheet1')
ew.save()

With the above file, run the following command.

$ pip install styleframe 
$ python convert.py

There will be "data.xlsx" in the same directory.

When read the above file with the current spreadsheet_decoder, it says the following error. You can use the following file and command to generate the error.

import 'dart:io';

import 'package:spreadsheet_decoder/spreadsheet_decoder.dart';

Future<void> main(List<String> arguments) async {
  final tmp = 'data.xlsx';
  final bytes = File(tmp).readAsBytesSync();
  final decoder = SpreadsheetDecoder.decodeBytes(bytes);
}
$ dart convert_excel.dart 
Unhandled exception:
type 'Null' is not a subtype of type 'List<int>'
#0      XlsxDecoder._parseTable (package:spreadsheet_decoder/src/xlsx.dart:314:49)
#1      XlsxDecoder._parseContent.<anonymous closure> (package:spreadsheet_decoder/src/xlsx.dart:301:7)
#2      Iterable.forEach (dart:core/iterable.dart:279:35)
#3      XlsxDecoder._parseContent (package:spreadsheet_decoder/src/xlsx.dart:300:39)
#4      new XlsxDecoder (package:spreadsheet_decoder/src/xlsx.dart:116:5)
#5      _newSpreadsheetDecoder (package:spreadsheet_decoder/src/spreadsheet.dart:39:14)
#6      new SpreadsheetDecoder.decodeBytes (package:spreadsheet_decoder/src/spreadsheet.dart:76:12)
#7      main (...)
#8      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295:32)
#9      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)```

How to fix

When Target has absolute path, the variable name namePath on the fixed code should not be with xl/$target but target.substring(1).

Related issue

https://github.com/OfficeDev/Open-XML-SDK/issues/88 https://github.com/tidyverse/readxl/issues/294

sestegra commented 2 years ago

@wapa5pow Great PR, thanks for contributing to the package.