问题
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