0

I'm trying to color a specific column on a column chart in Excel for web using Office Scripts. I'm able to set the color of all columns using this code:

function main(workbook: ExcelScript.Workbook) {
  const sheet = workbook.getWorksheet("mySheet")
  const chart = sheet.getChart("myChart")
  const series = chart.getSeries()[0]

  series.getFormat().getFill().setSolidColor("red")
}

But when I try to specify a certain point with this:

function main(workbook: ExcelScript.Workbook) {
  const sheet = workbook.getWorksheet("mySheet")
  const chart = sheet.getChart("myChart")
  const series = chart.getSeries()[0]

  const i = 2
  const point = series.getPoints()[i]
  point.getFormat().getFill().setSolidColor("red")
}

Then on the line const point = series.getPoints()[i] I get the error
ChartSeries getPoints: There was an internal error while processing the request.

To simplify as much as possible, I've make a new workbook with only this new chart in it, using the data to its side, which still gives me the same error. Here is a screenshot of the chart.

On further testing, this does work correctly when running the script on the desktop version. This error occurs only in the browser version, which unfortunately I must use

3
  • I've tested your script on M365 without issues. Please edit your post to share a chart snapshot. Commented Dec 18, 2024 at 22:01
  • @taller Thank you, I've added a screenshot link to the bottom of my question Commented Dec 19, 2024 at 2:43
  • I’ve recreated the exact chart as yours, and the script works perfectly. I recommend creating the chart in a new workbook and testing it with a fresh OfficeScript. Commented Dec 19, 2024 at 5:19

1 Answer 1

0

I used the recorder to get this code, and it seems to work?

Ah, you are right this doesn't work on the web!

function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();
    
    // Set range A1:B3 on selectedSheet
    selectedSheet.getRange("A1:B3").setValues([["A",1],["B",2],["C",3]]);

    // Insert chart on sheet selectedSheet
    let chart_1 = selectedSheet.addChart(ExcelScript.ChartType.columnClustered, selectedSheet.getRange("A1:B3"));

    // Change fill color for point on series on chart chart_1
    chart_1.getSeries()[0].getPoints()[0].getFormat().getFill().setSolidColor("ffc000");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.