Crear y copiar rangos con nombre usando JavaScript mediante C++
Introducción
Normalmente, las etiquetas de columnas y filas se usan para referirse a celdas individuales. Es posible crear nombres descriptivos para representar celdas, rangos de celdas, fórmulas o valores constantes. La palabra nombre puede referirse a una cadena de caracteres que representa una celda, rango de celdas, fórmula o valor constante. Asignar un nombre a un rango significa que ese rango de celdas puede ser referido por su nombre. Usa nombres fáciles de entender, como Productos, para referirte a rangos difíciles de entender, como Ventas!C20:C30. Las etiquetas pueden usarse en fórmulas que hacen referencia a datos en la misma hoja; si quieres representar un rango en otra hoja, puedes usar un nombre. Los rangos nombrados son algunas de las funciones más poderosas de Microsoft Excel, especialmente cuando se usan como rango de origen para controles de listas, tablas dinámicas, gráficos, etc.
Trabajar con Rangos con Nombre Usando Microsoft Excel
Crear Rangos con Nombre
Los siguientes pasos describen cómo nombrar una celda o rango de celdas usando MS Excel. Este método se aplica a Microsoft Office Excel 2003, Microsoft Excel 97, 2000 y 2002.
- Selecciona la celda o rango de celdas que deseas nombrar.
- Haz clic en la Caja de nombres al extremo izquierdo de la barra de fórmulas.
- Escribe el nombre para las celdas.
- Presiona ENTER.
Trabajar con Rangos con Nombre Usando Aspose.Cells
Aquí, usamos la API de Aspose.Cells para realizar la tarea.
Aspose.Cells proporciona una clase, Workbook que representa un archivo de Microsoft Excel. La clase Workbook contiene una colección worksheets que permite el acceso a cada hoja de cálculo en un archivo de Excel. Una hoja de cálculo está representada por la clase Worksheet. La clase Worksheet proporciona una colección cells.
Crear Rango con Nombre
Es posible crear un rango con nombre llamando al método sobrecargado createRange(string, string) de la colección cells. Una versión típica del método createRange(string) toma los siguientes parámetros:
- Nombre de la celda superior izquierda, el nombre de la celda superior izquierda en el rango.
- Nombre de la celda inferior derecha, el nombre de la celda inferior derecha en el rango.
Cuando se llama al método createRange(string), devuelve el rango recién creado como una instancia de la clase Range. Utilice este objeto Range para configurar el rango con nombre. Por ejemplo, establezca el nombre del rango utilizando la propiedad name. El siguiente ejemplo muestra cómo crear un rango con nombre de celdas que se extiende sobre B4:G14.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Create Named Range Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the Excel file from the file input
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.worksheets.get(0);
// Creating a named range
const range = worksheet.cells.createRange("B4", "G14");
// Setting the name of the named range
range.name = "TestRange";
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Named range created successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Ingresar Datos en las Celdas en el Rango Nombrado
Puede insertar datos en las celdas individuales de un rango siguiendo el patrón
- JavaScript: Rango[fila,columna]
Supongamos que tienes un rango de celdas que abarca de A1 a C4. La matriz hace 4 * 3 = 12 celdas. Las celdas de rango individuales están dispuestas secuencialmente: Rango[0,0], Rango[0,1], Rango[0,2], Rango[1,0], Rango[1,1], Rango[1,2], Rango[2,0], Rango[2,1], Rango[2,2], Rango[3,0], Rango[3,1], Rango[3,2].
Utiliza las siguientes propiedades para identificar las celdas en el rango:
- firstRow devuelve el índice de la primera fila en el rango nombrado.
- firstColumn devuelve el índice de la primera columna en el rango nombrado.
- rowCount devuelve el número total de filas en el rango nombrado.
- columnCount devuelve el número total de columnas en el rango con nombre.
El siguiente ejemplo muestra cómo ingresar algunos valores en las celdas de un rango especificado.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Create Named Range Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
// Instantiate or load workbook
let workbook;
if (fileInput.files && fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Get the first worksheet in the workbook.
const worksheet1 = workbook.worksheets.get(0);
// Create a range of cells based on H1:J4.
const range = worksheet1.cells.createRange("H1", "J4");
// Name the range.
range.name = "MyRange";
// Input some data into cells in the range.
range.get(0, 0).value = "USA";
range.get(0, 1).value = "SA";
range.get(0, 2).value = "Israel";
range.get(1, 0).value = "UK";
range.get(1, 1).value = "AUS";
range.get(1, 2).value = "Canada";
range.get(2, 0).value = "France";
range.get(2, 1).value = "India";
range.get(2, 2).value = "Egypt";
range.get(3, 0).value = "China";
range.get(3, 1).value = "Philipine";
range.get(3, 2).value = "Brazil";
// Save the excel file.
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'rangecells.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created and range populated successfully! Click the download link to get the file.</p>';
});
</script>
</html>
Identificar Celdas en el Rango con Nombre
Puedes insertar datos en las celdas individuales de un rango siguiendo el patrón:
- JavaScript: Rango[fila,columna]
Si tienes un rango con nombre que abarca de A1 a C4. La matriz hace 4 * 3 = 12 celdas. Las celdas de rango individuales están dispuestas secuencialmente: Rango[0,0], Rango[0,1], Rango[0,2], Rango[1,0], Rango[1,1], Rango[1,2], Rango[2,0], Rango[2,1], Rango[2,2], Rango[3,0], Rango[3,1], Rango[3,2].
Utiliza las siguientes propiedades para identificar las celdas en el rango:
- firstRow devuelve el índice de la primera fila en el rango nombrado.
- firstColumn devuelve el índice de la primera columna en el rango nombrado.
- rowCount devuelve el número total de filas en el rango nombrado.
- columnCount devuelve el número total de columnas en el rango con nombre.
El siguiente ejemplo muestra cómo ingresar algunos valores en las celdas de un rango especificado.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Get Named Range</title>
</head>
<body>
<h1>Get Named Range Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Getting the specified named range
const range = workbook.worksheets.rangeByName("TestRange");
if (!range) {
document.getElementById('result').innerHTML = '<p style="color: red;">Named range "TestRange" not found.</p>';
return;
}
// Identify range cells and display properties
const firstRow = range.firstRow;
const firstColumn = range.firstColumn;
const rowCount = range.rowCount;
const columnCount = range.columnCount;
const html = [
`<p>First Row : ${firstRow}</p>`,
`<p>First Column : ${firstColumn}</p>`,
`<p>Row Count : ${rowCount}</p>`,
`<p>Column Count : ${columnCount}</p>`
].join('');
document.getElementById('result').innerHTML = html;
});
</script>
</html>
Acceder a rangos con nombres
Acceder a un Rango Nombrado Específico
Llame al método rangeByName(string) de la colección worksheets para obtener un rango por el nombre especificado. Un método rangeByName(string) típico toma el nombre del rango con nombre y devuelve el rango con nombre especificado como una instancia de la clase Range. El siguiente ejemplo muestra cómo acceder a un rango especificado por su nombre.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Named Range Example</title>
</head>
<body>
<h1>Get Named Range Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Getting the specified named range
const worksheets = workbook.worksheets;
const range = worksheets.rangeByName("TestRange");
if (range !== null) {
document.getElementById('result').innerHTML = `<p>Named Range : ${range.refersTo}</p>`;
} else {
document.getElementById('result').innerHTML = '<p style="color: red;">Named range "TestRange" not found.</p>';
}
});
</script>
</html>
Acceder a todos los rangos con nombres en una hoja de cálculo
Llame al método worksheet de la colección namedRanges() para obtener todos los rangos con nombre en una hoja de cálculo. El método namedRanges() devuelve una matriz de todos los rangos con nombre en la colección worksheets.
El siguiente ejemplo muestra cómo acceder a todos los rangos nombrados en un libro de trabajo.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Named Ranges</title>
</head>
<body>
<h1>Get Named Ranges Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Getting all named ranges
const ranges = workbook.worksheets.namedRanges;
if (ranges) {
// Some collections expose 'count', others may expose 'length'
const total = (typeof ranges.count !== 'undefined') ? ranges.count : ranges.length;
document.getElementById('result').innerHTML = `<p style="color: green;">Total Number of Named Ranges: ${total}</p>`;
} else {
document.getElementById('result').innerHTML = '<p style="color: orange;">No named ranges found.</p>';
}
});
</script>
</html>
Copiar rangos con nombres
Aspose.Cells proporciona el método range.copy(Range, PasteOptions) para copiar un rango de celdas con formato en otro rango.
El siguiente ejemplo muestra cómo copiar un rango de celdas fuente en otro rango con nombre.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Copy Ranges</title>
</head>
<body>
<h1>Copy Ranges Example</h1>
<p>Select an Excel file to modify, or leave empty to create a new workbook.</p>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, BorderType, CellBorderType, Color } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
// Instantiate a new Workbook or load from selected file
let workbook;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Get all the worksheets in the book.
const worksheets = workbook.worksheets;
// Get the first worksheet in the worksheets collection.
const worksheet = workbook.worksheets.get(0);
// Create a range of cells.
const range1 = worksheet.cells.createRange("E12", "I12");
// Name the range.
range1.name = "MyRange";
// Set the outline border to the range.
range1.outlineBorder = { borderType: BorderType.TopBorder, style: CellBorderType.Medium, color: new Color(0, 0, 128) };
range1.outlineBorder = { borderType: BorderType.BottomBorder, style: CellBorderType.Medium, color: new Color(0, 0, 128) };
range1.outlineBorder = { borderType: BorderType.LeftBorder, style: CellBorderType.Medium, color: new Color(0, 0, 128) };
range1.outlineBorder = { borderType: BorderType.RightBorder, style: CellBorderType.Medium, color: new Color(0, 0, 128) };
// Input some data with some formattings into a few cells in the range.
range1.get(0, 0).putValue("Test");
range1.get(0, 4).putValue("123");
// Create another range of cells.
const range2 = worksheet.cells.createRange("B3", "F3");
// Name the range.
range2.name = "testrange";
// Copy the first range into second range.
range2.copy(range1);
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'copyranges.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>