ActionScript. Can't get data from local xls file - excel

I want to get data from xls file with help of as3xls.
private var file:FileReference = new FileReference();
private var sheet:Sheet;
private var loadedFile:ByteArray;
private function sendLogistImport():void {
file.addEventListener(Event.SELECT, onFileSelect);
file.browse();
file.addEventListener(Event.COMPLETE,
function (e:Event):void {
loadedFile = e.currentTarget.data as ByteArray;
var excelFile:ExcelFile = new ExcelFile();
excelFile.loadFromByteArray(loadedFile);
sheet = excelFile.sheets[0];
Alert.show(sheet.getCell(1,0).value);
}
);
}
private function onFileSelect(e:Event):void {
file.load();
}
But I get this error at the excelFile.loadFromByteArray(loadedFile);:
Error: Error #2030: End of file was encountered.
at flash.utils::ByteArray/readUTFBytes()
at com.as3xls.xls::ExcelFile/boundsheet()[/Users/manuelwudka-robles/Documents/Flex Builder 3/as3xls/com/as3xls/xls/ExcelFile.as:633]
at Function/http://adobe.com/AS3/2006/builtin::call()
at com.as3xls.xls::ExcelFile/loadFromByteArray()[/Users/manuelwudka-robles/Documents/Flex Builder 3/as3xls/com/as3xls/xls/ExcelFile.as:309]
at Function/<anonymous>()[D:\logist.as:244]
This is my xls file's screenshot :
Here is my xls file:
http://www.filedropper.com/template_1
Please tell me, where is my mistake?

I've tested it with various XLS formats, and it can't read any of them. Judging from the comments, the support for the various XLS formats is rather bad.
Try it with different XLS formats, maybe you find one that works, other than that I'm afraid I can't help you, fixing the problem in the library is a monstrous task, and I wouldn't touch it with a ten-foot pole: http://www.openoffice.org/sc/excelfileformat.pdf
Also check out some of the forks around the net, like for example https://github.com/djw/as3xls, they may have some fixes implemented that get you going.
And if nothing work for you, then I'd suggest to look for another library, and/or even use a totally different file format (like CSV) if that is an option.

Related

How to add object to file in nodejs?

title pretty much explains it all. I'm trying to add objects into a nodejs file and cant seem to get it working.
Each file essentially looks like this
[{"name":name,"date":date},{"name":name,"date":date}] (in simplest terms)
I want to be able to add an object, to that array that is in that file. Here is the code I came up with
for(o in collections){
fs.readFile(__dirname + "/HowIsCollections/"+collections[o].mintDate,'utf8',function(err,data){
const dat = JSON.parse(data)
const existedData = []
//console.log(existedData)
for(i in dat){
existedData.push(JSON.stringify(dat[i]))
}
const project = JSON.stringify(collections[o])
if(!existedData.includes(project)){
console.log("?")
dat.push(project)
}
fs.writeFileSync(__dirname + "/HowIsCollections/"+collections[o].mintDate,JSON.stringify(dat))
console.log("????")
})
}
Its pretty self explanatory. From the top, its reading the file, getting the data, taking all of the objects found in the file and putting it into an array.
the second half of the code, stringifys each object, it then compares against the array to see if that object exists in the array (existedData, the data from the file). If it doesnt, it adds it. Then at the end im just resaving the file.
dat.push(project) is the array in the file.
I have similar setups like this in other parts of my code, which work. this however does not, i get no errors, nothing, it just doesnt work. All of my console.log's show, but thats it.
I tried looking on here mostly for solutions, but most of them were just talking about stringifying an object in fs.writefile, which isnt what i need here.

cyber-duck/laravel-excel file corrupt / invalid format

I've spent hours finding out why the excel export with the package cyber-duck/laravel-excel export OK the excel when the datasource is a query, but when using a custom serialiser it simply stops formatting the excel correctly
No errors in code, super simple excel. Even trying the code posted in the documentation:
Usage:
$serialiser = new CustomSerialiser();
$excel = Exporter::make('Excel');
$excel->load($collection);
$excel->setSerialiser($serialiser);
return $excel->stream('filename.xlsx');
CustomSerialiser:
namespace App\Serialisers;
use Illuminate\Database\Eloquent\Model;
use Cyberduck\LaravelExcel\Contract\SerialiserInterface;
class ExampleSerialiser implements SerialiserInterface
{
public function getData($data)
{
$row = [];
$row[] = $data->field1;
$row[] = $data->relationship->field2;
return $row;
}
public function getHeaderRow()
{
return [
'Field 1',
'Field 2 (from a relationship)'
];
}
}
Any thoughts?
What software do you use to open the file? Excel? OpenOffice?
If you open the test folder > Unit > ExporterTest.php, you should see a working example in test_can_use_a_custom_serialiser.
You can change row 155 to $exporter = $this->app->make('cyber-duck/exporter')->make('Excel');, row 160 to $reader = ReaderFactory::create(Type::XLSX); (otherwise it would use a CSV) and comment out line 174 to keep the file so you can open it after the test has running.
The ExampleSerialiser you posted needs to be modified to match your Eloquent model and relationships. Also, the example use an Eloquent version and you mentioned the query builder. If you want to use the Query builder version, you need to use loadQuery (I'll try to update the documentation next week to cover this user case). Feel free to drop me an email with your code so I can have a look and try to help out (It's a bit hard to understand the issue without seeing the actual implementation). You should find me on github, I'm one of the Cyber-Duck guys working on our projects.

