Skip to content

Instantly share code, notes, and snippets.

@ci010
Created May 20, 2020 12:27
Show Gist options
  • Save ci010/61fc8fa2736592cc3c64630b6a795ea4 to your computer and use it in GitHub Desktop.
Save ci010/61fc8fa2736592cc3c64630b6a795ea4 to your computer and use it in GitHub Desktop.
Create a new snippet from a blank template.
name: Blank snippet
description: Create a new snippet from a blank template.
host: EXCEL
api_set: {}
script:
content: |
function grab() {
Excel.run(async (context) => {
let r = context.workbook.worksheets.getActiveWorksheet().getRange("A1:D8");
r.conditionalFormats.load('items')
await context.sync();
console.log(`Range A1:D8 has ${r.conditionalFormats.items.length} conditional formatting`);
for (let i = 0; i < r.conditionalFormats.items.length; ++i) {
let cond = r.conditionalFormats.items[i];
let scale = cond.colorScale;
cond.load("type");
scale.load("criteria");
await context.sync();
let cri = scale.criteria;
console.log(`[${i}]: ${cond.type}`);
console.log(
JSON.stringify(cri, (k, v) => {
if (k === "@odata.type") return undefined;
return v;
})
);
}
}).catch((e) => {
console.error(e);
});
}
let obj = {
maximum: {
color: "#F8696B",
formula: null,
type: Excel.ConditionalFormatColorCriterionType.highestValue
},
midpoint: {
color: "#FFEB84",
formula: "50",
type: Excel.ConditionalFormatColorCriterionType.percentile
},
minimum: {
color: "#63BE7B",
formula: null,
type: Excel.ConditionalFormatColorCriterionType.lowestValue
}
};
let values = [
[16101574, 742416, 4489, 1341552],
["", "", "", ""],
["", "", "", ""],
["", "", "", ""],
["", "", "", ""],
["", "", "", 257],
["", "", "", 1729],
["", "", "", ""]
];
async function setFormat() {
Excel.run(async (context) => {
let r = context.workbook.worksheets.getActiveWorksheet().getRange("A1:D8");
r.values = values;
r.conditionalFormats.load('items');
await context.sync();
let cond = r.conditionalFormats.items[0];
if (r.conditionalFormats.items.length === 0) {
cond = r.conditionalFormats.add(Excel.ConditionalFormatType.colorScale);
}
let scale = cond.colorScale;
scale.load("criteria");
await context.sync();
scale.criteria = obj;
}).catch((e) => {
console.error(e);
});
}
async function addFormat() {
Excel.run(async (context) => {
let r = context.workbook.worksheets.getActiveWorksheet().getRange("A1:D8");
r.values = values;
let cond = r.conditionalFormats.add(Excel.ConditionalFormatType.colorScale);
let scale = cond.colorScale;
scale.load("criteria");
await context.sync();
scale.criteria = obj;
}).catch((e) => {
console.error(e);
});
}
$("#run").click(() => {
grab();
});
$("#set").click(() => {
setFormat();
});
$("#add").click(() => {
addFormat();
});
language: typescript
template:
content: |-
<button id="run" class="ms-Button">
<span class="ms-Button-label">Grab Info</span>
</button>
<button id="set" class="ms-Button">
<span class="ms-Button-label"> Set Once</span>
</button>
<button id="add" class="ms-Button">
<span class="ms-Button-label"> Add Once</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