0

I used the "Record Actions" in the Automate tab to record myself creating a chart. The Chart looks exactly how I want it to look after recording.

enter image description here

function main(workbook: ExcelScript.Workbook) {
  let selectedSheet = workbook.getActiveWorksheet();
  // Insert chart on sheet selectedSheet
  let chart_24 = selectedSheet.addChart(ExcelScript.ChartType.pie, selectedSheet.getRange("A6:B7"));
}

Correct Chart

I changed up the code a little bit so it could work when I call this Script from Power Automate.

function main(workbook: ExcelScript.Workbook) {
  let sheet = workbook.getWorksheet("Dashboard");
  let rng = sheet.getRange("A6:B7");
  let chart_10 = sheet.addChart(ExcelScript.ChartType.pie, rng);
}

However, when I run either code above without having the data selected on the sheet, it is now giving me a bad chart like below.

Bad Chart

This just started happening to me. I had the code working fine for me for about two months but now it is giving me the bad graph.

After testing the code, I noticed it only works when you have the data selected and the sheet open. When you run it in Power Automate ( where you can't use select() ) it will always give me the bad graph now. Ever since March 25, 2021, I have been getting the bad graph. Prior to this date, my code ran perfectly fine. How can I use a range without having to use select() so I can run it in Power Automate?

Works when data is selected

Doesn't work when data isn't selected

Ethan
  • 808
  • 3
  • 21
  • 1
    This could be some API related issue that only affects in the Power Automate mode. Will alert the team and revert back. – Sudhi Ramamurthy Apr 05 '21 at 16:55
  • Hi @SudhiRamamurthy thanks for the response! When I run it just in Office Scripts within Excel, I seem to be getting the same issue. – Ethan Apr 05 '21 at 17:24

1 Answers1

1

This is because the row and column used by pie chart are wrongly switched. You can try to click the "Switch Row/Column" button under "Chart" tab when selecting the chart to see the result.

As a workaround for Power Automate, you can use

function main(workbook: ExcelScript.Workbook) {
  let sheet = workbook.getWorksheet("Sheet2");
  let rng = sheet.getRange("A1:B2");
// 3rd parameter will help avoid this bug. 
  let chart_10 = sheet.addChart(ExcelScript.ChartType.pie, rng, ExcelScript.ChartSeriesBy.columns);
}
Sudhi Ramamurthy
  • 2,358
  • 1
  • 10
  • 14
  • 1
    Please note that this is a harmless workaround - and eventually, we'll make the 3rd parameter be truly optional (it should default to series by columns). – Sudhi Ramamurthy Apr 06 '21 at 16:27