beaverbuzz
New Member
- Joined
- Aug 15, 2022
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
Good morning,
I wrote a thread a few days ago about my issue of automating the embedding an image in a cell in Excel.
I found a solution through using web Excel and automation of a workbook through Microsoft script (not VBA...).
The solution included a script which I tweaked for my purpose.
Please find attached a screenshot of the problem I am encountering with it.
Also please find below the entirety of the script.
Any suggestions/help to reconcile that problem would be appreciated!
beaverbuzz
I wrote a thread a few days ago about my issue of automating the embedding an image in a cell in Excel.
I found a solution through using web Excel and automation of a workbook through Microsoft script (not VBA...).
The solution included a script which I tweaked for my purpose.
Please find attached a screenshot of the problem I am encountering with it.
Also please find below the entirety of the script.
Any suggestions/help to reconcile that problem would be appreciated!
beaverbuzz
Excel Formula:
async function main(workbook: ExcelScript.Workbook) {
// Fetch the image from a URL.
const sheet = workbook.getWorksheet("Form1");
const response = await fetch(sheet.getRange("I7"));
// Store the response as an ArrayBuffer, since it is a raw image file.
const data = await response.arrayBuffer();
// Convert the image data into a base64-encoded string.
const image = convertToBase64(data);
// Add the image to a worksheet.
workbook.getWorksheet("WebSheet").addImage(image);
}
/**
* Converts an ArrayBuffer containing a .png image into a base64-encoded string.
*/
function convertToBase64(input: ArrayBuffer) {
const uInt8Array = new Uint8Array(input);
const count = uInt8Array.length;
// Allocate the necessary space up front.
const charCodeArray = new Array(count) as string[];
// Convert every entry in the array to a character.
for (let i = count; i >= 0; i--) {
charCodeArray[i] = String.fromCharCode(uInt8Array[i]);
}
// Convert the characters to base64.
const base64 = btoa(charCodeArray.join(''));
return base64;
}