question

VigneshBabuSundararajan-9452 avatar image
0 Votes"
VigneshBabuSundararajan-9452 asked YutaoHuang-MSFT edited

Office Script not working properly when file is not opened

I have a flow which triggers a excel office script .

A collection from power app initiates the flow .
Power app data : [{"Operations":"CNC_Programming","Score":"83%"},{"Operations":"CNC_Punching","Score":"89%"},{"Operations":"Bending","Score":"88%"},{"Operations":"Tapping_Operation","Score":"81%"},{"Operations":"Welding_Operation","Score":"79%"},{"Operations":"Grinding_Buffing_Operation","Score":"66%"},{"Operations":"Common_Points","Score":"81%"}]

I pass this data to excel , delete the existing item , paste it in that table .
Once script is triggered a chart is sent back to flow .

function main(workbook: ExcelScript.Workbook

) {

let selectedSheet = workbook.getWorksheet("Sheet1");

//get the current used range and lastRow

let myUsedRange = selectedSheet.getUsedRange();

let lastRow = myUsedRange.getAddress();

let radarchart = selectedSheet.addChart(ExcelScript.ChartType.radarFilled, selectedSheet.getRange(lastRow));

radarchart.getSeriesNameLevel[0]

let radarchartimage = radarchart.getImage();

radarchart.getTitle().setText('Score by Section');

radarchart.getAxes().getValueAxis().getFormat().getFont().setSize(1);

radarchart.getDataLabels().setShowValue(true);

radarchart.delete();

return [radarchartimage]

}

It works perfectly when excel file is kept opened but fails to paste the items on the table ( Only one row is getting pasted ) .

Kindly help in resolving this issue

office-excel-itprooffice-scripts-excel-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YutaoHuang-MSFT avatar image
0 Votes"
YutaoHuang-MSFT answered VigneshBabuSundararajan-9452 commented

Hello @VigneshBabuSundararajan-9452,

Just to make sure I understand your scenario correctly. In the script code you shared, I'm not seeing the part that does "pass this data to excel, delete the existing item, paste it in that table". I assume those operations are done directly in your Power Apps app, and then your app would invoke your Power Automate flow to run the Office Script to extract the chart image, right?

I'm guessing there might be a timing issue between the Excel Online operations in Power Apps (to prep the data in the table) and the operations in Power Automate (to extract the chart image). When the Run script action is trying to extract the chart image from the workbook, the changes made by the Excel Online the Power Apps haven't been fully propagated yet. These operations are from different "sessions" of Excel Online so there might be a synchronization among sessions in play here.

Do you think it's possible to move all the Excel related operations into the script? You can pass the original data all the way from Power Apps to the script and let the script to prep the table then extract the chart image. You will need to update your script code to accept parameter for the main function.

You can also try to add some delay (like 30 seconds) in Power Automate flow, before the Run script action, and see whether that would help.

Please let me know how it goes!

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Spot on !!! U understood the scenario
I have added a delay in the power automate and the issue got resolved.

Can you guide on how to send power automate collection (two columns) to Excel using script and create a chart?

Thank You

0 Votes 0 ·
VigneshBabuSundararajan-9452 avatar image
0 Votes"
VigneshBabuSundararajan-9452 answered VigneshBabuSundararajan-9452 commented

Spot on !!!
I have added delay in the power automate and the issue got resolved.

Can you guide on how to send power automate collection (two columns) to Excel using script and create a chart?

Thank You

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Glad to hear the delay workaround helped!

Regarding passing data to the scripts, can you share a bit more about the data? For example, where does the data come from? What is its format? Is it basically a string in JSON format like the one you shared in your original post?

[{"Operations":"CNC_Programming","Score":"83%"},{"Operations":"CNC_Punching","Score":"89%"},{"Operations":"Bending","Score":"88%"},{"Operations":"Tapping_Operation","Score":"81%"},{"Operations":"Welding_Operation","Score":"79%"},{"Operations":"Grinding_Buffing_Operation","Score":"66%"},{"Operations":"Common_Points","Score":"81%"}]

