Node.js経由でC++を使用して名前付き範囲に数式を設定する
名前付き範囲の式の設定
Excelアプリケーションと同様に、Aspose.Cells APIはRange.getRefersTo()プロパティを使用して、名前付き範囲の数式を指定する機能を提供します。この機能には、多数の使い道があります。いくつか例を挙げます。
名前付き範囲に簡単な式を設定する
簡単な式は、同じワークシート(または異なるワークシート)内の別のセルへの参照である場合があります。次の例は、新しいスプレッドシートで名前付き範囲を作成し、その参照先を別のセルに設定します。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create an instance of Workbook
const book = new AsposeCells.Workbook();
// Get the WorksheetCollection
const worksheets = book.getWorksheets();
// Add a new Named Range with name "NewNamedRange"
const index = worksheets.getNames().add("NewNamedRange");
// Access the newly created Named Range
const name = worksheets.getNames().get(index);
// Set RefersTo property of the Named Range to a formula. Formula references another cell in the same worksheet
name.setRefersTo("=Sheet1!$A$3");
// Set the formula in the cell A1 to the newly created Named Range
worksheets.get(0).getCells().get("A1").setFormula("NewNamedRange");
// Insert the value in cell A3 which is being referenced in the Named Range
worksheets.get(0).getCells().get("A3").putValue("This is the value of A3");
// Calculate formulas
book.calculateFormula();
// Save the result in XLSX format
book.save(path.join(dataDir, "output_out.xlsx"));
名前付き範囲に複雑な式を設定する
複雑な式は、動的な範囲や異なるワークシートの複数のセルにまたがる式など、さまざまなものになります。次の例は、INDEX 関数を使用して、リスト内の位置に基づいて値を取得する動的な範囲を作成します。
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 book = new AsposeCells.Workbook();
// Get the WorksheetCollection
const worksheets = book.getWorksheets();
// Add a new Named Range with name "data"
let index = worksheets.getNames().getCount();
worksheets.getNames().add("data");
// Access the newly created Named Range from the collection
const data = worksheets.getNames().get(index);
// Set RefersTo property of the Named Range to a cell range in same worksheet
data.setRefersTo("=Sheet1!$A$1:$A$10");
// Add another Named Range with name "range"
index = worksheets.getNames().getCount();
worksheets.getNames().add("range");
// Access the newly created Named Range from the collection
const range = worksheets.getNames().get(index);
// Set RefersTo property to a formula using the Named Range data
range.setRefersTo("=INDEX(data,Sheet1!$A$1,1):INDEX(data,Sheet1!$A$1,9)");
// Save the workbook
book.save(path.join(dataDir, "output_out.xlsx"));
異なるワークシートの2つのセルから値を合計する名前付き範囲を使用するもう1つの例がこちら
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create an instance of Workbook
const book = new AsposeCells.Workbook();
// Get the WorksheetCollection
const worksheets = book.getWorksheets();
// Insert some data in cell A1 of Sheet1
worksheets.get("Sheet1").getCells().get("A1").putValue(10);
// Add a new Worksheet and insert a value to cell A1
worksheets.get(worksheets.add()).getCells().get("A1").putValue(10);
// Add a new Named Range with name "range"
const index = worksheets.getNames().add("range");
// Access the newly created Named Range from the collection
const range = worksheets.getNames().get(index);
// Set RefersTo property of the Named Range to a SUM function
range.setRefersTo("=SUM(Sheet1!$A$1,Sheet2!$A$1)");
// Insert the Named Range as formula to 3rd worksheet
worksheets.get(worksheets.add()).getCells().get("A1").setFormula("range");
// Calculate formulas
book.calculateFormula();
// Save the result in XLSX format
book.save(path.join(dataDir, "output_out.xlsx"));