Dear,
In the HTML macro on a confluence page, I managed to get data from an attached excel file in a javascript variable. I could then do all sorts of manipulation. However, I did not manage to upload the processed file back to the attachments. Here is what I did already:
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.16.0/xlsx.full.min.js"></script>
var xldata; var workbook; var firstSheet; var result; var fresult=[]; var wb; var ws; var outData;
var url2="http://xxxxxxxx.com/download/attachments/xxxxxxx/Test.xlsx";
var request2 = new XMLHttpRequest();
request2.responseType = "arraybuffer";
request2.open("GET", url2, true);
request2.send(null);
request2.onload = function() {xldata = new Uint8Array(request2.response);}
With the javascript from the XLSX library, I could turn this into a new excel file.
// read the data from xldata into an XLSX object
workbook = XLSX.read(xldata , {type: 'array'});
// get a handle to the first sheet
firstSheet = workbook.Sheets[workbook.SheetNames[0]];
// put the data of the first sheet into a javascript variable result
result = XLSX.utils.sheet_to_json(firstSheet, { header: 1 });
//put the array into a structure (given the first row was a HEADER row)
result.forEach(function(row,i){if (i>0){var trow={}; row.forEach(function(elm,j){trow[result[0][j]]=elm});fresult.push(trow)}});
//create a new workbook
wb=XLSX.utils.book_new();
// create a new worksheet into the workbook and append a sheet
ws=XLSX.utils.json_to_sheet(fresult);
XLSX.utils.book_append_sheet(wb,ws,"NieuwLanden");
document.getElementById("landen").innerHTML="fresult: \n"+JSON.stringify(fresult);
So far so good, everything is working as expected!
There are two ways I can turn that workbook object into a file
// create variable with the "file" data
var outData = XLSX.write(wb, {bookType:'xlsx', type: 'binary'});
// or create a BLOB (that is automatically downloaded).
var data = XLSX.writeFile(wb, "nnew.xlsx");
The downloaded file is exactly what I expect it to be. A new excel book with the same data.
I would want to have this now - in pure javascript - uploaded to the attachments.
Here is what I tried.
var path="https://xxxxxxxxxxxx.com/rest/api/content/xxxxxxxxxx/child/attachment"
var request3 = new XMLHttpRequest();
request3.open("POST",path,true);
request3.setRequestHeader("X-Atlassian-Token", "nocheck");
request3.send(outData);
Then I get the error
XHR failed loading: POST “https://…/child/attachment”
POST “https://…/child/attachment” net::ERR_CONNECTION_TIMED_OUT
I have no idea what I do wrong…