Skip to content

Instantly share code, notes, and snippets.

@richie5um
Last active August 22, 2022 09:57
Show Gist options
  • Save richie5um/81a172235e20f6e8c62401ea57bfa42c to your computer and use it in GitHub Desktop.
Save richie5um/81a172235e20f6e8c62401ea57bfa42c to your computer and use it in GitHub Desktop.
name: Smz-GetDocx-CreateDocx
description: ''
host: WORD
api_set: {}
script:
content: >
$("#run").click(() => tryCatch(getDocumentAsCompressed));
// async function run() {
// await Word.run(async (context) => {
// // this getDocumentAsBase64 assumes a valid base64-encoded docx file
// // var content = getDocumentAsBase64();
// var content = undefined;
// var myNewDoc = context.application.createDocument(content);
// context.load(myNewDoc);
// return context.sync()
// .then(function () {
// myNewDoc.open();
// context.sync();
// }).catch(function (myError) {
// //otherwise we handle the exception here!
// console.log("Error", myError.message);
// })
// }).catch(function (myError) { console.log("Error", myError.message);
});
// }
async function createNewFile(fileContent) {
await Word.run(async (context) => {
var base64String = btoa(fileContent);
console.log(base64String);
// this getDocumentAsBase64 assumes a valid base64-encoded docx file
// var content = getDocumentAsBase64();
var content = undefined;
var myNewDoc = context.application.createDocument(base64String);
context.load(myNewDoc);
return context.sync()
.then(function () {
myNewDoc.open();
context.sync();
}).catch(function (myError) {
//otherwise we handle the exception here!
console.log("Error", myError.message);
})
}).catch(function (myError) { console.log("Error", myError.message); });
}
// The following example gets the document in Office Open XML ("compressed")
format in 65536 bytes (64 KB) slices.
// Note: The implementation of app.showNotification in this example is from
the Visual Studio template for Office Add-ins.
function getDocumentAsCompressed() {
Office.context.document.getFileAsync(Office.FileType.Compressed, { sliceSize: 65536 /*64 KB*/ },
function (result) {
if (result.status == "succeeded") {
// If the getFileAsync call succeeded, then
// result.value will return a valid File Object.
const myFile = result.value;
const sliceCount = myFile.sliceCount;
const docdataSlices = [];
let slicesReceived = 0, gotAllSlices = true;
console.log("File size:" + myFile.size + " #Slices: " + sliceCount);
// Get the file slices.
getSliceAsync(myFile, 0, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
}
else {
console.log("Error:", result.error.message);
}
});
}
function getSliceAsync(file, nextSlice, sliceCount, gotAllSlices,
docdataSlices, slicesReceived) {
file.getSliceAsync(nextSlice, function (sliceResult) {
if (sliceResult.status == "succeeded") {
if (!gotAllSlices) { // Failed to get all slices, no need to continue.
return;
}
// Got one slice, store it in a temporary array.
// (Or you can do something else, such as
// send it to a third-party server.)
docdataSlices[sliceResult.value.index] = sliceResult.value.data;
if (++slicesReceived == sliceCount) {
// All slices have been received.
file.closeAsync();
var fileContent = onGotAllSlices(docdataSlices);
createNewFile(fileContent);
}
else {
getSliceAsync(file, ++nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
}
}
else {
gotAllSlices = false;
file.closeAsync();
console.log("getSliceAsync Error:", sliceResult.error.message);
}
});
}
function onGotAllSlices(docdataSlices) {
let docdata = [];
for (let i = 0; i < docdataSlices.length; i++) {
docdata = docdata.concat(docdataSlices[i]);
}
let fileContent = new String();
for (let j = 0; j < docdata.length; j++) {
fileContent += String.fromCharCode(docdata[j]);
}
// Now all the file content is stored in 'fileContent' variable,
// you can do something with it, such as print, fax...
return fileContent;
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
language: typescript
template:
content: |
<button id="run" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
language: html
style:
content: |-
section.samples {
margin-top: 20px;
}
section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css
[email protected]/client/core.min.js
@types/core-js
[email protected]
@types/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment