Skip to content

Instantly share code, notes, and snippets.

@gooooloo
Created June 21, 2021 09:01
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/8983f6984276d8293778b09d70ef1900 to your computer and use it in GitHub Desktop.
Save gooooloo/8983f6984276d8293778b09d70ef1900 to your computer and use it in GitHub Desktop.
Clicking the button, then ungroup the PT, check the content
name: groupThenUnGroup
description: 'Clicking the button, then ungroup the PT, check the content'
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", 2000], ["8/18/2020", 3000], ["2/5/2018", 4000], ["1/1/2018", 2000], ["3/23/2019", 3000], ["3/24/2019", 4000]];
const range = dataSheet.getRange("A1:B7");
range.values = data;
range.format.autofitColumns();
pivotSheet.activate();
await context.sync();
const rangeToAnalyze = context.workbook.worksheets.getItem("Data").getRange("A1:B7");
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 hierarchy3 = pivotTable.addDateGroup(field, "ByMonths");
pivotTable.rowHierarchies.add(hierarchy3);
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