Skip to content

Instantly share code, notes, and snippets.

@crazygao
Created April 17, 2019 07:34
Show Gist options
  • Save crazygao/0a3b985114ed7d4ffbb211514e4833f2 to your computer and use it in GitHub Desktop.
Save crazygao/0a3b985114ed7d4ffbb211514e4833f2 to your computer and use it in GitHub Desktop.
name: autofit bug sample (2)
description: ''
host: EXCEL
api_set: {}
script:
content: >
$("#run").click(() => tryCatch(run));
$("#checkRange").click(() => tryCatch(checkValues));
async function run() {
await setup();
}
async function setup() {
await Excel.run(async (context) => {
await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Sample");
const sheet = context.workbook.worksheets.getItem("Sample");
// Generate range data
const data = [
["Year", "Category", "Product", "Sales", "Rating"],
["2017", "Components", "Chains", 20000, 0.75],
["2015", "Clothing", "Socks", 3700, 0.22],
["2016", "Clothing", "Bib-Shorts", 4000, 0.22]
];
const range = sheet.getRange("A1:E4");
console.log("Original Width");
await checkValuesInternal(context);
range.values = data;
range.numberFormat = createFormatsForTable(sampleColumnFormats, 4, 5);
range.format.autofitColumns();
console.log("After Autofit");
await checkValuesInternal(context);
sheet.tables
.add(range, true)
.getRange()
.select();
//range.format.autofitColumns();
console.log("After CreateTable");
await checkValuesInternal(context);
sheet.activate();
});
}
const sampleColumnFormats: string[] = [
"General",
"General",
"General",
'_($* #,##0_);_($* (#,##0);_($* "-"??_);_(@_)',
"0%"
];
//const sampleColumnFormats: string[] = ["General", "General", "General",
"General", "General"];
function createFormatsForTable(columnFormats: string[], rows: number, cols:
number): string[][] {
const formats: string[][] = [];
// The first row has the headers and should not have any special formatting
const headerRow: string[] = [];
for (let i = 0; i < cols; i++) {
headerRow.push("General");
}
formats.push(headerRow);
// Each row should have the same formatting
for (let i = 1; i < rows; i++) {
formats.push([...columnFormats]);
}
return formats;
}
async function checkValuesInternal(context) {
const sheet = context.workbook.worksheets.getItem("Sample");
var formats = sheet.getRange("A1:E1").getColumnProperties({ format: { columnWidth: true } });
await context.sync();
formats.value.forEach(function (value, index, array) {
console.log("" + value.format.columnWidth);
});
}
async function checkValues() {
await Excel.run(checkValuesInternal);
}
/** 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: |-
<button id="run">run</button>
<button id="checkRange">checkRange</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/beta/hosted/office.js
https://appsforoffice.microsoft.com/lib/beta/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