I have created an office script that does some operations on excel data and creates a table. The data of this table is the input for a chart. The chart is already existing in the Excel file.
Now I need to update the data range of the chart manually whenever new items are added to the table, but I want to do this automated ofcourse.
I was not able to find any documentation on this. In VBA I can update data ranges of a chart and I tried to port that to the office script, but with no luck.
in VBA it is like this:
ActiveChart.FullSeriesCollection(1).XValues = "='summary and graph'!$A$2:$A$42"
ActiveChart.FullSeriesCollection(1).Values = "='summary and graph'!$B$2:$B$42"
ActiveChart.FullSeriesCollection(2).Values = "='summary and graph'!$C$2:$C$42"
What would be a way to do this in excel office script? What would the syntax be?
Thanks, Joris
Here is the code you get when you do the recording to create a chart for the table
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
let table1 = workbook.getTable("Table1");
// Insert chart on sheet selectedSheet with source data from table1
let chart_5 = selectedSheet.addChart(ExcelScript.ChartType.columnClustered, table1.getRange());
}
You can change the chart source data using the setData method.
You can also get the chart series collection using the getSeries method, and set Values for each series using the setValues method. Example code could be -
let seriesCollection = chart_5.getSeries();
let range = workbook.getWorksheet("summary and graph").getRange("B2:B42");
seriesCollection[0].setValues(range);
range = workbook.getWorksheet("summary and graph").getRange("C2:C42");
seriesCollection[1].setValues(range);
Related
I am new to using excel scripts with power automate. Trying to convert some data to a table. The thing is the number of rows will differ each time.
I've done this in excel desktop, but not sure how to do it using an excel script.
The problem is finding the used range in excel(Office Scripts). Once you have it, i assume you know how to convert it to table.
function main(workbook: ExcelScript.Workbook)
{
// Get the current, active worksheet.
let currentWorksheet = workbook.getActiveWorksheet();
// Get the range containing all the cells with data or formatting.
let usedRange = currentWorksheet.getUsedRange();
// Log the range's address to the console.
console.log(usedRange.getAddress());
}
Link for your reference
From:
https://learn.microsoft.com/en-us/office/dev/scripts/resources/samples/excel-samples
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// Create a table with the used cells.
let usedRange = selectedSheet.getUsedRange();
let newTable = selectedSheet.addTable(usedRange, true);
// Sort the table using the first column.
newTable.getSort().apply([{ key: 0, ascending: true }]);
}
I Try to create a office script, that do the same as this excel makro:
Sub clear()
Rows("2:" & Rows.Count).ClearContents
End Sub
my office Script looks like this:
function main(workbook: ExcelScript.Workbook)
{
let table = workbook.getTable("Table1");
let rowCount = workbook.getTables()[0].getRowCount();
table.deleteRowsAt(2, rowCount);
}
i get following error:
Line 9: Cannot read property 'deleteRowsAt' of undefined
i dont get why the property "deleteRowsAt" is undefined.
it needs to be a office script, because i want to automate a process with power automate.
Not sure why you didn't use table in your rowCount code. You don't know if you are even getting that table for sure.
Assuming your table starts in Row 2 and the headers are in Row 1, to delete the table, see the following code below.
function main(workbook: ExcelScript.Workbook) {
let table = workbook.getTable("Table1");
let rowCount = table.getRowCount();
table.deleteRowsAt(0, rowCount);
}
the office Script works, u need to check if the Table name is really your table name
I'm trying to write a script that finds data in a sheet dynamically (the dimensions of the table need to be flexible in both axis) and then updates the source data range for an existing chart on another sheet (so that my users don't need to set up the styling themselves).
Below is my script so far. Everything works apart from the final line where Excel Online gives me the error:
"Line 10: Chart setData: You cannot perform the requested operation"
function main(workbook: ExcelScript.Workbook)
{
let selectedSheet = workbook.getWorksheet("Enter data in this sheet");
// Add a new table at used range on selectedSheet
let range = selectedSheet.getUsedRange();
if(selectedSheet.getTables().length == 0)
{
let newTable = workbook.addTable(range, true);
}
workbook.getWorksheet("The Chart").getChart("Chart 1").setData(range);
}
The worksheet names are correct and so is the chart name as far as I can see:
screenshot of excel online showing chart and worksheet names
Answer found; the chart was on a protected sheet which did not allow editing of objects. Updating the protection to allow all users to edit objects has resolved the issue.
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.
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"));
}
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.
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?
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);
}
i am testing a script where I want to create Mutliple charts with the same pivot data,
I am able to get the first chart using the below snippets
$pivotTableParams = #{
PivotTableName = "PWAuditData1"
Address = $excel.Sheet1.cells["F1"]
SourceWorkSheet = $excel.Sheet1
PivotRows = echo o_username
PivotData = #{'Total' = 'sum'}
PivotTableStyle = 'Light21'
PivotChartDefinition = #{
Title="Total Downloads by Users";
ChartType="ColumnClustered";
Column=5; Row=15; Width=600; Height=500;
YMajorUnit=500; YMinorUnit=100;
LegendPosition="Bottom"
}
}
and then call the above definition using
$pt = Add-PivotTable #pivotTableParams -PassThru
I want to use the same data from above Pivot Table and insert for example a doughnut chart for which i use the Add-ExcelChart
$pivotSheet=$excel.Sheet1
$pt1 = Add-ExcelChart -Worksheet $excel.Sheet1 -PivotTable $pivotSheet.pivotTables[0] -ChartType Doughnut -Row 100 -Column 20 -PassThru
when the code runs, I am able to get the first chart, but for inserting the second chart it gives error as below,
Parameter set cannot be resolved using the specified named parameters
image
Question:
What is the correct way to reference the existing Pivot object which is already? placed on the sheet ?
Thanks for looking to this,
RJ