Online Word breaks when inserting a content control

I have tested this with Script Lab (https://www.microsoft.com/en-us/garage/profiles/script-lab/) and it seems saving a document (automatically) to One Drive breaks Word if the document has something in it inserted through an add in (reproduced with a different add in that uses the same piece of code).
This is the code for Script Lab:
$("#run").click(run);
var content = '<span>Hello, World!</span>';
async function run() {
Word.run(function (context) {
var range = context.document.getSelection();
var cc = range.insertContentControl();
var ccRange = cc.insertHtml(content, 'replace');
cc.tag = 'citation';
cc.select('end');
context.load(cc);
context.load(range);
context.load(ccRange);
return context.sync();
})
}
The code above successfully inserts content to selection. However, if I wait for a couple of moments after inserting, Word breaks on saving and asks me to send an error report. Same code does not produce any errors on the desktop version of Word. Also, same piece of code has been used by our add in for months now without any issues until now.
Any ideas what is this about? Is there something wrong with the online version of Word or should the code for inserting be somehow updated?

Is there a way to get all embedded objects in .xlsx file using xssf event mdel api

Is there a way to get all embedded objects in .xlsx file using xssf event model api?
Usermodel has the method workbook.getallembedds...similarly is there anything in eventmodel?
This is an example in usermodel.I want to implement the same functionality using eventusermodel.Kindly help.
for (PackagePart pPart : workbook.getAllEmbedds()) {
String contentType = pPart.getContentType();
if (contentType.equals(------)
Instead of xssfworkbook(in usermodel), in the eventmodel code i have a containerObject of type OPCPackage.
#Gagravarr : Thanks for your reply. I tried using the method suggested by you...but im unable to get the contents of the embedded excel.Could you please help me find out where I am going wrong.Here is a part of the code:
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(container);
XSSFReader xssfReader = new XSSFReader(container);
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator)xssfReader.getSheetsData();
for(PackageRelationship rel : iter.getSheetPart().getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
for (PackagePart pPart :getAllEmbedds()) {
String contentType = pPart.getContentType();
// Excel Workbook - OpenXML file format
if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")) {
OPCPackage excelObject = OPCPackage.open(pPart.getInputStream());
`
Your best bet is probably just to enumerate all the package parts, and find the ones that interest you from that
Alternately, the logic to identify embedded parts attached to a given sheet is pretty simple:
List<PackagePart> embedds = new LinkedList<PackagePart>();
// Get the embeddings for the workbook
for(PackageRelationship rel : sheet.getSheetPart().getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
for(PackageRelationship rel : sheet.getSheetPart().getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
return embedds;
Finally all I used was this!
ArrayList<PackagePart> parts = container.getParts();
for (PackagePart pPart :parts) {
String contentType = pPart.getContentType();
if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {

Reading/Editing XLIFF using C#

I need to parse an XLIFF file using C#, but I'm having some trouble. These files are fairly complex, containing a huge amount of nodes.
Basically, all I need to do is read the source node from each trans-unit node, do some processing on it, and insert the processed text into the corresponding target node (which will always be present, but empty).
An example of one of the nodes I need to parse would be (the whole file may contain 100s of these):
<trans-unit id="0000000002" datatype="text" restype="string">
<source>Windows Update is not installed</source>
<target/>
<iws:segment-metadata tm_score="0.00" ws_word_count="6" max_segment_length="0">
<iws:status target_content="placeholders_only"/>
</iws:segment-metadata>
<iws:boundary-seg sequence="bs20721"/>
<iws:markup-seg sequence="0000000001">
</trans-unit>
The trans-unit nodes can be buried deep in the files, the header section contains a lot of data. I'd like to use LINQ to XML to read the data, but I'm not having any luck getting it to work. Here's my current code (just trying to read and output the source nodes from the file:
XDocument doc = XDocument.Load(path);
Console.WriteLine("Before loop");
foreach (var transUnitNode in doc.Descendants("trans-unit"))
{
Console.WriteLine("In loop");
XElement sourceNode = transUnitNode.Element("source");
XElement targetNode = transUnitNode.Element("target");
Console.WriteLine("Source: " + sourceNode.Value);
}
I never see 'In loop' and I don't know why, can someone tell me what I'm doing wrong here, or suggest a better way to achieve what I'm trying to do here?
Thanks.
Try
XNamespace df = doc.Root.Name.Namespace;
foreach (XElement transUnitNode in doc.Descendants(df + "trans-unit"))
{
XElement sourceNode = transUnitNode.Element(df + "source");
// and so one, use the df namespace object to qualify any elements names
}
See also http://msdn.microsoft.com/en-us/library/bb387093.aspx.

Resources