Create Workbook and Worksheet Scoped Named Ranges with Node.js via C++
Contents
[
Hide
]
Microsoft Excel allows users to define named ranges with two different scopes: workbook (also known as global scope) and worksheet.
- Named ranges with a workbook scope can be accessed from any worksheet within that workbook by simply using its name.
- Worksheet scoped named ranges are accessed with the reference of the particular worksheet in which it was created.
Aspose.Cells for Node.js via C++ provides the same functionality as Microsoft Excel for adding workbook and worksheet scoped named ranges. When creating a worksheet scoped named range, the worksheet reference should be used in the named range to specify it as a worksheet scoped named range.
Adding a Named Range with Workbook Scoped
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create a new Workbook object
const workbook = new AsposeCells.Workbook();
// Get first worksheet of the workbook
const sheet = workbook.getWorksheets().get(0);
// Get worksheet's cells collection
const cells = sheet.getCells();
// Create a range of Cells from Cell A1 to C10
const workbookScope = cells.createRange("A1", "C10");
// Assign the name to workbook scope named range
workbookScope.setName("workbookScope");
// Save the workbook
workbook.save(path.join(dataDir, "WorkbookScope.out.xlsx"));
Adding a Named Range with Worksheet Scope
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create a new Workbook object
const workbook = new AsposeCells.Workbook();
// Get first worksheet of the workbook
const sheet = workbook.getWorksheets().get(0);
// Get worksheet's cells collection
const cells = sheet.getCells();
// Create a range of Cells
const localRange = cells.createRange("A1", "C10");
// Assign name to range with sheet reference
localRange.setName("Sheet1!local");
// Save the workbook
workbook.save(path.join(dataDir, "output.out.xls"));
Advance topics
- Create Access and Copy Named Ranges
- Format and Modify Named Ranges
- Get Range with External Links
- Implementing Non-Sequential Ranges