Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] The images array is empty. getImages() is not valid! #2732

Open
BrotherPeng opened this issue Mar 30, 2024 · 3 comments
Open

[BUG] The images array is empty. getImages() is not valid! #2732

BrotherPeng opened this issue Mar 30, 2024 · 3 comments

Comments

@BrotherPeng
Copy link

馃悰 Bug Report

Why can't the code get the image information
Lib version: 4.x.x

I need to get the new information about the image and the location

    const workbook = new ExcelJS.Workbook();
    await workbook.xlsx.load(fs.readFileSync(filePath));
    const worksheet = workbook.getWorksheet(1);
    const images = worksheet.getImages()

The images array is empty.

@BrotherPeng BrotherPeng changed the title [BUG] XYZ [BUG] The images array is empty. getImages() is not valid! Mar 30, 2024
@snailuncle
Copy link

Because your image file name ends with an English period (dot), when you save the file in Excel, it will automatically add another period (dot). So, the image file name saved by Excel becomes two dots. However, when ExcelJS is matching image file names with a regular expression, it looks for '.png', not '..png'. Do you understand?

Please remove the dot before the image file name extension.

  const ext = path.extname(local_img_path).toLowerCase();
  const extensionWithoutDot = ext.startsWith(".") ? ext.substring(1) : ext;

If you want to know why this happens, you can look at the code within ExcelJS

this file : lib/xlsx/xlsx.js

async addMedia(zip, model) {
  await Promise.all(
    model.media.map(async medium => {
      if (medium.type === 'image') {
        const filename = `xl/media/${medium.name}.${medium.extension}`;
        if (medium.filename) {
          const data = await fsReadFileAsync(medium.filename);
          return zip.append(data, {name: filename});
        }
        if (medium.buffer) {
          return zip.append(medium.buffer, {name: filename});
        }
        if (medium.base64) {
          const dataimg64 = medium.base64;
          const content = dataimg64.substring(dataimg64.indexOf(',') + 1);
          return zip.append(content, {name: filename, base64: true});
        }
      }
      throw new Error('Unsupported media');
    })
  );
}

this line of code

const filename = `xl/media/${medium.name}.${medium.extension}`;

During the file save process, your image file name extension has changed from '.png' to '..png'.
i think you get it.

The regular expression code for image matching is in this file.
lib/xlsx/xlsx.js

if (entryName.match(/xl\/media\//) || entryName.match(/xl\/theme\/([a-zA-Z0-9]+)[.]xml/)) {
...
}

@snailuncle
Copy link

  const ext = path.extname(local_img_path).toLowerCase();
  const extensionWithoutDot = ext.startsWith(".") ? ext.substring(1) : ext;
  const imageId = workbook.addImage({
    buffer: imageBuffer,
    extension: extensionWithoutDot,
    type: "image",
  });

@snailuncle
Copy link

match = entryName.match(/xl/media/([a-zA-Z0-9]+[.][a-zA-Z0-9]{3,4})$/);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants