Last active
January 2, 2023 03:41
-
-
Save skysan87/1189328005c184734baa9ccca686bc21 to your computer and use it in GitHub Desktop.
[Script Lab][Excel JavaScript API]シート一覧を表示するスニペット。インポート方法: import > Snippet URL or YAMLにyamlの内容をコピペする。
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: 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 | |
[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