Created
March 23, 2023 17:38
-
-
Save ToxesFoxes/4f4d684db0850f09e9b495f5c86fc7b9 to your computer and use it in GitHub Desktop.
Comment and image in one sheet bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -------------- REQUIREMENTS -------------- | |
// npm install excel4node | |
var xl = require('excel4node'); | |
var http = require('http'); | |
const https = require('https'); | |
var fs = require('fs'); | |
// ----------------- STEP 1 ----------------- | |
// Create a new instance of a Workbook class | |
var wb = new xl.Workbook() | |
var ws = wb.addWorksheet('Test') | |
// ----------------- STEP 2 ----------------- | |
// Add text and comment to cell A1 | |
ws.cell(1, 1).string('Test Cell').comment('Test comment') | |
// ----------------- STEP 3 ----------------- | |
// Download image and add to worksheet | |
/** | |
* Download file from url | |
* @param {string} url url of image | |
* @param {string} filename path to download image | |
* @returns | |
*/ | |
function download(url, filename) { | |
let client = http; | |
if (url.toString().indexOf("https") === 0) { | |
client = https; | |
} | |
return new Promise((resolve, reject) => { | |
client.get(url, (res) => { | |
res.pipe(fs.createWriteStream(filename)) | |
.on('error', reject) | |
.once('close', () => resolve(filename)) | |
}) | |
}) | |
} | |
download('https://dummyimage.com/80x80/000/fff.png&text=TEST', 'test.png').then(() => { | |
// Add image to worksheet and set position | |
ws.addImage({ | |
path: 'test.png', | |
type: 'picture', | |
position: { | |
type: 'oneCellAnchor', | |
from: { | |
col: 1, | |
colOff: 0, | |
row: 2, | |
rowOff: 0 | |
} | |
} | |
}) | |
}).finally(() => { | |
// ----------------- STEP 4 ----------------- | |
// Save workbook into file | |
wb.write('comment-and-image-example.xlsx') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment