Last active
August 15, 2018 14:46
-
-
Save XiangGaoMSFT/3bfa781af9f9c1172bf3ab5cd1866f2d to your computer and use it in GitHub Desktop.
Sample script to list all names - Shared with Script Lab
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: 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 | |
[email protected]/dist/css/fabric.min.css | |
[email protected]/dist/css/fabric.components.min.css | |
[email protected]/client/core.min.js | |
@types/core-js | |
@microsoft/[email protected]/dist/office.helpers.min.js | |
@microsoft/[email protected]/dist/office.helpers.d.ts | |
[email protected] | |
@types/jquery |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment