Office.js add-in: Insert image / image in Excel 2016

How do you add a simple image to a specific location in an Excel spreadsheet using the new Office.js api?

0
source share
1 answer

This answer explains how to do this in Word: fooobar.com/questions/1646582 / ... , and similarly I was able to do it like this: Excel:

insertImageBase64New() {
  Excel.run(async (ctx) => {

    let sheet = ctx.workbook.worksheets.getItem("Sheet1");
    let address = "C3";
    let range = sheet.getRange(address);
    range.select();
    await ctx.sync();

    Office.context.document.setSelectedDataAsync(this.getBase64(), {
      coercionType: Office.CoercionType.Image,
      imageLeft: 0,
      imageTop: 0,
      imageWidth: 300,
      imageHeight: 100
    }, function(asyncResult) {
        if (asyncResult.status === Office.AsyncResultStatus.Failed) {
          console.log("Action failed with error: " + asyncResult.error.message);
        }
    });
  });
}

getBase64() {
    return "return "iVBORw0KGgoAAAANSUhEU..."; //put your base64 encoded image here
}

Documentation link: https://dev.office.com/reference/add-ins/excel/rangefill

I used a random site to encode the image: https://www.browserling.com/tools/image-to-base64

0
source

All Articles