Comments (5)
@Durgesh1402 Thanks for asking about this.
@sumurthy Can you provide any guidance or example here?
Thanks.
from office-scripts-docs.
Following up @sumurthy :)
from office-scripts-docs.
Hi @Durgesh1402,
Here's a sample script to copy an image from one worksheet to another:
/**
* This script transfers an image from one worksheet to another.
*/
function main(workbook: ExcelScript.Workbook)
{
// Get the worksheet with the image on it.
let firstWorksheet = workbook.getWorksheet("FirstSheet");
// Get the first image from the worksheet.
// If a script added the image, you could add a name to make it easier to find.
let image: ExcelScript.Image;
firstWorksheet.getShapes().forEach((shape, index) => {
if (shape.getType() === ExcelScript.ShapeType.image) {
image = shape.getImage();
return;
}
});
// Copy the image to another worksheet.
image.getShape().copyTo("SecondSheet");
}
I'll look into the second scenario (insert an image from a folder) as well. In the meantime, you can use the Record Actions button to generate a script based on UI actions. So, you can record yourself inserting the image you want.
from office-scripts-docs.
Hi @Durgesh1402,
Here's a sample from @wandyezj on how to add an image from a URL to the current worksheet:
async function main(workbook: ExcelScript.Workbook) {
const link = "https://pnp.github.io/images/parker-p-500.png"
const response = await fetch(link);
const data = await response.arrayBuffer();
const image = convertToBase64(data);
workbook.getActiveWorksheet().addImage(image)
}
function convertToBase64(input: ArrayBuffer) {
const uInt8Array = new Uint8Array(input);
const count = uInt8Array.length;
const charCodeArray = new Array(count) // allocate all space up front
// Convert all characters
let i = count;
while (i--) {
charCodeArray[i] = String.fromCharCode(uInt8Array[i]);
}
// convert to base 64
const base64 = btoa(charCodeArray.join(''));
return base64;
}
A couple things about that script:
- You can use any publicly accessible link for that image. You can't use anything that requires authentication (like on a company server).
- It uses
fetch
and there are some things to be aware of there. See External API call support in Office Scripts for details. - The complexity comes from converting the raw image data to a base64-encoded string, which is the required input for the
addImage
method. I'd recommend requesting a more streamlined API through UserVoice. We use UserVoice to prioritize community requests based on their upvotes. I imagine that your scenario is common and others will appreciate and support it.
from office-scripts-docs.
I've created an internal work item to add this sample to the docs in some capacity.
from office-scripts-docs.
Related Issues (20)
- This action cannot be recorded and cannot copy as code. HOT 12
- Issue in the sample HOT 5
- Documentation should reflect that refresh is supported for Power BI
- Error: Line 27: Table addRows: This won't work because it would move cells in a table on your worksheet. HOT 8
- Contradicting wording in docs? HOT 7
- The ability to simply update tables in XLSM is often inquired about - is it possible? HOT 1
- 'Open in VSCode' doesn't work HOT 5
- Move rows across tables additional filters HOT 3
- Excel Office Scripting default parameters HOT 8
- Tutorial: Update a spreadsheet from a Power Automate flow need update. HOT 1
- "Run Script" action not available HOT 1
- Run Office Scripts in Excel from buttons page out of date HOT 1
- 'excelonlinebusiness' operation 'GetSingleScript' failed with status code 'BadRequest' "status": 400, "message": "Unable to parse script reference" HOT 3
- Script converting then corrupt XLSX? HOT 6
- Wrong title of sample code HOT 1
- No information if refreshAllDataConnections() is supposed to wait for refresh to finish before it continues. HOT 4
- The wrong chart type HOT 1
- Error after Run the code HOT 3
- Platfrom support is inconsistent with other documentations HOT 3
- Ability to Change Column Header Text in Pivot Table HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from office-scripts-docs.