Access the Text Box by the Name with Node.js via C++
Access the Text Box by the Name
Earlier, text boxes were accessed by index from the Worksheet.getTextBoxes() collection, but now you can also access the text box by name from this collection. This is a convenient and quick way to access your text box if you already know its name.
The following sample code first creates a text box and assigns it some text and name. Then in the next lines, we access the same text box by its name and print its text.
Node.js code to access the text box by name
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sample.xlsx");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook(filePath);
// Access first worksheet from the collection
const sheet = workbook.getWorksheets().get(0);
// Add the TextBox to the worksheet
const idx = sheet.getTextBoxes().add(10, 10, 10, 10);
// Access newly created TextBox using its index & name it
const tb1 = sheet.getTextBoxes().get(idx);
tb1.setName("MyTextBox");
// Set text for the TextBox
tb1.setText("This is MyTextBox");
// Access the same TextBox via its name
const tb2 = sheet.getTextBoxes().get("MyTextBox");
// Display the text of the TextBox accessed via name
console.log(tb2.getText());
console.log("Press any key to continue...");
Console output generated by the sample code
Here is the console output of the above sample code.
This is MyTextBox