Last active
January 26, 2021 20:58
-
-
Save kbrandl/01858318da8cbdff606e8bba32145882 to your computer and use it in GitHub Desktop.
Shared with Script Lab
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: 49031720-how-to-get-all-formulas-of-a-sheet | |
description: '' | |
author: kbrandl | |
host: EXCEL | |
api_set: {} | |
script: | |
content: | | |
$("#setup").click(() => tryCatch(setup)); | |
$("#get-formulas").click(() => tryCatch(getFormulas)); | |
async function getFormulas() { | |
try { | |
await Excel.run(async (context) => { | |
const sheet = context.workbook.worksheets.getItem("Sample"); | |
// Get the used range in the sheet, | |
// then load all formulas in that sheet. | |
const range = sheet.getUsedRange(); | |
range.load("address, formulas"); | |
await context.sync(); | |
console.log(`The used range in the worksheet is: ${range.address}`); | |
console.log(`Contents of that range: ${JSON.stringify(range.formulas, null, 4)}`); | |
}); | |
} | |
catch (error) { | |
OfficeHelpers.UI.notify(error); | |
OfficeHelpers.Utilities.log(error); | |
} | |
} | |
async function setup() { | |
try { | |
await Excel.run(async (context) => { | |
const sheet = await OfficeHelpers.ExcelUtilities | |
.forceCreateSheet(context.workbook, "Sample"); | |
const data = [ | |
["Product", "Qty", "Unit Price", "Total Price"], | |
["Almonds", 2, 7.50, "=C3*D3"], | |
["Coffee", 1, 34.50, "=C4*D4"], | |
["Chocolate", 5, 9.56, "=C5*D5"], | |
["TOTAL", "=SUM(C3:C5)", "=SUM(D3:D5)", "=SUM(E3:E5)"] | |
]; | |
const range = sheet.getRange("B2:E6"); | |
range.values = data; | |
range.format.autofitColumns(); | |
const header = range.getRow(0); | |
header.format.fill.color = "#4472C4"; | |
header.format.font.color = "white"; | |
sheet.activate(); | |
await context.sync(); | |
}); | |
} | |
catch (error) { | |
OfficeHelpers.UI.notify(error); | |
OfficeHelpers.Utilities.log(error); | |
} | |
} | |
/** 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: | | |
<p class="ms-font-m">Add sample data to the sheet:</p> | |
<button id="setup" class="ms-Button"> | |
<span class="ms-Button-label">Setup</span> | |
</button> | |
<br/></br> | |
<p class="ms-font-m">Get all formulas in the sheet:</p> | |
<button id="get-formulas" class="ms-Button"> | |
<span class="ms-Button-label">Get Formulas</span> | |
</button> | |
language: html | |
style: | |
content: '' | |
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