0 Votes 0 ·

Yes it is basically a string in json format

[{"Operations":"CNC_Programming","Score":"83%"},{"Operations":"CNC_Punching","Score":"89%"},{"Operations":"Bending","Score":"88%"},{"Operations":"Tapping_Operation","Score":"81%"},{"Operations":"Welding_Operation","Score":"79%"},{"Operations":"Grinding_Buffing_Operation","Score":"66%"},{"Operations":"Common_Points","Score":"81%"}]

I pass this from power app to power automate .

Then i delete the existing content in excel online using power automate .
I upload the Json into excel

Then my script is triggered ( The Trailing contains the script)

function main(workbook: ExcelScript.Workbook

) {

let selectedSheet = workbook.getWorksheet("Sheet1");

//get the current used range and lastRow

let myUsedRange = selectedSheet.getUsedRange();

let lastRow = myUsedRange.getAddress();

let radarchart = selectedSheet.addChart(ExcelScript.ChartType.radarFilled, selectedSheet.getRange(lastRow));

radarchart.getSeriesNameLevel[0]

let radarchartimage = radarchart.getImage();

radarchart.getTitle().setText('Score by Section');

radarchart.getAxes().getValueAxis().getFormat().getFont().setSize(1);

radarchart.getDataLabels().setShowValue(true);

radarchart.delete();

return [radarchartimage]

}


0 Votes 0 ·
YutaoHuang-MSFT avatar image
0 Votes"
YutaoHuang-MSFT answered

Firstly, you will need to add a parameter to the main function to accept the incoming data in JSON format:

function main(workbook: ExcelScript.Workbook, data: string): string {
  let selectedSheet = workbook.getWorksheet("Sheet1");
  let items: Item[] = JSON.parse(data);
  let values = items.map(item => [item.Operations, item.Score]);
  values.unshift(["Operations", "Score"]);
  let newRange = selectedSheet.getRange("A1").getResizedRange(values.length - 1, 1);
  newRange.setValues(values);
  //get the current used range and lastRow
  let myUsedRange = selectedSheet.getUsedRange();
  let lastRow = myUsedRange.getAddress();
  let radarchart = selectedSheet.addChart(ExcelScript.ChartType.radarFilled, selectedSheet.getRange(lastRow));
  radarchart.getSeriesNameLevel[0]
  radarchart.getTitle().setText('Score by Section');
  radarchart.getAxes().getValueAxis().getFormat().getFont().setSize(1);
  radarchart.getDataLabels().setShowValue(true);
  let radarchartimage = radarchart.getImage();
  radarchart.delete();
  return radarchartimage;
}

interface Item {
  Operations: string;
  Score: string;
}


The code is mostly based on the code snippet you shared but with a few changes:

  • I added a few lines to parse the input string into a two-dimensional string array then insert into the worksheet.

  • I slightly adjusted a few lines in the chart generation code to make sure it returns the chart image with the updated chart formatting.

  • I saw you returned [radarchartimage] in your original code. This is returning an array with a single item. Should it just return radarchartimage, which contains the base64-encoded image?


Then on the Power Automate side, you'll need to pass along the input JSON data from the PowerApps trigger into the Run script action. You may need to delete the existing Run script action and insert again to make sure it shows up the data input box.

199383-image.png



image.png (33.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

VigneshBabuSundararajan-9452 avatar image
0 Votes"
VigneshBabuSundararajan-9452 answered YutaoHuang-MSFT edited

Thank You
Is there any reference material for excel script?


Thanks

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The official documentation of Office Scripts is here: https://docs.microsoft.com/en-us/office/dev/scripts/overview/excel. You can find the API references, samples, and some useful tips and tricks there.

And of course do check out these below wonderful Q&A communities, where you can get most of your questions answered by many Office Scripts (and Power Automate) experts.

0 Votes 0 ·