Compartilhar via


Criar um sumário da pasta de trabalho

Este exemplo mostra como criar um índice para o livro. Cada entrada no índice é uma hiperligação para uma das folhas de cálculo no livro.

A folha de cálculo do índice a mostrar ligações para as outras folhas de cálculo.

Configuração: ficheiro do Excel de exemplo

Este livro contém os dados, objetos e formatação esperados pelo script.

Código de exemplo: Criar um índice de livro

Execute o seguinte script no livro de exemplo e experimente o exemplo! Abra o Editor de Código ao aceder a Automatizar>a Criação de Novo Script>no Editor de Código, substitua o código predefinido pelo código de exemplo que pretende executar e, em seguida, selecione Executar.

function main(workbook: ExcelScript.Workbook) {
  // Insert a new worksheet at the beginning of the workbook.
  let tocSheet = workbook.addWorksheet();
  tocSheet.setPosition(0);
  tocSheet.setName("Table of Contents");

  // Give the worksheet a title in the sheet.
  tocSheet.getRange("A1").setValue("Table of Contents");
  tocSheet.getRange("A1").getFormat().getFont().setBold(true);

  // Create the table of contents headers.
  let tocRange = tocSheet.getRange("A2:B2")
  tocRange.setValues([["#", "Name"]]);

  // Get the range for the table of contents entries.
  let worksheets = workbook.getWorksheets();
  tocRange = tocRange.getResizedRange(worksheets.length, 0);

  // Loop through all worksheets in the workbook, except the first one.
  for (let i = 1; i < worksheets.length; i++) {
    // Create a row for each worksheet with its index and linked name.
    tocRange.getCell(i, 0).setValue(i);
    tocRange.getCell(i, 1).setHyperlink({
      textToDisplay: worksheets[i].getName(),
      documentReference: `'${worksheets[i].getName()}'!A1`
    });
  };

  // Activate the table of contents worksheet.
  tocSheet.activate();
}