Coder Social home page Coder Social logo

Image about office-scripts-docs HOT 5 CLOSED

Durgesh1402 avatar Durgesh1402 commented on August 10, 2024
Image

from office-scripts-docs.

Comments (5)

ElizabethSamuel-MSFT avatar ElizabethSamuel-MSFT commented on August 10, 2024

@Durgesh1402 Thanks for asking about this.

@sumurthy Can you provide any guidance or example here?

Thanks.

from office-scripts-docs.

mscharlock avatar mscharlock commented on August 10, 2024

Following up @sumurthy :)

from office-scripts-docs.

AlexJerabek avatar AlexJerabek commented on August 10, 2024

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.

AlexJerabek avatar AlexJerabek commented on August 10, 2024

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.

AlexJerabek avatar AlexJerabek commented on August 10, 2024

I've created an internal work item to add this sample to the docs in some capacity.

from office-scripts-docs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.