Skip to content

Instantly share code, notes, and snippets.

@jipyua
Last active December 11, 2020 02:20
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/b268f182df17cd36944d2d4bce3bc752 to your computer and use it in GitHub Desktop.
Save jipyua/b268f182df17cd36944d2d4bce3bc752 to your computer and use it in GitHub Desktop.
Inserts worksheets from another workbook into the current workbook.
name: Insert external worksheets
description: Inserts worksheets from another workbook into the current workbook.
host: EXCEL
api_set: {}
script:
content: |
$("#file").change(getFileBase64);
$("#run").click(() => tryCatch(run));
const workbookContents;
async function run() {
await Excel.run(async (context) => {
//Excel.InsertWorksheetOptions
var options = { sheetNamesToInsert: [], positionType: Excel.WorksheetPositionType.before, relativeTo: "Sheet1" };
const sheets = context.workbook.insertWorksheetsFromBase64(workbookContents, options);
await context.sync();
});
}
async function getFileBase64() {
const myFile = <HTMLInputElement>document.getElementById("file");
const reader = new FileReader();
reader.onload = (event) => {
// strip off the metadata before the base64-encoded string
const startIndex = reader.result.toString().indexOf("base64,");
workbookContents = reader.result.toString().substr(startIndex + 7);
};
// read in the file as a data URL so we can parse the base64-encoded string
reader.readAsDataURL(myFile.files[0]);
}
async function insertWorksheetsFromBase64(workbookContents) {
Excel.run((context) => {
//Excel.InsertWorksheetOptions
var options = { sheetNamesToInsert: [], positionType: Excel.WorksheetPositionType.before, relativeTo: "Sheet2" };
const sheets = context.workbook.insertWorksheetsFromBase64(workbookContents);
return context.sync();
});
}
/** 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>This sample shows how to copy the worksheets from an existing workbook into the current workbook.</p>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<p>Select an Excel workbook to copy its worksheets into the current workbook.</p>\n\t<form>\n\t\t<input type=\"file\" id=\"file\" />\n </form>\n\n\t<button id=\"run\" class=\"ms-Button\">\n\t <span class=\"ms-Button-label\">Insert sheets</span>\n\t</button>\n\t<p>\n\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://appsforoffice.edog.officeapps.live.com/lib/beta/hosted/office.js
https://appsforoffice.edog.officeapps.live.com/lib/beta/hosted/office-with-first-party.d.ts
//https://appsforoffice.microsoft.com/lib/beta/hosted/office.js
//@types/office-js-preview
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