Skip to content

Instantly share code, notes, and snippets.

@XiangGaoMSFT
Last active August 15, 2018 14:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XiangGaoMSFT/3bfa781af9f9c1172bf3ab5cd1866f2d to your computer and use it in GitHub Desktop.
Save XiangGaoMSFT/3bfa781af9f9c1172bf3ab5cd1866f2d to your computer and use it in GitHub Desktop.
Sample script to list all names - Shared with Script Lab
name: View Names
description: Sample script to list all names
author: xianggaomsft
host: EXCEL
api_set: {}
script:
content: |
$("#refresh").click(() => tryCatch(refresh));
async function refresh() {
var tableDom = $("#namesTable");
tableDom.empty();
await Excel.run(async (context) => {
var names = context.workbook.names;
await appendNamedItem(context, tableDom, names, null);
var worksheets = context.workbook.worksheets;
var worksheetsCount = worksheets.getCount();
worksheets.load(["items", "name"]);
await context.sync();
for (var i = 0; i < worksheetsCount.value; i++) {
names = worksheets.items[i].names;
await appendNamedItem(context, tableDom, names, worksheets.items[i].name);
}
});
}
async function appendNamedItem(context, tableDom, names, sheetName) {
names.load(["name", "formula", "scope"]);
await context.sync();
for (var i = 0; i < names.items.length; i++) {
var tr = $("<tr>");
tr.append($("<td>").text(names.items[i].name));
tr.append($("<td>").text(names.items[i].formula));
tr.append($("<td>").text(sheetName ? sheetName : names.items[i].scope));
tableDom.append(tr);
}
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
}
catch (error) {
OfficeHelpers.UI.notify(error);
OfficeHelpers.Utilities.log(error);
}
}
language: typescript
template:
content: |
<h1>Names</h1>
<button id="refresh" class="ms-Button">
<span class="ms-Button-label">Refresh</span>
</button>
<table class="ms-Table">
<thead>
<tr>
<th>Name</th>
<th>Refers to</th>
<th>Scope</th>
</tr>
</thead>
<tbody id="namesTable">
</tbody>
</table>
language: html
style:
content: |-
h1 {
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
https://appsforoffice.microsoft.com/lib/1/hosted/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
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts
jquery@3.1.1
@types/jquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment