Office.js 插件:在 Excel 中插入图像/图片 (Office 365)

cs_*_*pil 3 javascript office-addins office-js

如何使用新的 Office.js api 将简单图像添加到 Excel 电子表格上的特定位置?

cs_*_*pil 5

这个答案解释了如何在Word中执行此操作:/sf/answers/2673636521/,同样,我可以在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
}
Run Code Online (Sandbox Code Playgroud)

文档参考: https: //dev.office.com/reference/add-ins/excel/rangefill

我用来编码图像的随机网站:https://www.browserling.com/tools/image-to-base64