Skip to content

Instantly share code, notes, and snippets.

@ci010
Last active July 1, 2021 08:57
Show Gist options
  • Save ci010/2bed6b284002eacbb9a4ac83ff9470d8 to your computer and use it in GitHub Desktop.
Save ci010/2bed6b284002eacbb9a4ac83ff9470d8 to your computer and use it in GitHub Desktop.
name: TableRowCollection.DeleteRows
description: ''
host: EXCEL
api_set: {}
script:
content: |
$("#DeleteRowsAt").click(() => tryCatch(DeleteRowsAt));
$("#DeleteRows").click(() => tryCatch(DeleteRows));
async function DeleteRowsAt() {
await Excel.run(async (context) => {
const table = context.workbook.tables.getItem($("#table").val() as string);
const index = Number($("#index").val());
const countStr = $("#count").val() as string;
const count = countStr.length === 0 ? undefined : Number(countStr);
const countParam = Number.isNaN(count) ? undefined : count;
console.log(`DeleteRowsAt(${index}, ${countParam})`);
table.rows.deleteRowsAt(index, countParam);
await context.sync();
});
}
async function DeleteRows() {
await Excel.run(async (context) => {
const table = context.workbook.tables.getItem($("#table").val() as string);
const rows = ($("#rows").val() as string)
.split(",")
.map((s) => s.trim())
.filter((s) => s.length !== 0)
.map((s) => Number(s.trim()));
console.log("DeleteRows");
console.log(rows);
table.rows.deleteRows(rows);
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: "<div>\n\tOn Table <input id=\"table\">\n\t<div>\n\t\tDeleteRowsAt(<input id=\"index\" type=\"number\">, <input id=\"count\" type=\"number\">)\n\t\t<button id=\"DeleteRowsAt\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Run</span>\n</button>\n\t</div>\n\n\t<div>\n\t\tDeleteRows(<input id=\"rows\">)\n\t\t<button id=\"DeleteRows\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Run</span>\n</button>\n\t</div>\n\n</div>"
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
https://unpkg.com/@microsoft/office-js@1.1.71-custom.25/dist/office.js
https://unpkg.com/@microsoft/office-js@1.1.71-custom.25/dist/office.d.ts
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