• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Office Scripts - Excel for Web|Dashboards & Charts|Excel Forum|My Online Training Hub

You are here: Home / Office Scripts - Excel for Web|Dashboards & Charts|Excel Forum|My Online Training Hub

vba course banner

Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumDashboards & ChartsOffice Scripts - Excel for Web
sp_PrintTopic sp_TopicIcon
Office Scripts - Excel for Web
Avatar
Peter Fresh
Member
Members
Level 0
Forum Posts: 15
Member Since:
August 6, 2020
sp_UserOfflineSmall Offline
1
May 14, 2021 - 9:25 pm
sp_Permalink sp_Print sp_EditHistory

2021-05-14-13_17_53-.pngImage Enlarger

Hey guys,

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 image
  let 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

sp_PlupAttachments Attachments
  • sp_PlupImage 2021-05-14-13_17_53-.png (141 KB)
sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
May 16, 2021 - 2:34 pm
sp_Permalink sp_Print

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.

Avatar
Peter Fresh
Member
Members
Level 0
Forum Posts: 15
Member Since:
August 6, 2020
sp_UserOfflineSmall Offline
3
May 17, 2021 - 8:33 pm
sp_Permalink sp_Print

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 
}
For some reason, it is still not working.
When I run this code in Excel Web, everythings seems fine just as before. In Power Automate I get following message:
 
We were unable to run the script. Please try again.
Runtime error: Line 6: Cannot read property 'setPosition' of undefined
To me, it looks like Power Automate can't handle the variable of chart_1. My guess was it to change the code as following:
 
let chart_1 = selectedSheet.getChart("Diagramm 1"); -> const chart_1 = selectedSheet.getChart("Diagramm 1");
 
 
Unfortunately, this did not work just like before. I got the same error message from Power Automate.
 
Last guess from me was it to delete the line with the setPosition method - this led to the same error message with the little change of now saying:
 
Runtime error: Line 6: Cannot read property 'getImage()' of undefined
Do you have another idea to fix this problem?
 
Regards,
Peter
 
 
 
Avatar
Peter Fresh
Member
Members
Level 0
Forum Posts: 15
Member Since:
August 6, 2020
sp_UserOfflineSmall Offline
4
May 18, 2021 - 12:02 am
sp_Permalink sp_Print

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 

sp_PlupAttachments Attachments
  • sp_PlupImage final_code-1.png (24 KB)
  • sp_PlupImage flow_creating_png-1.png (51 KB)
sp_AnswersTopicAnswer
Answers Post
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1824
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
5
May 18, 2021 - 5:01 am
sp_Permalink sp_Print

When is the Power Automate flow triggered? If the book is not open in Excel Web, there may be no active sheet.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Riny van Eekelen, Lavina Dhanwani, Jorn Bast, Malcolm Sawyer
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 216
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Melanie Ford
Isaac Felbah
Adele Glover
Hitesh Asrani
Rohan Abraham
Anthony van Riessen
Erlinda Eloriaga
Abisola Ogundele
MARTYN STERRY
Rahim Lakhani
Forum Stats:
Groups: 3
Forums: 24
Topics: 6355
Posts: 27792

 

Member Stats:
Guest Posters: 49
Members: 32324
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.