Skip to content

Instantly share code, notes, and snippets.

@jipyua
Last active April 17, 2020 12:13
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/2fd4816d2386e4478f062e672350a0fa to your computer and use it in GitHub Desktop.
Save jipyua/2fd4816d2386e4478f062e672350a0fa to your computer and use it in GitHub Desktop.
Sample code for WorkSheetCollection.AddFromBase64()
name: WorkSheetCollection.AddFromBase64()
description: Sample code for WorkSheetCollection.AddFromBase64()
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(getExcelFileBase64));
function getExcelFileBase64() {
var file = document.getElementById("myFile").files[0];
if (file == null) {
addFromBase64("");
}
var reader = new FileReader();
reader.onload = (function (theFile) {
return function (e) {
var startIndex = e.target.result.indexOf("base64,");
var mybase64 = e.target.result.substr(startIndex + 7, e.target.result.length);
addFromBase64(mybase64);
};
})(file);
reader.readAsDataURL(file);
}
async function addFromBase64(base64) {
await Excel.run(async (ctx) => {
var worksheet = ctx.workbook.worksheets.getActiveWorksheet();
var newWorkSheets = ctx.workbook.worksheets.addFromBase64(base64, [], Excel.WorksheetPositionType.before, worksheet);
await ctx.sync();
var newSheetNames = "";
for (var i = 0; i < newWorkSheets.value.length; i++) {
var newSheet = ctx.workbook.worksheets.getItem(newWorkSheets.value[i]);
newSheet.load("name");
await ctx.sync();
newSheetNames += "\"" + newSheet.name + "\", ";
//ctx.workbook.worksheets.getItem(newWorkSheets.value[i]).tabColor = getRandomColor();
}
console.log("Inserted sheets number: " + newWorkSheets.value.length + ".\nInserted sheets name: " + newSheetNames);
await ctx.sync();
});
}
function getRandomColor() {
return "#000000".replace(/0/g, function() {
return (~~(Math.random() * 16)).toString(16);
});
}
/** 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: "<section class=\"ms-font-m\">\n\t<p class=\"ms-font-m\">This sample demonstrates Excel WorkSheetCollection.AddFromBase64() API call.</p>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<p class=\"ms-font-m\">Please select a file to insert:</p>\n\t<input id=\"myFile\" name=\"myFile\" type=\"file\">\n\t<p class=\"ms-font-m\"></p>\n\t<button id=\"run\" class=\"ms-Button\">\n\t\t<span class=\"ms-Button-label\">Add Sheets From Base64</span>\n </button>\n</section>"
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://unpkg.com/@microsoft/office-js@1.1.39-custom.28/dist/office.debug.js
@types/office-js
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
jquery@3.1.1
@types/jquery@3.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment