Skip to content

Instantly share code, notes, and snippets.

@gooooloo
Created June 24, 2021 01:51
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 gooooloo/039881db7d19d9c991179af936ea51fd to your computer and use it in GitHub Desktop.
Save gooooloo/039881db7d19d9c991179af936ea51fd to your computer and use it in GitHub Desktop.
Clicking the button, then ungroup the PT,
name: groupThenUnGroupSimple
description: 'Clicking the button, then ungroup the PT,'
host: EXCEL
api_set: {}
script:
content: |
$("#setup").click(() => tryCatch(setup));
async function setup() {
await Excel.run(async (context) => {
context.workbook.worksheets.getItemOrNullObject("Data").delete();
const dataSheet = context.workbook.worksheets.add("Data");
context.workbook.worksheets.getItemOrNullObject("Pivot").delete();
const pivotSheet = context.workbook.worksheets.add("Pivot");
const data = [
["Date", "Sales"],
["8/19/2020", 18],
["8/18/2020", 18],
["2/5/2018", 187],
["1/1/2018", 142],
["1/2/2018", 150],
["1/3/2018", 180],
["1/4/2018", 110],
["1/5/2018", 121],
["1/6/2018", 134],
["1/7/2018", 155],
["1/8/2018", 155],
["1/9/2018", 155],
["1/10/2018", 155],
["1/11/2018", 177],
["1/12/2018", 238],
["1/13/2018", 238],
["1/14/2018", 238],
["3/20/2019", 128],
["3/21/2019", 177],
["3/22/2019", 180],
["3/23/2019", 180],
["3/24/2019", 220]
];
const range = dataSheet.getRange("A1:B23");
range.values = data;
range.format.autofitColumns();
pivotSheet.activate();
await context.sync();
const rangeToAnalyze = context.workbook.worksheets.getItem("Data").getRange("A1:B23");
const rangeToPlacePivot = context.workbook.worksheets.getItem("Pivot").getRange("A2");
context.workbook.worksheets.getItem("Pivot").pivotTables.add("Farm Sales", rangeToAnalyze, rangeToPlacePivot);
await context.sync();
const pivotTable = context.workbook.worksheets.getActiveWorksheet().pivotTables.getItem("Farm Sales");
const hierarchy = pivotTable.hierarchies.getItem("Date");
const field = hierarchy.fields.getItem("Date");
const hierarchy2 = pivotTable.addDateGroup(field, "ByYears");
pivotTable.rowHierarchies.add(hierarchy2);
const hierarchy4 = pivotTable.hierarchies.getItem("Sales");
pivotTable.dataHierarchies.add(hierarchy4);
await context.sync();
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
language: typescript
template:
content: "<section class=\"ms-font-m\">\n\t<p>This sample shows how to create PivotTables and add hierarchies to form rows, columns, and data sets.</p>\n</section>\n\n<section class=\"setup ms-font-m\">\n\t<h3>Set up</h3>\n\t<button id=\"setup\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Setup sample</span>\n </button>\n</section>"
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/beta/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