Created
April 29, 2021 10:36
-
-
Save MiaofeiWang/53e713ee0fbb0cae33639953b2f53379 to your computer and use it in GitHub Desktop.
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: Repro Chart data label formula bug | |
description: '' | |
host: EXCEL | |
api_set: {} | |
script: | |
content: > | |
$("#run").click(() => tryCatch(run)); | |
async function run() { | |
await Excel.run(async (context) => { | |
var sheetName = "Charts"; | |
var useExistingChart = false; | |
//await removeAllChartsAsync(sheetName); | |
await Excel.run(async (ctx) => { | |
var ctx = new Excel.RequestContext(); | |
var worksheet = ctx.workbook.worksheets.getItem(sheetName); | |
var sourceData = worksheet.getRange(sheetName + "!" + "E1:H5"); | |
var chart; | |
if (useExistingChart) { | |
chart = worksheet.charts.getItemAt(0); | |
} else { | |
chart = worksheet.charts.add("Pie", sourceData, "Auto"); | |
} | |
var pointsCollection = chart.series.getItemAt(0).points; | |
var point = pointsCollection.getItemAt(0); | |
point.load("dataLabel"); | |
await ctx.sync(); | |
var dataLabel = point.dataLabel; | |
if (!useExistingChart) { | |
dataLabel.load("formula"); | |
await ctx.sync(); | |
assertCompareValues(null, dataLabel.formula, "Chart Point Data Label Formula"); | |
dataLabel.formula = "=" + sheetName + "!E3"; | |
} | |
dataLabel.load("formula"); | |
await ctx.sync(); | |
if (isWAC() || isiOS()) { | |
assertCompareValues(sheetName + "!E3", dataLabel.formula, "Chart Data Label Formula"); | |
} else { | |
assertCompareValues(sheetName + "!$E$3", dataLabel.formula, "Chart Data Label Formula"); | |
} | |
}); | |
}); | |
} | |
function isWAC() { | |
return (<any>window).Office.context.platform === (<any>window).Office.PlatformType.OfficeOnline; | |
} | |
function isiOS() { | |
return (<any>window).Office.context.platform === (<any>window).Office.PlatformType.iOS; | |
} | |
async function removeAllChartsAsync(sheetName: string) { | |
console.log('Cleaning all charts from the sheet "' + sheetName + '"'); | |
var ctx = new Excel.RequestContext(); | |
var charts = ctx.workbook.worksheets.getItem(sheetName).charts; | |
ctx.load(charts, "id"); | |
await ctx.sync(); | |
console.log(charts.count + " charts were found"); | |
for (var i = 0; i < charts.count; i++) { | |
charts.getItemAt(0).delete(); | |
} | |
await ctx.sync(); | |
} | |
function assert(statementOrLambda: boolean | (() => boolean), explanation?: | |
string) { | |
if (typeof statementOrLambda === "boolean") { | |
if (!statementOrLambda) { | |
throw new Error("Assert failed" + (explanation ? " because " + explanation : "")); | |
} else { | |
console.log("Assert passed" + (explanation ? ": " + explanation : "")); | |
} | |
} else { | |
assert((<() => boolean>statementOrLambda)(), statementOrLambda.toString()); | |
} | |
} | |
function assertCompareValues(expectedValue, actualValue, additionalComment?: | |
string) { | |
var prefix = additionalComment ? additionalComment + " - " : ""; | |
assert(actualValue == expectedValue, prefix + "Expected: '" + expectedValue + "' Actual: '" + actualValue + "'"); | |
} | |
/** 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 class=\"ms-font-m\">This sample demonstrates basic Excel API calls.</p>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\n\t<button id=\"run\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Run</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/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