Skip to content

Commit

Permalink
Merge pull request #190 from jdugh/insert_image_in_cell
Browse files Browse the repository at this point in the history
Insert image in cell table
  • Loading branch information
optilude authored Apr 18, 2024
2 parents 52c6ba8 + 66f502b commit 599f982
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,8 @@ class Workbook {
newCellsInserted = self.substituteArray(cells, cell, value);
} else if (placeholder.subType == 'image' && value != "") {
self.substituteImage(cell, placeholder.placeholder, placeholder, value, drawing);
} else if (placeholder.subType === "imageincell" && value != "") {
self.substituteImageInCell(cell, value);
} else {
self.insertCellValue(cell, value);
}
Expand Down Expand Up @@ -1045,6 +1047,9 @@ class Workbook {
self.updateRowSpan(newRow, newCellsInsertedOnNewRow);
} else if (placeholder.subType == 'image' && value != '') {
self.substituteImage(newCell, placeholder.placeholder, placeholder, value, drawing);
} else if (placeholder.subType === "imageincell" && value != "") {
self.substituteImageInCell(newCell, value);
newRow.append(newCell);
} else {
self.insertCellValue(newCell, value);

Expand Down
39 changes: 39 additions & 0 deletions test/crud-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,45 @@ describe("CRUD operations", function() {
});
});

it("Insert imageincells table", function(done) {
fs.readFile(path.join(__dirname, 'templates', 'test-insert-images_in_cell.xlsx'), function(err, data) {
expect(err).toBeNull();
var option = {
imageRootPath : path.join(__dirname, 'templates', 'dataset')
}
var t = new XlsxTemplate(data, option);
var imgB64 = 'iVBORw0KGgoAAAANSUhEUgAAALAAAAA2CAYAAABnXhObAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAUjSURBVHhe7ZtbyGVjGMfXlmmccpqRcYgZmQsTMeVYQiJzozEYJRHCcCUaNy5kyg3KhUNEhjSRbwipIcoF0pRpxiFiLsYUIqcoZ8b2/6/1vKtnv3u9+/smuXjW/v/qv593PetZa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wgU3av2YhguOHBX42IA0J/Fj2N4se3/tOwg8j0qwH3+POpjnC/z1G1RCipzxn0eMfBU8y8C70AINcyKeT4iAMIy6y9u3QN02zWoP8y9D50OHQMuTOaFaVQd0JuWxVJ1i/N7SUwqI/QY5CbjG0vy33hrorBmPddCSB2YYQm7L69y3ftW6L5eshBFhiy9R6X5sEOD+caja4PG8QeROX1pXUNYQoaX6qzQV4JcnrvTo/fzSl45nWHpj4YUT+Q5y8B+YPhRJ8nDsG/qjMf9UsVVdYZJ43hpxS4+NjPsHL4Xj8EdTxUXEi7adE/Q0WKD5mNno1rOClr7FzcxkMDS6P+zDiWNIsQg3yeyHHG6kRmGcsrfP50r5zZqvD+sUIh0L8ZdznqONj4zHSZ+ui6/N6sG2xY8K2/1gzNDjG2re9MrCYHpKBp3kIIXqADCxCIwOL0MjAIjQysAiNDCxCIwOL0MjAIjQysAiNDCxCIwOL0MjAIjQysAiNDCxCIwOL0MjAIjQysAhN+x8ZQkREPbAITFX9C5ozpqaetbGcAAAAAElFTkSuQmCC';

t.substitute('table_image_in_cell', {
imgArray : [
{filename : imgB64},
{filename : imgB64},
{filename : imgB64},
],
someText : "Hello ImageInCell",
});
var newData = t.generate();
var richDataFile = etree.parse(t.archive.file("xl/richData/_rels/richValueRel.xml.rels").asText()).getroot();
expect(richDataFile.findall("Relationship").length).toEqual(3);
var richDataFile = etree.parse(t.archive.file("xl/richData/rdrichvalue.xml").asText()).getroot();
expect(richDataFile.findall("rv").length).toEqual(3);
expect(parseInt(richDataFile.attrib.count)).toEqual(richDataFile.findall("rv").length);
var richDataFile = etree.parse(t.archive.file("xl/richData/richValueRel.xml").asText()).getroot();
expect(richDataFile.findall("rel").length).toEqual(3);
var richDataFile = etree.parse(t.archive.file("xl/metadata.xml").asText()).getroot();
expect(parseInt(richDataFile.find('futureMetadata').attrib.count)).toEqual(3);
expect(parseInt(richDataFile.find('valueMetadata').attrib.count)).toEqual(3);
expect(richDataFile.find('futureMetadata').findall("bk").length).toEqual(3);
expect(richDataFile.find('valueMetadata').findall("bk").length).toEqual(3);
var sheet = etree.parse(t.archive.file("xl/worksheets/sheet2.xml").asText()).getroot();
expect(sheet.find("./sheetData/row[@r='2']/c[@r='A2']").attrib.vm).toEqual("1");
expect(sheet.find("./sheetData/row[@r='3']/c[@r='A3']").attrib.vm).toEqual("2");
expect(sheet.find("./sheetData/row[@r='4']/c[@r='A4']").attrib.vm).toEqual("3");
fs.writeFileSync('test/output/insert_imageincell_table.xlsx', newData, 'binary');
done();
});
});

it("Insert some format of image", function(done) {
fs.readFile(path.join(__dirname, "templates", "test-insert-images.xlsx"), function(err, data) {
expect(err).toBeNull();
Expand Down
Binary file modified test/templates/test-insert-images_in_cell.xlsx
Binary file not shown.

0 comments on commit 599f982

Please sign in to comment.