Hi everyone,
I'm trying to automate the creation of a specific type of chart in Excel using Office Scripts. I have a table of data and I want to generate a clustered column chart based on a selection of columns.
I've been looking at the Excel Script API, but I'm struggling with how to correctly define the range for the chart series and axes. My data looks something like this:
// Example Data Structure (Conceptual)
// Sheet1!A1:C5
// Month | Sales | Profit
// Jan | 100 | 20
// Feb | 120 | 25
// Mar | 110 | 22
// Apr | 130 | 28
I want the 'Month' column (A2:A5) to be my category (X) axis and 'Sales' (B2:B5) and 'Profit' (C2:C5) to be the value (Y) series.
Here's what I have so far, but it's not quite working:
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
let chart = selectedSheet.addChart(ExcelScript.ChartType.columnClustered);
// How do I correctly specify these ranges?
chart.setSeries([
{
categories: selectedSheet.getRange("A2:A5"), // This might be wrong
values: selectedSheet.getRange("B2:B5") // This might be wrong
},
{
categories: selectedSheet.getRange("A2:A5"), // This might be wrong
values: selectedSheet.getRange("C2:C5") // This might be wrong
}
]);
// Other chart configurations like title, etc. will follow.
}
Any help on how to correctly define the chart series and categories using ranges in Office Scripts would be greatly appreciated!
Thanks!