August 6, 2020
I am working on a project where I can create an image of a chart from excel and save the outcome to a sharepoint folder.
Goal is to create several images which can be displayed easier and faster from a sharepoint site.
For this project I used this script from the microsoft docs as a template.
Email the images of an Excel chart and table - Office Scripts | Microsoft Docs
For some reason my script fails and I am not able to create an image file.
Here is my script, which I have written so far:
function main(workbook: ExcelScript.Workbook) {// Fetch chart and use getImage method to create imagelet selectedSheet = workbook.getActiveWorksheet();console.log('line ran successfully');console.log('selectedSheet created');const chart_1 = selectedSheet.getChart("diagram 15");chart_1.setPosition("I23");console.log('line ran successfully');console.log('chart_1 created and positioned correctly');const img = chart_1.getImage();console.log('line ran successfully');console.log('img created');return {img}}// The interface for chart images in power automate.interface ReportImages {img: string}
When I run this script in Excel Web, everything seems fine and I cannot track any mistakes.
However when I create a flow in power automate, this scripts fail as shown in the screenshot next.
I would be really grateful for any advice
Thanks in advance!
Regards,
Peter
Power Query
Power Pivot
Xtreme Pivot Tables
Excel for Decision Making
Excel for Finance
Excel Analysis Toolpak
Power BI
Excel
Word
Outlook
Excel Expert
Excel Customer Service
PowerPoint
November 8, 2013
Hi Peter,
Not seeing the correct code in your sample.
Microsoft docs has this first line:
function main(workbook: ExcelScript.Workbook): ReportImages {
then ends with:
// The interface for table and chart images.
interface ReportImages { chartImage: string tableImage: string }
You can see that there is a relationship between main and Reportimages, highlighted in red.
In your code the first line is incomplete.
August 6, 2020
Hey Catalin,
thanks for this hint.
I added the corresponding word at the beginning of my code. Now, my code looks like this. (I didn't change the console outcome from German to English - but this shouldn't affect the code after all and I hope you can still give me a try 🙂 )
function main(workbook: ExcelScript.Workbook): ReportImages {let selectedSheet = workbook.getActiveWorksheet();console.log('Zeile erfolgreich');console.log('selectedSheet erstellt');let chart_1 = selectedSheet.getChart("Diagramm 1");chart_1.setPosition("A1");console.log('Zeile erfolgreich');console.log('chart_1 erstellt und positioniert');const img = chart_1.getImage();console.log('Zeile erfolgreich');console.log('img erstellt');return {img}}// The interface for table and chart images.interface ReportImages {img: string}
We were unable to run the script. Please try again.
Runtime error: Line 6: Cannot read property 'setPosition' of undefined
let chart_1 = selectedSheet.getChart("Diagramm 1"); -> const chart_1 = selectedSheet.getChart("Diagramm 1");
Runtime error: Line 6: Cannot read property 'getImage()' of undefined
August 6, 2020
Hey Catalin,
I finally can say I have found a solution.
Really, I do not understand the reason why this works, but I am glad it does.
I came up with the idea to change the code from referring to the active sheet by workbook.getActiveWorksheet() into referring to a specific worksheet like that: sheet_grafiken = workbook.getWorksheet("Grafiken"). This code then works just fine in Power Automate! I will attach a screenshot of the final code.
Next problem was to create an image file from a string coded in base64 and save that image in a sharepoint library.
I came up with a solution and I will attach a screenshot to this post as well so someone else can participate and I will be happy to receive any suggestions for improvement.
Thanks!
Peter
Answers Post
1 Guest(s)