Skip to content

Instantly share code, notes, and snippets.

@jipyua
Last active March 6, 2018 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jipyua/d1313b663d5d5162f03bf6347759bb39 to your computer and use it in GitHub Desktop.
Save jipyua/d1313b663d5d5162f03bf6347759bb39 to your computer and use it in GitHub Desktop.
Shared with Script Lab
name: GetFileAsyncPPT
description: ''
author: jipyua
host: POWERPOINT
api_set: {}
script:
content: |
$('#selectedFile').change(getFullFile);
function getFullFile() {
var startTime, getFileTime, getAllSlicesTime;
startTime = Date.now();
Office.context.document.getFileAsync("pdf", { sliceSize: 4194304 }, function (result) {
if (result.status == "succeeded") {
// If the getFileAsync call succeeded, then result.value will return a valid File Object
var myFile = result.value;
var sliceCount = myFile.sliceCount;
var slicesReceived = 0, isAllSlicesSuccess = true, docdataSlices = [];
//document.getElementById("result").innerText = "File size:" + myFile.size + "#Slices: " + sliceCount;
//app.showNotification(" File size:" + myFile.size + " #Slices: " + sliceCount, "");
// Iterate over the file slices
for (var i = 0; i < sliceCount && isAllSlicesSuccess; i++) {
myFile.getSliceAsync(i, function (sliceResult) {
if (sliceResult.status == "succeeded") {
if (!isAllSlicesSuccess) { // Some slice has failed to get, no need to continue
//app.showNotification("Error", "One slice failed to get");
return;
}
//app.showNotification("Success", "i: " + i);
// One chunk was got, store it in a temporal array
// or you can do something with the chunk, such as sent it to a third party server
docdataSlices[sliceResult.value.index] = sliceResult.value.data;
if (++slicesReceived == sliceCount) {
getAllSlicesTime = Date.now();
var performance = (getAllSlicesTime - startTime) / 1000.0;
//app.showNotification("Success", "All slices has been get, Seconds: " + performance);
// All slices have been received
myFile.closeAsync(function (closeRes) {
if (closeRes.status == "succeeded") {
// app.showNotification("Close Success", "Success");
}
else {
//app.showNotification("Close Error", closeRes.error.message);
}
});
onGetAllSlicesSucceeded(docdataSlices, false);
}
}
else {
isAllSlicesSuccess = false;
myFile.closeAsync(function (closeRes) {
if (closeRes.status == "succeeded") {
// app.showNotification("Close Success", "Success");
}
else {
//app.showNotification("Close Error", closeRes.error.message);
}
});
//app.showNotification("Get Slice Error:", sliceResult.error.message);
}
});
}
}
else {
getFileTime = Date.now();
var performance = (getFileTime - startTime) / 1000.0;
//app.showNotification('Get File Error:', "Seconds: " + performance + " " + result.error.message);
}
});
function onGetAllSlicesSucceeded(docdataSlices, copyDoc) {
var docdata = [];
for (var i = 0; i < docdataSlices.length; i++) {
docdata = docdata.concat(docdataSlices[i]);
}
var fileContent = new String();
for (var j = 0; j < docdata.length; j++) {
fileContent += String.fromCharCode(docdata[j]);
}
// Now all the file content is stored at variable 'fileContent'
// you can do other things with it, such as print, fax...
if (copyDoc) {
Word.run(function (ctx) {
var doc = ctx.application.createDocument(btoa(fileContent));
doc.open();
return ctx.sync().then(function () {
//app.showNotification("Success", "Current document copied!");
});
}).catch(function (error) {
//app.showNotification("Error", error.message + JSON.stringify(error));
});
}
else {
var popup = window.open("data:application/octet-stream," + escape((fileContent)), "child");
//var popup = window.open("data:application/pdf," + escape((fileContent)), "child");
}
}
}
language: typescript
template:
content: "<form>\n\tselct a doc to be created <input type=\"file\" id=\"selectedFile\">\n\t<br><br><br><br> \n\n</form>"
language: html
style:
content: |
/* Your style goes here */
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/beta/hosted/office.js
https://appsforoffice.microsoft.com/lib/beta/hosted/office.d.ts
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
core-js@2.4.1/client/core.min.js
@types/core-js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts
jquery@3.1.1
@types/jquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment