Partager via


ExcelScript.TextRange interface

Contient le texte lié à une forme, en plus des propriétés et méthodes de manipulation du texte.

Remarques

Exemples

/**
 * This script adds text to a shape.
 */
function main(workbook: ExcelScript.Workbook) {
  // Create a hexagon shape in the current worksheet.
  const sheet = workbook.getActiveWorksheet();
  const hexagon = sheet.addGeometricShape(ExcelScript.GeometricShapeType.hexagon);
  
  // Set the text of the shape.
  const hexText: ExcelScript.TextRange = hexagon.getTextFrame().getTextRange();
  hexText.setText("Forest");
}

Méthode

getFont()

Renvoie un ShapeFont objet qui représente les attributs de police de la plage de texte.

getSubstring(start, length)

Renvoie un objet TextRange pour les caractères dans la plage de donnée.

getText()

Représente le contenu de texte brut de la plage de texte.

setText(text)

Représente le contenu de texte brut de la plage de texte.

Détails de la méthode

getFont()

Renvoie un ShapeFont objet qui représente les attributs de police de la plage de texte.

getFont(): ShapeFont;

Retourne

Exemples

/**
 * This sample sets the font of a shape to be bold. 
 */
function main(workbook: ExcelScript.Workbook) {
  // Get the first shape in the current worksheet.
  const sheet = workbook.getActiveWorksheet();
  const shape = sheet.getShapes()[0];

  // Get the text font from the shape.
  const text: ExcelScript.TextRange = shape.getTextFrame().getTextRange();
  const shapeTextFont: ExcelScript.ShapeFont = text.getFont();

  // Set the font to be bold.
  shapeTextFont.setBold(true);
}

getSubstring(start, length)

Renvoie un objet TextRange pour les caractères dans la plage de donnée.

getSubstring(start: number, length?: number): TextRange;

Paramètres

start

number

Index de base zéro du premier caractère à obtenir à partir de la plage de texte.

length

number

Facultatif. Nombre de caractères à retourner dans la nouvelle plage de texte. Si length est omis, tous les caractères du début à la fin du dernier paragraphe de la plage de texte sont retournés.

Retourne

getText()

Représente le contenu de texte brut de la plage de texte.

getText(): string;

Retourne

string

Exemples

/**
 * This script writes all the text from the workbook's geometric shapes in a new worksheet.
 */
function main(workbook: ExcelScript.Workbook) {
  // Create a new worksheet.
  const shapeTextSheet = workbook.addWorksheet("ShapeText");
  let shapeTextValues: string[][] = [];

  // Get the text from every geometric shape in every worksheet.
  workbook.getWorksheets().forEach((sheet) => {
    sheet.getShapes().forEach((shape) => {
      if (shape.getType() === ExcelScript.ShapeType.geometricShape)
      shapeTextValues.push([
        sheet.getName(),
        shape.getGeometricShapeType().toString(),
        shape.getTextFrame().getTextRange().getText()]);
    });
  });

  // Add the text to the new worksheet.
  const range = shapeTextSheet.getRangeByIndexes(
    0,
    0, 
    shapeTextValues.length, 
    shapeTextValues[0].length);
  range.setValues(shapeTextValues);
}

setText(text)

Représente le contenu de texte brut de la plage de texte.

setText(text: string): void;

Paramètres

text

string

Retourne

void