Fabian Küng
Fabian Küng

Reputation: 6183

Insert chart as image into Word document from Excel

I have a Word Add-In where I want the user to enter a path to an Excel file (that resides somewhere on a network drive), read all the charts from the Excel file, display a small summary of all the charts and then let the user select one and insert it into the document as an image.

Similar to this example here, except I don't want the Excel files to be hosted on OneDrive (just a local directory) and I am doing it in pure JS/HTML, no ASP.NET: https://github.com/OfficeDev/PowerPoint-Add-in-Microsoft-Graph-ASPNET-InsertChart

I am struggling with the first part, accessing the Excel file from Word. How do I do that? As per this post Load Excel file in MS Word Add-in with Office.js from Oct 2016 it might be added in the future. Any news on that or is the only way to go still via OneDrive?

Once I can open the Excel and access its content in Javascript I think it is straight forward, e.g.:

Get all the charts

var charts = ctx.workbook.worksheets.getActiveWorksheet().charts.load("name");

Convert them to images

chartObject.getImage(height, width, fittingMode);

When the user selects one I just insert it into the document at the selection.

Upvotes: 0

Views: 332

Answers (1)

Sudhi Ramamurthy
Sudhi Ramamurthy

Reputation: 2478

I'm afraid the answer is still the same. Currently, there is no way to open the Excel file locally hosted using Office-JS APIs. We are working on enabling Excel REST API for OneDrive consumer platform. So, if you have a OneDrive sync-client on the PC, you can seamlessly access the Excel file from OneDrive and read/write to it and the file will be synced to local drive. It does mean you'd connect over the network to read Excel file.

You could also look at Open XML SDK to build a fully local solution. It is bit heavy handed for a simple scenario such as getting some data. Excel REST APIs are ideal for such scenarios if you don't mind going over the network.

Upvotes: 1

Related Questions