Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save geoffappleford/c36b21cdefaf696a624c2c279e44f531 to your computer and use it in GitHub Desktop.
Save geoffappleford/c36b21cdefaf696a624c2c279e44f531 to your computer and use it in GitHub Desktop.
Inserts worksheets from another workbook into the current workbook.
name: Insert external worksheets (1)
description: Inserts worksheets from another workbook into the current workbook.
host: EXCEL
api_set: {}
script:
content: |
$("#file").change(() => tryCatch(insertSheets));
$("#go").click(() => $("#file").click());
async function insertSheets() {
const button = <HTMLButtonElement>document.getElementById("go");
const myFile = <HTMLInputElement>document.getElementById("file");
const reader = new FileReader();
reader.onload = (event) => {
Excel.run((context) => {
// strip off the metadata before the base64-encoded string
const startIndex = reader.result.toString().indexOf("base64,");
const workbookContents = reader.result.toString().substr(startIndex + 7);
const sheets = context.workbook.worksheets;
sheets.addFromBase64(
workbookContents,
null, // get all the worksheets
Excel.WorksheetPositionType.end // insert them after the current workbook's worksheets
);
return context.sync();
});
};
// read in the file as a data URL so we can parse the base64-encoded string
reader.readAsDataURL(myFile.files[0]);
}
/** 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\t\t<button id=go>Go</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://appsforoffice.microsoft.com/lib/beta/hosted/office.js
@types/office-js-preview
[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