Hinzufügen von Bildern zu einer Arbeitsmappe

In diesem Beispiel wird gezeigt, wie Sie mit Bildern mithilfe eines Office-Skripts in Excel arbeiten.

Szenario

Bilder helfen bei Branding, visueller Identität und Vorlagen. Sie tragen dazu bei, dass eine Arbeitsmappe mehr als nur eine riesige Tabelle ist.

Im ersten Beispiel wird ein Bild von einem Arbeitsblatt in ein anderes kopiert. Dies kann verwendet werden, um das Logo Ihres Unternehmens an der gleichen Position auf jedem Blatt zu platzieren.

Im zweiten Beispiel wird ein Bild aus einer URL kopiert. Dies kann verwendet werden, um Fotos, die ein Kollege in einem freigegebenen Ordner gespeichert hat, in eine zugehörige Arbeitsmappe zu kopieren. Beachten Sie, dass dieses Beispiel nicht für die Verwendung mit einer lokalen Bilddatei angepasst werden kann, da dies von Office-Skripts nicht unterstützt wird.

Setup: Excel-Beispieldatei

Diese Arbeitsmappe enthält die Daten, Objekte und Formatierungen, die vom Skript erwartet werden.

Beispielcode: Kopieren eines Bilds über Arbeitsblätter hinweg

Fügen Sie der Beispielarbeitsmappe das folgende Skript hinzu, und probieren Sie das Beispiel selbst aus!

/**
 * 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");
}

Beispielcode: Hinzufügen eines Bilds aus einer URL zu einer Arbeitsmappe

Wichtig

Dieses Beispiel funktioniert aufgrund des fetch Aufrufs nicht in Power Automate.

async function main(workbook: ExcelScript.Workbook) {
  // Fetch the image from a URL.
  const link = "https://raw.githubusercontent.com/OfficeDev/office-scripts-docs/master/docs/images/git-octocat.png";
  const response = await fetch(link);

  // 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;
}