office.js insert table to powerpoint

Viewed 396

I'm trying to make a PowerPoint add-in using office.js. The add-in is suppose to collect data from an external source and insert a table containing the data into PowerPoint, but I'm facing some trouble inserting the table into Powerpoint. It seems office.js don't have functionality of inserting tables into PowerPoint(only for Excel and Word).

  • Is there any other good solution for making an add-in that can solve the problem of inserting a table into PowerPoint?
  • Does someone know if Office.js will support inserting tables to PowerPoint in the nearby future?

A code snippet of what I'm trying to do:

 function insertTable() {
    var table = new Office.TableData();
    table.headers = [['Header', 'Header']];
    table.rows = [['Entry', 'Entry'], ['Entry', 'Entry'], ['Entry', 'Entry']];
    Office.context.document.setSelectedDataAsync(table, {coercionType: Office.CoercionType.Table},
        function (asyncResult) {
            if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                console.log(asyncResult.error.message);
            }
        });
}

-->"The enumeration isn't supported in the current host application."

1 Answers

I had to port an old VSTO AddIn to Office.js and was struggling with adding Charts and tables to PowerPoint, Up to my knowledge there is no straight forward way to do this:

The Office.js AddIns run on browsers (Edge Legacy if used within an office program),so Magic of JavaScript to the rescue.

Office.js provides a way to insert slides from existing presentations to the active presentation. There are many JavaScript libraries which provide support for PowerPoint presentation using OpenXML standard. I have used PPTXGenJS

With this library I've created a presentation in memory, converted it to base64 format and inserted it to the active presentation like below:

var pres = new PptxGenJS();
  var slide = pres.addSlide();
  slide.addTable(['Color', 'Value'], ['Red', 1], ['Blue', 2]);
  pres.write('base64').then(async function (data) {
    await PowerPoint.run(async function (context) {
      const selectedSlideID = 256;//await getSelectedSlideID();
      context.presentation.insertSlidesFromBase64(data, { formatting: PowerPoint.InsertSlideFormatting.useDestinationTheme, targetSlideId: selectedSlideID + "#" });
      await context.sync();
    });

  });
Related