cancel
Showing results for 
Search instead for 
Did you mean: 

SAP UI5 Excel Export - duplicating last file

0 Kudos

I am trying to download JSON data to an excel. It works fine except when I am running a for loop and trying to download the array in chunks of 25, it always duplicates the last file. More specifically, if there is only one record in the array, still 2 files get created.

Below is my code:

var i, j, temparray, chunk = 25, fileNumber; 

var fileName = "Order export-"; 

for (i = 0, j = oModel.length, fileNumber = 1; i < j; i += chunk, fileNumber++) { 

temparray = oModel.slice(i, i + chunk); 
oSettings = {
workbook: {
columns: aCols
},
dataSource: temparray,
fileName: fileName.concat(fileNumber)
};

var oSpreadsheet = new sap.ui.export.Spreadsheet(oSettings); 

oSpreadsheet.build().then(function () {
sap.m.MessageToast.show("Spreadsheet export has finished");
});
maheshpalavalli
Active Contributor
0 Kudos

Hi Arati,

So for one record, the code inside the for loop is executed 2 times and it is create 2 excel sheets?

BR,

Mahesh

0 Kudos

maheshkumar.palavalli : For one record, the for loop will execute only once. then it does i+= chunk, then i becomes greater than j and loop does not execute the lines below it. But the control then goes to print spreadsheet and duplicates the record. For example, if I set a counter inside the for loop, the counter will at the end have value 1 as it got executed once, but there will be two files being printed.

0 Kudos

The file does not print for every loop. It prints everything at the end. I think that's somewhere the issue is.

maheshpalavalli
Active Contributor
0 Kudos

Hi Arati Deshpande,

The same code I copy pasted and it is working for me.

I tested with 2 records and selected chunks as 1. It is generating 2 excel sheets. am I missing something else?

Also, can you check if the you have written again some code after the for loop to generate the excel sheet.

In your comment i didn't understand this

But the control then goes to print spreadsheet and duplicates the record

what does it means that it will not execute the lines below it but the control goes to the print spread sheet statement?

BR,

Mahesh

0 Kudos

maheshkumar.palavalli yes. I tried this in debug many times. None of the other lines were executed but code control simply went to spreadsheet code.

One example can be, if you have any spreadsheet download code, just add a for loop around it and test for few iterations. It should replicate the issue.

maheshpalavalli
Active Contributor
0 Kudos
Hi Arati Deshpande

Same your code copied and it is working, check the below jsbin sample

https://jsbin.com/puzuliq/edit?html,output

BR,

Mahesh

0 Kudos

maheshkumar.palavalli strange. Same thing does not work for me.

maheshpalavalli
Active Contributor
0 Kudos

Hi Arati, you need to provide your total js code for this, hide any confidential data and provide us the complete controller js file.

0 Kudos

maheshkumar.palavalli I went through my code again and there was a if else condition... Inside if condition this entire for loop is present. My mistake was after else , I forgot to give else code in {}. So excel download code for else condition was without any {} and it was just running once (as it is not in for loop). When if condition was passing it would run for entire cycle of the for loop and then once for the else too. I added {} and it got resolved. Thank you very much for your quick responses yesterday and help.

maheshpalavalli
Active Contributor

Hi Arati Deshpande

I expected that and so I asked you to provide the whole source code 🙂

Please close this question.

BR,

Mahesh

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member560141
Participant
0 Kudos

It sounds like you have a issue with a pointer to the RAM. You are not creating any copy of the data. Only pointers. This is why it is using the last one. It is the last to create the pointer. You can solve this with extending your data if i am correct. please try something like this

//create a "copy" of a object
var temparray = $.extend(true, {}, oModel.slice(i,i+ chunk));

0 Kudos

Thank you for feedback. I tried doing that but I get the below error:

Uncaught TypeError: Cannot read property 'match' of undefined at Object.l [as parse] (sapui5.hana.ondemand.com/1.52.4/resources/sap/ui/export/js/libs/uri.all.min.js:2) at r (sapui5.hana.ondemand.com/1.52.4/resources/sap/ui/export/js/XLSXExportUtils.js:1) at Object.c [as fetch] (sapui5.hana.ondemand.com/1.52.4/resources/sap/ui/export/js/XLSXExportUtils.js:1) at onmessage (sapui5.hana.ondemand.com/1.52.4/resources/sap/ui/export/js/SpreadsheetWorker.js:1)

The file does not print for every loop. It prints everything at the end. I think that's somewhere the issue is.