Skip to content

Instantly share code, notes, and snippets.

@skysan87
Last active January 2, 2023 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skysan87/1189328005c184734baa9ccca686bc21 to your computer and use it in GitHub Desktop.
Save skysan87/1189328005c184734baa9ccca686bc21 to your computer and use it in GitHub Desktop.
[Script Lab][Excel JavaScript API]シート一覧を表示するスニペット。インポート方法: import > Snippet URL or YAMLにyamlの内容をコピペする。
name: Excel Sheet List
description: Listup Excel Worksheets
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(refresh));
// ワークシート一覧を作成
async function refresh() {
await Excel.run(async (context) => {
let sheets = context.workbook.worksheets;
let activeSheet = context.workbook.worksheets.getActiveWorksheet();
sheets.load("items/name");
activeSheet.load("name");
await context.sync();
const $sheetList = $("#sheetList");
$sheetList.empty();
sheets.items.forEach(function(sheet) {
$sheetList.append(`<li data-name="${sheet.name}">${sheet.name}</li>`);
});
$(`#sheetList li[data-name="${activeSheet.name}"]`).addClass("activate");
});
}
$("#sheetList").on("click", "li", (e) => {
const name = e.target.dataset.name;
tryCatch(() => activeSheet(name));
});
// 指定したシートをアクティブにする
async function activeSheet(sheetName) {
await Excel.run(async (context) => {
$("#sheetList li.activate").removeClass("activate");
let sheet = context.workbook.worksheets.getItem(sheetName);
sheet.activate();
await context.sync();
$(`#sheetList li[data-name="${sheetName}"]`).addClass("activate");
});
}
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
console.error(error);
}
}
language: typescript
template:
content: "<button id=\"run\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">refresh</span>\n</button>\n\n<div>\n\t<ul id=\"sheetList\">\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;
}
#sheetList li {
cursor: pointer;
}
#sheetList li:hover {
background-color: gainsboro;
}
.activate {
color: red;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
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