Created
July 10, 2019 07:36
-
-
Save lumine2008/1a20545d0c8b96b656688a9444d7f8ce to your computer and use it in GitHub Desktop.
Inserts, deletes, and clears a range.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: 'Insert, delete, and clear' | |
| description: 'Inserts, deletes, and clears a range.' | |
| host: EXCEL | |
| api_set: {} | |
| script: | |
| content: | | |
| $("#setup").click(() => tryCatch(setup)); | |
| $("#insert-range").click(() => tryCatch(insertRange)); | |
| $("#delete-range").click(() => tryCatch(deleteRange)); | |
| $("#clear-range").click(() => tryCatch(clearRange)); | |
| async function insertRange() { | |
| await Excel.run(async (context) => { | |
| const sheet = context.workbook.worksheets.getItem("Sample"); | |
| var i = 0; | |
| for (;i<10000;i++) | |
| { | |
| const range = sheet.getRange("B4:E4"); | |
| range.insert(Excel.InsertShiftDirection.down); | |
| range.values = [[1,1,1,1]]; | |
| } | |
| await context.sync(); | |
| }); | |
| } | |
| async function deleteRange() { | |
| await Excel.run(async (context) => { | |
| const sheet = context.workbook.worksheets.getItem("Sample"); | |
| const range = sheet.getRange("B4:E4"); | |
| range.delete(Excel.DeleteShiftDirection.up); | |
| await context.sync(); | |
| }); | |
| } | |
| async function clearRange() { | |
| await Excel.run(async (context) => { | |
| const sheet = context.workbook.worksheets.getItem("Sample"); | |
| let range = sheet.getRange("E2:E5"); | |
| range.clear(); | |
| await context.sync(); | |
| }); | |
| } | |
| async function setup() { | |
| await Excel.run(async (context) => { | |
| context.workbook.worksheets.getItemOrNullObject("Sample").delete(); | |
| const sheet = context.workbook.worksheets.add("Sample"); | |
| const data = [ | |
| ["Product", "Qty", "Unit Price", "Total Price"], | |
| ["Almonds", 2, 7.5, "=C3 * D3"], | |
| ["Coffee", 1, 34.5, "=C4 * D4"], | |
| ["Chocolate", 5, 9.56, "=C5 * D5"] | |
| ]; | |
| const range = sheet.getRange("B2:E5"); | |
| range.values = data; | |
| range.format.autofitColumns(); | |
| const header = range.getRow(0); | |
| header.format.fill.color = "#4472C4"; | |
| header.format.font.color = "white"; | |
| sheet.activate(); | |
| await 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"> | |
| <p>This sample shows how to insert, delete and clear the contents of a range.</p> | |
| </section> | |
| <section class="setup ms-font-m"> | |
| <h3>Set up</h3> | |
| <button id="setup" class="ms-Button"> | |
| <span class="ms-Button-label">Add sample data</span> | |
| </button> | |
| </section> | |
| <section class="samples ms-font-m"> | |
| <h3>Try it out</h3> | |
| <button id="insert-range" class="ms-Button"> | |
| <span class="ms-Button-label">Insert range</span> | |
| </button> | |
| <button id="delete-range" class="ms-Button"> | |
| <span class="ms-Button-label">Delete range</span> | |
| </button> | |
| <button id="clear-range" class="ms-Button"> | |
| <span class="ms-Button-label">Clear range</span> | |
| </button> | |
| </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/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