question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

adding Image doesn't work

See original GitHub issue

My files: image

My excel file: image

My code

const excel = require('exceljs');
const fs = require('fs');
(async () => {
    try {
        const wb = new excel.Workbook();
        const file = await wb.xlsx.readFile('excel1.xlsx');
        const ws = file.getWorksheet('lal'); // sheetname is 'lal'
        
        const imgId = wb.addImage({
            buffer: fs.readFileSync('dk2.png'),
            extension: 'png'
        });

        // not work
        ws.addBackgroundImage(imgId);
        // not work
        ws.addImage(imgId, 'I1:I2');
        // not work
        ws.addImage(imgId, {
            tl: { col: 8, row: 2 },
            br: { col: 8, row: 2 }
        });
        console.log('file img', imgId); // id=0

    } catch (error) {
        console.log('error', error);
    }
})();

after running the code , the excel1.xlsx changed nothing, there wasn’t an image in it. and the code didn’t throw any error or warning, could anyone help me? @guyonroche

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

3reactions
guyonrochecommented, Mar 20, 2018

@labs00 I see that you are reading the excel1.xlsx file at the start, but not ‘saving’ it after adding the image. Until you save (using writeFile) the changes will only be in memory

0reactions
gluhovromacommented, Nov 15, 2019

can we have complete code along with writeFile()? I am looking for to save the image in excel using javascript.

Hello, Nodejs example:

 async getReport(data, type, translations) {
       let buffer;
       switch(type) {
           case "pdf":
           buffer = await this.pdfBuilder.getPdfBuffer(data, translations);
           break;
           case "xls":
           buffer = await this.excelBuilder.getExelBuffer(data, translations);
           break;
       }
       return buffer
   }
writeFile(buffer:any, format:string) {       
    return new Promise((resolve, reject) => {            
        let buf = Buffer.from(buffer);
        let title = this.createHash() + '.' +  format;
        fs.writeFile(__dirname +'/output/' + title, buf, function(err) {
            if (err) {
                return  reject(err);
            }
            return resolve(title);
          });
    });
}
    createHash() {
        return  crypto.randomBytes(20).toString('hex');
    }
   public async getExelBuffer(report: any, translations:any): Promise<any> {         
        let workbook: Workbook = await this.createWorkBook(report, translations);
        return await workbook.xlsx.writeBuffer()
    }

browser example:

getExelBuffer() and then

createBlob(data: any, name:string, type:string, mimeType:string) {
        let blob = new Blob([data], { type : mimeType });
        let a = document.createElement("a");
        document.body.appendChild(a);
        let url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = this.createPath(name, type);
        a.click();
        window.URL.revokeObjectURL(url);
    }

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML insert Image doesn't work - Stack Overflow
Right click the broken image and "Open Image in New Tab ", and you'll see the URL of where the image is trying...
Read more >
7 Reasons Why Images Are Not Loading on Your Website
7 Reasons Why Images Are Not Loading on Your Website · 1. Incorrect File Paths · 2. Files Names Misspelled · 3. Wrong...
Read more >
Troubleshoot issues with Google Images - Google Search Help
Troubleshoot issues with Google Images · Step 1: Try private browsing mode · Step 2: Clear your browser's cache & cookies · Step...
Read more >
I can't add an Image - HTML & CSS - SitePoint Forums
I am new to CSS and I am trying to do something simple but for some reason it's not working for me. All...
Read more >
How to fix image not showing in an HTML page
How to fix image not showing in an HTML page · Check the src path to of the <img> tag · Check the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found