-
-
Save rgallagherab/20440e358fb25034c5c77f94f350ff80 to your computer and use it in GitHub Desktop.
This file contains 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: Blank snippet (5) | |
description: Create a new snippet from a blank template. | |
host: EXCEL | |
api_set: {} | |
script: | |
content: > | |
OfficeExtension.config.extendedErrorLogging = true; | |
const sheetConfig = async (name = "Worksheet 1") => { | |
await Excel.run(async (context) => { | |
const worksheet = context.workbook.worksheets.getActiveWorksheet(); | |
worksheet.load("id"); | |
await context.sync(); | |
// If this line is removed the other function runs fine. Something about setting the name puts it in a state where rows can not be frozen. | |
worksheet.name = name; | |
await context.sync(); | |
}); | |
}; | |
const headerConfig = async (value = "Header Text", name = "named.range", | |
address = "A1", shouldFreeze = true) => { | |
return Excel.run(async (context) => { | |
const worksheet = context.workbook.worksheets.getActiveWorksheet(); | |
const names = worksheet.names.load("items"); | |
await context.sync(); | |
const range = worksheet.getRange(address); | |
range.values = [[value]]; | |
worksheet.names.add(name, range); | |
await context.sync(); | |
}); | |
}; | |
const freezeHeader = async () => { | |
await Excel.run(async (context) => { | |
const worksheet = context.workbook.worksheets.getActiveWorksheet(); | |
worksheet.load("names"); | |
await context.sync(); | |
worksheet.freezePanes.freezeRows(1); | |
await context.sync(); | |
}); | |
}; | |
const logRangeInfo = async () => { | |
await Excel.run(async (context) => { | |
const worksheet = context.workbook.worksheets.getActiveWorksheet(); | |
worksheet.load("names"); | |
const range = worksheet.getUsedRange(); | |
await context.sync(); | |
console.log(`worksheet.names:\n${JSON.stringify(worksheet.names, null, 2)}`); | |
}); | |
}; | |
const run = async (callback) => { | |
try { | |
await callback(); | |
} catch (e) { | |
console.log(e); | |
console.log( | |
'Items after worksheet.name is set have bad range reference\ntype: "Error", value: "#REF!" vs type: "Range", value: "Valid worksheet address here"' | |
); | |
} | |
}; | |
const configureSheet = () => { | |
run(async () => { | |
await headerConfig(); | |
await sheetConfig("Worksheet 1"); | |
await headerConfig("Header Two Text", "named.range2", "B1", false); | |
await headerConfig("Header Three Text", "named.range3", "C1", false); | |
await freezeHeader(); | |
}); | |
}; | |
const displayDetails = async () => { | |
await logRangeInfo(); | |
}; | |
$("#configure").click(configureSheet); | |
$("#display").click(displayDetails); | |
language: typescript | |
template: | |
content: |- | |
<button id="configure" class="ms-Button"> | |
<span class="ms-Button-label">Configure Sheet</span> | |
</button> | |
<button id="display" class="ms-Button"> | |
<span class="ms-Button-label">Display Worksheet Names</span> | |
</button> | |
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/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