Office.js Add-in: Insert image/picture in Excel 2016

我只是一个虾纸丫 提交于 2020-01-06 07:24:31

问题


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


回答1:


This answer explains how to do this in Word: https://stackoverflow.com/a/38194807/3806701, and similarly, I was able to do it this way 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 reference: https://dev.office.com/reference/add-ins/excel/rangefill

Random website I used to encode an image: https://www.browserling.com/tools/image-to-base64



来源:https://stackoverflow.com/questions/49658664/office-js-add-in-insert-image-picture-in-excel-2016

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!