-
Notifications
You must be signed in to change notification settings - Fork 6
Basic image embedding and row height feature #3
Conversation
… keys method more defensive
fixing multiple images, keys method and adding row height config
Thanks @tarwich. FWIW I thought I might add how I used this lib in my POC. <!-- this is part of an angular2 template -->
<input type="file" accept="image/*" capture="camera" (change)="onPicChange(pic)" #pic /> // method is part of a class!
onPicChange(data: any) : void {
var self = this;
var reader = new FileReader();
reader.addEventListener("load", function () {
var img = new Image();
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
img.onload = function()
{
var maxWidth = 300;
var width = img.width;
var height = img.height;
if (width > maxWidth) {
height *= maxWidth / width;
width = maxWidth;
}
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, width, height);
self.picSrc = canvas.toDataURL("image/jpeg");
self.picBlob = self.picSrc.split(',')[1];
}
img.src = reader.result;
}, false);
reader.readAsDataURL(data.files[0]);
} Then it follow the example given initially: workbook.Sheets['Order-' + id]['!images'] = [
{ name: 'image1.jpg',
data: this.picBlob,
opts: { base64: true },
position: {
type: 'twoCellAnchor',
attrs: { editAs: 'oneCell' },
from: { col: 2, row : 2 },
to: { col: 6, row: 5 }
}
}, I also stored the image data in an offline storage in base64 (not efficient, but works). HTH |
Hi, I have tested your code but my export omits the images embedded. This is the table that generates the excel contents:
JS File:codecBase64=(url, callback)=>{
Kindly advice what am doing wrong here. Thank you |
I'm no longer maintaining this repo, so I don't know. I'm willing to give you a guess, though. Check your base64 and make sure it's valid. I don't know if the rest of the code works, but you can validate your base64 to know if that's where the issue is. |
This is a copy of the pull request submitted here: SheetJS#509
This adds the ability to embed images in the Excel exports. It also adds the ability to specify row height.
Usage for image embedding:
Usage for row height:
These are basic changes to add these features, and so could be further improved to offer more functionality / configuration. However the general approach seems a reasonable basis for this and has been tested and found to work well.
Credit to @mgreter and @paulish for the original code changes. My only changes were a few fixes for multiple images.