Lavorare con la gestione dei fogli di calcolo
Gestire i fogli di lavoro
La classe WorksheetCollection fornisce metodi per gestire i fogli di lavoro all’interno di una cartella di lavoro. Aggiungi nuovi fogli di lavoro con addWorksheet(), rimuovi con removeWorksheet() e riordina con moveWorksheet().
import { Workbook } from "excel-cells";
const workbook = new Workbook();
const sheets = workbook.worksheets;
const sheet2 = sheets.addWorksheet("Sales");
const sheet3 = sheets.addWorksheet("Summary");
sheet2.putValue("A1", "Sales Data");
sheet3.putValue("A1", "Summary Report");
await workbook.save("multi-sheet.xlsx");Valori delle celle e formule
Scrivi interi, decimali e stringhe con worksheet.putValue(). Accedi alle celle per riferimento con getCell2() o per indice di riga/colonna con getCell(). Imposta le formule con cell.setFormula().
const workbook = new Workbook();
const worksheet = workbook.worksheets[0]!;
worksheet.putValue("A1", 42);
worksheet.putValue("A2", 3.14159);
worksheet.putValue("A3", "Hello World");
const cellA4 = worksheet.getCell2("A4");
cellA4.setFormula("=SUM(A1:A2)");
await workbook.save("values.xlsx");Stile di carattere e riempimento
Applica le impostazioni del carattere usando la classe Style. Imposta il nome del carattere, la dimensione, il grassetto, il corsivo e il colore. Applica lo stile alle singole celle con cell.setStyle().
import { Workbook, Style } from "excel-cells";
const workbook = new Workbook();
const worksheet = workbook.worksheets[0]!;
const style = new Style();
style.setFontName("Arial");
style.setFontSize(14);
style.setBold(true);
style.setItalic(true);
style.setFontColor("FF0000");
const cell = worksheet.getCell2("A1");
cell.putValue("Styled Text");
cell.setStyle(style);
await workbook.save("styled.xlsx");Convalida dei dati
Aggiungi regole di convalida agli intervalli di celle usando DataValidation. Specifica il tipo di convalida (elenco, intervallo numerico o personalizzato) e applica con worksheet.addDataValidation().
import { Workbook, DataValidation } from "excel-cells";
const workbook = new Workbook();
const worksheet = workbook.worksheets[0]!;
const validation = new DataValidation();
validation.type = "list";
validation.formula1 = '"Option1,Option2,Option3"';
worksheet.addDataValidation(validation, "B1:B10");
await workbook.save("validated.xlsx");Filtro automatico
Abilita intestazioni di colonna filtrabili con worksheet.setAutoFilter(). Rimuovi i filtri con worksheet.removeAutoFilter().
const workbook = new Workbook();
const worksheet = workbook.worksheets[0]!;
worksheet.putValue("A1", "Name");
worksheet.putValue("B1", "Age");
worksheet.putValue("C1", "City");
worksheet.putValue("A2", "Alice");
worksheet.putValue("B2", "25");
worksheet.putValue("C2", "New York");
worksheet.setAutoFilter("A1:C4");
await workbook.save("filtered.xlsx");Hyperlinks
Allega collegamenti ipertestuali alle celle con cell.setHyperlink(). Supporta URL, indirizzi email e riferimenti interni al foglio.
const workbook = new Workbook();
const worksheet = workbook.worksheets[0]!;
worksheet.putValue("A1", "Click here");
worksheet.getCell2("A1").setHyperlink("https://www.example.com");
worksheet.putValue("A3", "Send email");
worksheet.getCell2("A3").setHyperlink("mailto:test@example.com");
await workbook.save("hyperlinks.xlsx");Protezione della cartella di lavoro
Proteggi le cartelle di lavoro con una password usando workbook.protect(). Blocca singole celle con style.setLocked(true).
import { Workbook, Style } from "excel-cells";
const workbook = new Workbook();
const worksheet = workbook.worksheets[0]!;
worksheet.putValue("A1", "Protected Cell");
const style = new Style();
style.setLocked(true);
worksheet.getCell2("A1").setStyle(style);
workbook.protect(true, "password");
await workbook.save("protected.xlsx");Dimensionamento di colonne e righe
Imposta le larghezze delle colonne e le altezze delle righe per controllare le dimensioni delle celle nel file XLSX di output.
const workbook = new Workbook();
const worksheet = workbook.worksheets[0]!;
worksheet.putValue("A1", "Wide Column");
worksheet.setColumnWidth(0, 30);
worksheet.setRowHeight(0, 25);
await workbook.save("sized.xlsx");